-
Notifications
You must be signed in to change notification settings - Fork 239
Fix OpenAPI error examples missing when service-level and operation-level errors share HTTP status codes #2754
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6b6e115
openapi bug test
justincange 3f376e0
fix bug, cleanup
justincange 84ef561
Merge branch 'smithy-lang:main' into jc/error-bug
justincange 6c94e37
fix deconflict logic && remove poor comments
justincange 560459f
rename test file
justincange f2649fc
rename test file
justincange a08ca9e
Apply suggestions from code review
justincange b551823
cleanup comments, bring test out into a file
justincange 0e765df
new test files
justincange e0d0a9c
client error in operation
justincange 23bfcb9
new gradle build
justincange f8790e0
Update smithy-openapi/src/test/java/software/amazon/smithy/openapi/fr…
justincange File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...c/test/java/software/amazon/smithy/openapi/fromsmithy/ErrorDeconflictingExamplesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package software.amazon.smithy.openapi.fromsmithy; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.InputStream; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.openapi.OpenApiConfig; | ||
import software.amazon.smithy.utils.IoUtils; | ||
|
||
/** | ||
* Tests that all error examples are included in the openapi when error deconflicting (`onErrorStatusConflict`) is set to `oneOf`. | ||
*/ | ||
public class ErrorDeconflictingExamplesTest { | ||
|
||
@Test | ||
public void serviceLevelErrorsShouldNotOverrideOperationSpecificExamples() { | ||
Model model = Model.assembler() | ||
.discoverModels() | ||
.addImport(getClass().getResource("error-deconflicting-test.smithy")) | ||
.assemble() | ||
.unwrap(); | ||
|
||
OpenApiConfig config = new OpenApiConfig(); | ||
config.setService(ShapeId.from("test.service.error#TestService")); | ||
config.setOnErrorStatusConflict(OpenApiConfig.ErrorStatusConflictHandlingStrategy.ONE_OF); | ||
|
||
Node result = OpenApiConverter.create().config(config).convertToNode(model); | ||
|
||
InputStream expectedStream = getClass().getResourceAsStream("error-deconflicting-test.openapi.json"); | ||
if (expectedStream == null) { | ||
fail("Expected OpenAPI output file not found: error-deconflicting-test.openapi.json"); | ||
} | ||
|
||
Node expected = Node.parse(IoUtils.toUtf8String(expectedStream)); | ||
Node.assertEquals(result, expected); | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
...resources/software/amazon/smithy/openapi/fromsmithy/error-deconflicting-test.openapi.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
{ | ||
"openapi": "3.0.2", | ||
"info": { | ||
"title": "TestService", | ||
"version": "1.0.0" | ||
}, | ||
"paths": { | ||
"/test": { | ||
"post": { | ||
"operationId": "TestOp", | ||
"requestBody": { | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/TestOpRequestContent" | ||
}, | ||
"examples": { | ||
"TestOp_example1": { | ||
"summary": "Success example", | ||
"description": "", | ||
"value": { | ||
"value": "good" | ||
} | ||
}, | ||
"TestOp_example2": { | ||
"summary": "Client error (defined on the service) example", | ||
"description": "", | ||
"value": { | ||
"value": "client bad" | ||
} | ||
}, | ||
"TestOp_example3": { | ||
"summary": "Custom error example", | ||
"description": "", | ||
"value": { | ||
"value": "bad" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"responses": { | ||
"200": { | ||
"description": "TestOp 200 response", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/TestOpResponseContent" | ||
}, | ||
"examples": { | ||
"TestOp_example1": { | ||
"summary": "Success example", | ||
"description": "", | ||
"value": { | ||
"result": "success" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"400": { | ||
"description": "TestOp400Error 400 response", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/TestOp400ErrorResponseContent" | ||
}, | ||
"examples": { | ||
"TestOp_example2": { | ||
"summary": "Client error (defined on the service) example", | ||
"description": "", | ||
"value": { | ||
"message": "Client error occurred" | ||
} | ||
}, | ||
"TestOp_example3": { | ||
"summary": "Custom error example", | ||
"description": "", | ||
"value": { | ||
"message": "Custom error occurred", | ||
"code": "CUSTOM_ERROR", | ||
"details": "This should appear in 400 response examples" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"ClientError": { | ||
"type": "object", | ||
"properties": { | ||
"message": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"CustomError": { | ||
"type": "object", | ||
"properties": { | ||
"message": { | ||
"type": "string" | ||
}, | ||
"code": { | ||
"type": "string" | ||
}, | ||
"details": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"TestOp400ErrorResponseContent": { | ||
"oneOf": [ | ||
{ | ||
"$ref": "#/components/schemas/CustomError" | ||
}, | ||
{ | ||
"$ref": "#/components/schemas/ClientError" | ||
} | ||
] | ||
}, | ||
"TestOpRequestContent": { | ||
"type": "object", | ||
"properties": { | ||
"value": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"TestOpResponseContent": { | ||
"type": "object", | ||
"properties": { | ||
"result": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
.../test/resources/software/amazon/smithy/openapi/fromsmithy/error-deconflicting-test.smithy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
$version: "2.0" | ||
namespace test.service.error | ||
use aws.protocols#restJson1 | ||
|
||
@restJson1 | ||
service TestService { | ||
version: "1.0.0", | ||
operations: [TestOp] | ||
errors: [ClientError] | ||
} | ||
|
||
@http(method: "POST", uri: "/test") | ||
operation TestOp { | ||
input: TestInput, | ||
output: TestOutput, | ||
errors: [CustomError] // Operation-specific 400 error | ||
} | ||
|
||
structure TestInput { value: String } | ||
structure TestOutput { result: String } | ||
|
||
|
||
@error("client") | ||
@httpError(400) | ||
structure ClientError { | ||
message: String | ||
} | ||
|
||
// Operation-specific 400 error - should have examples generated | ||
@error("client") | ||
@httpError(400) | ||
structure CustomError { | ||
message: String, | ||
code: String, | ||
details: String | ||
} | ||
|
||
apply TestOp @examples([ | ||
{ | ||
title: "Success example" | ||
input: { value: "good" } | ||
output: { result: "success" } | ||
}, | ||
{ | ||
title: "Client error (defined on the service) example" | ||
input: { value: "client bad" } | ||
error: { | ||
shapeId: ClientError | ||
content: { | ||
message: "Client error occurred" | ||
} | ||
} | ||
allowConstraintErrors: true | ||
}, | ||
{ | ||
title: "Custom error example" | ||
input: { value: "bad" } | ||
error: { | ||
shapeId: CustomError | ||
content: { | ||
message: "Custom error occurred" | ||
code: "CUSTOM_ERROR" | ||
details: "This should appear in 400 response examples" | ||
} | ||
} | ||
allowConstraintErrors: true | ||
} | ||
]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.