Skip to content

Commit

Permalink
Lower input validation severity for error examples
Browse files Browse the repository at this point in the history
  • Loading branch information
srchase committed Aug 24, 2023
1 parent 302ace8 commit 6556b90
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
7 changes: 4 additions & 3 deletions docs/source-2.0/spec/documentation-traits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ Each ``example`` trait value is a structure with the following members:
- Provides an error shape ID and example error parameters for the
operation.

The values provided for the ``input`` and ``output`` members MUST be
compatible with the shapes and constraints of the corresponding structure.
These values use the same semantics and format as
When ``input`` and ``output`` members are present, both MUST be compatible
with the shapes and constraints of the corresponding structure. When ``input``
and ``error`` members are present, any validation events for the ``input`` will
be emitted as a ``WARNING``. These values use the same semantics and format as
:ref:`custom trait values <trait-node-values>`.

A value for ``output`` or ``error`` SHOULD be provided. However, both
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import software.amazon.smithy.model.traits.ExamplesTrait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.NodeValidationVisitor;
import software.amazon.smithy.model.validation.Severity;
import software.amazon.smithy.model.validation.ValidationEvent;

/**
Expand All @@ -47,15 +48,23 @@ private List<ValidationEvent> validateExamples(Model model, OperationShape shape
List<ExamplesTrait.Example> examples = trait.getExamples();

for (ExamplesTrait.Example example : examples) {
boolean isOutputDefined = example.getOutput().isPresent();
boolean isErrorDefined = example.getError().isPresent();

model.getShape(shape.getInputShape()).ifPresent(input -> {
NodeValidationVisitor validator = createVisitor(
"input", example.getInput(), model, shape, example);
events.addAll(input.accept(validator));
List<ValidationEvent> inputValidationEvents = input.accept(validator);
// Lower severity of input validation if this is an example of an error.
if (isErrorDefined) {
for (ValidationEvent event : inputValidationEvents) {
events.add(event.toBuilder().severity(Severity.WARNING).build());
}
} else {
events.addAll(inputValidationEvents);
}
});

boolean isOutputDefined = example.getOutput().isPresent();
boolean isErrorDefined = example.getError().isPresent();

if (isOutputDefined && isErrorDefined) {
events.add(error(shape, trait, String.format(
"Example: `%s` has both output and error defined, only one should be present.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
[WARNING] ns.foo#Operation: Example output of `Testing 2`: Invalid structure member `additional` found for `ns.foo#OperationOutput` | ExamplesTrait
[ERROR] ns.foo#Operation: Example output of `Testing 2`: Missing required structure member `bam` for `ns.foo#OperationOutput` | ExamplesTrait
[WARNING] ns.foo#Operation: Example error of `Testing 1`: Invalid structure member `extra` found for `ns.foo#OperationError` | ExamplesTrait
[WARNING] ns.foo#Operation: Example input of `Testing 4`: Missing required structure member `foo` for `ns.foo#OperationInput` | ExamplesTrait
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
"bat": "baz"
}
}
},
{
"title": "Testing 4",
"input": {
"baz": "baz"
},
"error": {
"shapeId": "ns.foo#OperationError",
"content": {
"bat": "baz"
}
}
}
]
}
Expand Down

0 comments on commit 6556b90

Please sign in to comment.