Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http-Date Trait Value Validation Bug Fix #1946

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@

package software.amazon.smithy.model.validation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;

import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.knowledge.NullableIndex;
Expand Down Expand Up @@ -51,6 +47,7 @@
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.TimestampShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.TimestampFormatTrait;
import software.amazon.smithy.model.validation.node.NodeValidatorPlugin;
import software.amazon.smithy.model.validation.node.TimestampValidationStrategy;
import software.amazon.smithy.utils.BuilderRef;
Expand Down Expand Up @@ -354,11 +351,20 @@ public List<ValidationEvent> unionShape(UnionShape shape) {

@Override
public List<ValidationEvent> memberShape(MemberShape shape) {
List<ValidationEvent> events = applyPlugins(shape);
events.addAll(model.getShape(shape.getTarget())
.map(member -> member.accept(this))
.orElse(ListUtils.of()));
return events;
List<ValidationEvent> allEvents = applyPlugins(shape);
Optional<Shape> target = model.getShape(shape.getTarget());
if (target.map(t -> t.isTimestampShape() && shape.hasTrait(TimestampFormatTrait.class)).orElse(false)) {
TimestampShape t = target.get().asTimestampShape().get();
TimestampFormatTrait fmt = shape.expectTrait(TimestampFormatTrait.class);
TimestampShape updatedShape = t.toBuilder().addTrait(fmt).build();
allEvents.addAll(updatedShape.accept(this));
} else {
allEvents.addAll(model.getShape(shape.getTarget())
.map(member -> member.accept(this))
.orElse(ListUtils.of()));
}

return allEvents;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ final class TimestampFormatPlugin implements NodeValidatorPlugin {
public void apply(Shape shape, Node value, Context context, Emitter emitter) {
if (shape instanceof TimestampShape) {
validate(shape, shape.getTrait(TimestampFormatTrait.class).orElse(null), value, emitter);
} else if (shape instanceof MemberShape && shape.getTrait(TimestampFormatTrait.class).isPresent()) {
// Only perform timestamp format validation on a member when it references
// a timestamp shape and the member has an explicit timestampFormat trait.
validate(shape, shape.getTrait(TimestampFormatTrait.class).get(), value, emitter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public static Collection<Object[]> data() {
{"ns.foo#HttpDate", "\"Tuesday, 29 April 2014 18:30:38 GMT\"", new String[] {"Invalid value provided for http-date formatted timestamp. Expected a string value that matches the IMF-fixdate production of RFC 7231 section-7.1.1.1. Found: Tuesday, 29 April 2014 18:30:38 GMT"}},
{"ns.foo#HttpDate", "\"Tue, 29 Apr 2014 18:30:38 PST\"", new String[] {"Invalid value provided for http-date formatted timestamp. Expected a string value that matches the IMF-fixdate production of RFC 7231 section-7.1.1.1. Found: Tue, 29 Apr 2014 18:30:38 PST"}},
{"ns.foo#HttpDate", "11", new String[] {"Invalid value provided for http-date formatted timestamp. Expected a string value that matches the IMF-fixdate production of RFC 7231 section-7.1.1.1. Found: number"}},
{"ns.foo#Structure4", "{\"httpDate\": \"Tue, 29 Apr 2014 18:30:38 GMT\"}", null},

// date-time
{"ns.foo#DateTime", "\"1985-04-12T23:20:50.52Z\"", null},
Expand All @@ -257,8 +258,7 @@ public static Collection<Object[]> data() {
// timestamp member with format.
{"ns.foo#TimestampList", "[\"1985-04-12T23:20:50.52Z\"]", null},
{"ns.foo#TimestampList", "[\"1985-04-12T23:20:50.52-07:00\"]", new String[] {
"0: Invalid string value, `1985-04-12T23:20:50.52-07:00`, provided for timestamp, `smithy.api#Timestamp`. Expected an RFC 3339 formatted timestamp (e.g., \"1985-04-12T23:20:50.52Z\")",
"0: Invalid string value, `1985-04-12T23:20:50.52-07:00`, provided for timestamp, `ns.foo#TimestampList$member`. Expected an RFC 3339 formatted timestamp (e.g., \"1985-04-12T23:20:50.52Z\")"
"0: Invalid string value, `1985-04-12T23:20:50.52-07:00`, provided for timestamp, `smithy.api#Timestamp`. Expected an RFC 3339 formatted timestamp (e.g., \"1985-04-12T23:20:50.52Z\")"
}},
{"ns.foo#TimestampList", "[123]", new String[] {"0: Expected a string value for a date-time timestamp (e.g., \"1985-04-12T23:20:50.52Z\")"}},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@
}
}
},
"ns.foo#Structure4": {
"type": "structure",
"members": {
"httpDate": {
"target": "smithy.api#Timestamp",
"traits": {
"smithy.api#timestampFormat": "http-date"
}
}
}
},
"ns.foo#Service": {
"type": "service",
"version": "2017-17-01",
Expand Down