generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 21
Add document discriminator sanitizer for JSON protocols #932
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
7 commits
Select commit
Hold shift + click to select a range
cb64f4f
Add document discrinator sanitizer for JSON protocols
joewyz 5b95834
Address comments
joewyz 6539dda
Apply spotless
joewyz 8e9c935
Add handling for code based exceptions
joewyz 307386a
Add new method to parse errortype from document, move error sanitizer…
joewyz 8e9b84f
Move the discriminator extraction for error to httpErrorDeserializer
joewyz d179735
Remove redundant code, modify ErrorPayloadParser interface to make pr…
joewyz 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
Some comments aren't visible on the classic Files Changed page.
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
41 changes: 41 additions & 0 deletions
41
.../it/java/software/amazon/smithy/java/client/aws/jsonprotocols/AwsJson11ProtocolTests.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,41 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.amazon.smithy.java.client.aws.jsonprotocols; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import software.amazon.smithy.java.io.ByteBufferUtils; | ||
| import software.amazon.smithy.java.io.datastream.DataStream; | ||
| import software.amazon.smithy.java.protocoltests.harness.*; | ||
| import software.amazon.smithy.model.node.Node; | ||
| import software.amazon.smithy.model.node.ObjectNode; | ||
|
|
||
| @ProtocolTest( | ||
| service = "aws.protocoltests.json#JsonProtocol", | ||
| testType = TestType.CLIENT) | ||
| public class AwsJson11ProtocolTests { | ||
| @HttpClientRequestTests | ||
| @ProtocolTestFilter( | ||
| skipTests = { | ||
| "SDKAppliedContentEncoding_awsJson1_1", | ||
| "SDKAppendsGzipAndIgnoresHttpProvidedEncoding_awsJson1_1", | ||
| }) | ||
| public void requestTest(DataStream expected, DataStream actual) { | ||
| Node expectedNode = ObjectNode.objectNode(); | ||
| if (expected.contentLength() != 0) { | ||
| expectedNode = Node.parse(new String(ByteBufferUtils.getBytes(expected.asByteBuffer()), | ||
| StandardCharsets.UTF_8)); | ||
| } | ||
| Node actualNode = Node.parse(new StringBuildingSubscriber(actual).getResult()); | ||
| assertEquals(expectedNode, actualNode); | ||
| } | ||
|
|
||
| @HttpClientResponseTests | ||
| public void responseTest(Runnable test) { | ||
| test.run(); | ||
| } | ||
| } |
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
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
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
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
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
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
86 changes: 86 additions & 0 deletions
86
client/client-http/src/main/java/software/amazon/smithy/java/client/http/ErrorTypeUtils.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,86 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.amazon.smithy.java.client.http; | ||
|
|
||
| import software.amazon.smithy.java.core.serde.document.Document; | ||
| import software.amazon.smithy.model.shapes.ShapeType; | ||
|
|
||
| public final class ErrorTypeUtils { | ||
|
|
||
| private ErrorTypeUtils() {} | ||
|
|
||
| /** | ||
| * Read the error type from __type field of the document. | ||
| * | ||
| * @param document the document of the payload to read. | ||
| * @return the extracted error type from the document. | ||
| */ | ||
| public static String readType(Document document) { | ||
| String errorType = null; | ||
| var member = document.getMember("__type"); | ||
| if (member != null && member.type() == ShapeType.STRING) { | ||
| errorType = member.asString(); | ||
| } | ||
| return errorType; | ||
| } | ||
|
|
||
| /** | ||
| * Read the error type from __type or code field of the document. | ||
| * | ||
| * @param document the document of the payload to read. | ||
| * @return the extracted error type from the document. | ||
| */ | ||
| public static String readTypeAndCode(Document document) { | ||
| String errorType = readType(document); | ||
| if (errorType == null) { | ||
| var member = document.getMember("code"); | ||
| if (member != null && member.type() == ShapeType.STRING) { | ||
| errorType = member.asString(); | ||
| } | ||
| } | ||
| return errorType; | ||
| } | ||
|
|
||
| /** | ||
| * Removes the trailing URI in {@code __type} field or {@code code} field of the document. | ||
| * | ||
| * <p>For example, given {@code __type = "aws.protocoltests.restjson#FooError:http://abc.com"}, | ||
| * protocols like restJSON should ignore the trailing URI and keep the namespace of the error type. | ||
| * | ||
| * @param text The error type string. | ||
| * @return The error type string without the trailing URI. | ||
| */ | ||
| public static String removeUri(String text) { | ||
| if (text == null) { | ||
| return null; | ||
| } | ||
| var colon = text.indexOf(':'); | ||
| if (colon > 0) { | ||
| text = text.substring(0, colon); | ||
| } | ||
| return text; | ||
| } | ||
|
|
||
| /** | ||
| * Removes the namespace and trailing URI in {@code __type} field or {@code code} field of the document. | ||
| * | ||
| * <p>For example, given {@code __type = "aws.protocoltests.restjson#FooError:http://abc.com"}, | ||
| * protocols like awsJSON 1.1 should ignore the namespace and the trailing URI of the error type. | ||
| * | ||
| * @param text The error type string. | ||
| * @return The error type string without the trailing URI. | ||
| */ | ||
| public static String removeNamespaceAndUri(String text) { | ||
| if (text == null) { | ||
| return null; | ||
| } | ||
| var hash = text.indexOf('#'); | ||
| if (hash > 0) { | ||
| text = text.substring(hash + 1); | ||
| } | ||
| return removeUri(text); | ||
| } | ||
| } |
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
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.