Skip to content

Conversation

justincange
Copy link
Contributor

We noticed that when a Smithy service defines both service-level errors and operation-specific errors that map to the same HTTP status code, the generated OpenAPI spec includes the correct schema structure but completely omits error response examples.

Expected:
ErrorExamples from operation @examples should appear in HTTP error response sections.
Actual: Error response examples are missing while request and success examples work correctly.

This affects API documentation quality for services using both service-level and operation-specific errors.

This PR has the test case and a potential fix. Thanks!

@justincange justincange requested a review from a team as a code owner August 25, 2025 20:01
Comment on lines 108 to 132
// The bug: When ValidationException (service-level) and CustomError (operation-level)
// both map to 400, the service-level error takes precedence and operation-specific
// examples are lost
assertTrue(jsonContent.getMember("examples").isPresent(),
"BUG: Service-level ValidationException overrides operation-specific CustomError examples. " +
"Expected CustomError examples to appear in 400 response but they are missing.");

ObjectNode examples = jsonContent.expectMember("examples").expectObjectNode();
assertFalse(examples.getMembers().isEmpty(),
"Expected CustomError examples in 400 response content");

// Look for our specific custom error example content
boolean foundCustomErrorExample = examples.getMembers().values().stream()
.filter(Node::isObjectNode)
.map(node -> node.expectObjectNode())
.filter(example -> example.getMember("value").isPresent())
.map(example -> example.expectMember("value").expectObjectNode())
.anyMatch(value ->
value.getMember("code").filter(node -> "CUSTOM_ERROR".equals(node.expectStringNode().getValue())).isPresent() &&
value.getMember("message").filter(node -> "Custom error occurred".equals(node.expectStringNode().getValue())).isPresent()
);

assertTrue(foundCustomErrorExample,
"Expected to find CustomError example with code 'CUSTOM_ERROR' and message 'Custom error occurred' " +
"but service-level ValidationException appears to be overriding it");
Copy link
Contributor

@haydenbaker haydenbaker Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing all of this manual assertion, let's just have the expected generated openapi file as a test resource and assert that it matches. Check AwsRestJson1ProtocolTest to see how that's done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, tried to do it in a more focused, concise way just for this use case

…omsmithy/ErrorDeconflictingExamplesTest.java

Co-authored-by: Miles Ziemer <[email protected]>
@sugmanue sugmanue merged commit 5740c35 into smithy-lang:main Sep 16, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants