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

Validate that query literal do not conflict with query params #1786

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -23,10 +23,15 @@
import java.util.Map;
import java.util.Set;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.pattern.UriPattern;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.HttpQueryTrait;
import software.amazon.smithy.model.traits.HttpTrait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.model.validation.ValidationUtils;
Expand All @@ -41,7 +46,7 @@ public List<ValidationEvent> validate(Model model) {
if (!model.isTraitApplied(HttpQueryTrait.class)) {
return Collections.emptyList();
} else {
return validateBindings(getQueryBindings(model));
return validateBindings(getQueryBindings(model), getStructureToOperations(model));
}
}

Expand All @@ -64,7 +69,10 @@ private Map<StructureShape, Map<String, Set<String>>> getQueryBindings(Model mod
return queryBindings;
}

private List<ValidationEvent> validateBindings(Map<StructureShape, Map<String, Set<String>>> queryBindings) {
private List<ValidationEvent> validateBindings(
Map<StructureShape, Map<String, Set<String>>> queryBindings,
Map<StructureShape, List<OperationShape>> structureToOperations
) {
List<ValidationEvent> events = new ArrayList<>();

for (Map.Entry<StructureShape, Map<String, Set<String>>> entry : queryBindings.entrySet()) {
Expand All @@ -77,8 +85,39 @@ private List<ValidationEvent> validateBindings(Map<StructureShape, Map<String, S
paramsToMembers.getKey(), ValidationUtils.tickedList(paramsToMembers.getValue()))));
}
}

List<OperationShape> operations = structureToOperations.getOrDefault(entry.getKey(),
Collections.emptyList());
for (OperationShape operation : operations) {
UriPattern pattern = operation.expectTrait(HttpTrait.class).getUri();
for (Map.Entry<String, String> literalEntry : pattern.getQueryLiterals().entrySet()) {
String literalKey = literalEntry.getKey();
if (entry.getValue().containsKey(literalKey)) {
events.add(error(entry.getKey(), String.format(
"`httpQuery` parameter name binding conflicts found for the `%s` parameter, the "
sugmanue marked this conversation as resolved.
Show resolved Hide resolved
+ "http trait for the `%s` operation defines a query literal with the same name",
literalKey, operation.getId())));
}
}
}
}

return events;
}

private Map<StructureShape, List<OperationShape>> getStructureToOperations(Model model) {
Map<StructureShape, List<OperationShape>> structureToOperations = new HashMap<>();
for (ServiceShape service : model.getServiceShapes()) {
for (OperationShape operation : TopDownIndex.of(model).getContainedOperations(service)) {
sugmanue marked this conversation as resolved.
Show resolved Hide resolved
if (operation.hasTrait(HttpTrait.class)) {
model.expectShape(operation.getInputShape())
sugmanue marked this conversation as resolved.
Show resolved Hide resolved
.asStructureShape()
.ifPresent(structure -> structureToOperations
.computeIfAbsent(structure, key -> new ArrayList<>())
.add(operation));
}
}
}
return structureToOperations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ERROR] smithy.example#OperationOneInput: `httpQuery` parameter name binding conflicts found for the `code` parameter, the http trait for the `smithy.example#OperationOne` operation defines a query literal with the same name | HttpQueryTrait
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
$version: "2"
namespace smithy.example

// OperationOne defines a query literal `code` that conflicts with the
// query param defined in the input structure.

service SmithyExample {
hpmellema marked this conversation as resolved.
Show resolved Hide resolved
operations: [
OperationOne
]
}

@http(code: 200, method: "GET", uri: "/example?code")
@readonly
operation OperationOne {
input: OperationOneInput
output: OperationOneOutput
}

structure OperationOneInput {
@httpQuery("code")
code: String

@httpQuery("state")
state: String
}

structure OperationOneOutput {
@required
@httpHeader("Content-Type")
contentType: String

@required
@httpPayload
content: Blob
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ERROR] smithy.example#OperationOneInput: `httpQuery` parameter name binding conflicts found for the `code` parameter, the http trait for the `smithy.example#OperationOne` operation defines a query literal with the same name | HttpQueryTrait
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
$version: "2"
namespace smithy.example

// OperationOne defines a query literal `code` with value `bar` that
// conflicts with the query param defined in the input structure.

service SmithyExample {
operations: [
OperationOne
]
}

@http(code: 200, method: "GET", uri: "/example?code=bar")
@readonly
operation OperationOne {
input: OperationOneInput
output: OperationOneOutput
}

structure OperationOneInput {
@httpQuery("code")
code: String

@httpQuery("state")
state: String
}

structure OperationOneOutput {
@required
@httpHeader("Content-Type")
contentType: String

@required
@httpPayload
content: Blob
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[ERROR] smithy.example#OperationOneInput: `httpQuery` parameter name binding conflicts found for the `code` parameter, the http trait for the `smithy.example#OperationOne` operation defines a query literal with the same name | HttpQueryTrait
[ERROR] smithy.example#OperationOneInput: `httpQuery` parameter name binding conflicts found for the `code` parameter, the http trait for the `smithy.example#OperationTwo` operation defines a query literal with the same name | HttpQueryTrait
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
$version: "2"
namespace smithy.example

// OperationOne and OperationTwo define a query literal `code` and
// `code=bar` that conflicts with the query param defined in the input
// structure.

service SmithyExample {
operations: [
OperationOne
OperationTwo
]
}

@http(code: 200, method: "GET", uri: "/one?code")
@readonly
operation OperationOne {
input: OperationOneInput
output: OperationOneOutput
}

@http(code: 200, method: "GET", uri: "/two?code=bar")
@readonly
operation OperationTwo {
input: OperationOneInput
output: OperationOneOutput
}

structure OperationOneInput {
@httpQuery("code")
code: String

@httpQuery("state")
state: String
}

structure OperationOneOutput {
@required
@httpHeader("Content-Type")
contentType: String

@required
@httpPayload
content: Blob
}