-
Notifications
You must be signed in to change notification settings - Fork 33
Attach response metadata to operation outputs and errors, escaping collisions #794
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
Open
ubaskota
wants to merge
1
commit into
smithy-lang:develop
Choose a base branch
from
ubaskota:response_metadata_impl
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,6 +108,76 @@ public void testOperationNameCollidingWithClientMethodIsEscaped() { | |
| .getName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResponseMetadataIsEscapedOnOutputsAndErrors() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also add a small codegen assertion for this too? This covers the symbol provider name change but an integration assertion could verify that a generated output shape contains both the escaped modeled member and the SDK reserved |
||
| Model model = loadModel(RESPONSE_METADATA_MODEL); | ||
| PythonSymbolProvider provider = createProvider(model); | ||
|
|
||
| assertEquals("response_metadata_", memberName(provider, model, "GetThingOutput$responseMetadata")); | ||
| assertEquals("response_metadata_", memberName(provider, model, "ThingError$responseMetadata")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResponseMetadataIsNotEscapedOnOtherShapes() { | ||
| // Only outputs and errors are given the attribute, so members elsewhere | ||
| // must keep their natural name. | ||
| Model model = loadModel(RESPONSE_METADATA_MODEL); | ||
| PythonSymbolProvider provider = createProvider(model); | ||
|
|
||
| assertEquals("response_metadata", memberName(provider, model, "GetThingInput$responseMetadata")); | ||
| assertEquals("response_metadata", memberName(provider, model, "Nested$responseMetadata")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnrelatedMembersOnOutputsAndErrorsAreUnaffected() { | ||
| Model model = loadModel(RESPONSE_METADATA_MODEL); | ||
| PythonSymbolProvider provider = createProvider(model); | ||
|
|
||
| assertEquals("thing_arn", memberName(provider, model, "GetThingOutput$thingArn")); | ||
| assertEquals("retry_after", memberName(provider, model, "ThingError$retryAfter")); | ||
| } | ||
|
|
||
| private static final String RESPONSE_METADATA_MODEL = """ | ||
| $version: "2" | ||
| namespace smithy.example | ||
|
|
||
| service TestService { | ||
| version: "2024-01-01" | ||
| operations: [GetThing] | ||
| errors: [ThingError] | ||
| } | ||
|
|
||
| operation GetThing { | ||
| input: GetThingInput | ||
| output: GetThingOutput | ||
| } | ||
|
|
||
| structure GetThingInput { | ||
| responseMetadata: String | ||
| } | ||
|
|
||
| structure GetThingOutput { | ||
| responseMetadata: String | ||
| thingArn: String | ||
| nested: Nested | ||
| } | ||
|
|
||
| structure Nested { | ||
| responseMetadata: String | ||
| } | ||
|
|
||
| @error("client") | ||
| structure ThingError { | ||
| responseMetadata: String | ||
| retryAfter: String | ||
| } | ||
| """; | ||
|
|
||
| private static String memberName(PythonSymbolProvider provider, Model model, String relativeId) { | ||
| var member = model.expectShape(ShapeId.from(NS + "#" + relativeId), MemberShape.class); | ||
| return provider.toMemberName(member); | ||
| } | ||
|
|
||
| private static Model loadModel(String smithyIdl) { | ||
| return Model.assembler().addUnparsedModel("test.smithy", smithyIdl).assemble().unwrap(); | ||
| } | ||
|
|
||
4 changes: 4 additions & 0 deletions
4
...-core/.changes/next-release/smithy-aws-core-feature-70d7c47f420d4c4b8e5bfca35eb43d1e.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,4 @@ | ||
| { | ||
| "type": "feature", | ||
| "description": "AWS protocols now report request identifiers on operation outputs and errors, read from the `x-amzn-requestid`, `x-amz-request-id`, and `x-amz-id-2` headers. awsQuery reads them from the response body." | ||
| } |
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
20 changes: 20 additions & 0 deletions
20
packages/smithy-aws-core/src/smithy_aws_core/_private/query/_xml.py
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,20 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Helpers for reading awsQuery response bodies that are not shape-modeled.""" | ||
|
|
||
| from xml.etree.ElementTree import Element | ||
|
|
||
|
|
||
| def local_name(tag: str) -> str: | ||
| """Strip namespace URI from an element tag: {uri}local -> local.""" | ||
| if tag.startswith("{"): | ||
| return tag.split("}", 1)[1] | ||
| return tag | ||
|
|
||
|
|
||
| def find_child(element: Element, name: str) -> Element | None: | ||
| """Return the first child element whose local name matches ``name``.""" | ||
| for child in element: | ||
| if local_name(child.tag) == name: | ||
| return child | ||
| return None |
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
26 changes: 26 additions & 0 deletions
26
packages/smithy-aws-core/src/smithy_aws_core/_private/query/metadata.py
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,26 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from xml.etree.ElementTree import ParseError, fromstring | ||
|
|
||
| from ._xml import find_child | ||
|
|
||
|
|
||
| def parse_aws_query_request_id(body: bytes) -> str | None: | ||
| """Parse the request ID from an awsQuery response body. | ||
|
|
||
| There is no request ID header. Successes nest it under ``ResponseMetadata``, | ||
| errors put it directly under the root. | ||
| """ | ||
| try: | ||
| root = fromstring(body) # noqa: S314 | ||
| except ParseError: | ||
| return None | ||
|
|
||
| metadata = find_child(root, "ResponseMetadata") | ||
| if metadata is not None: | ||
| nested = find_child(metadata, "RequestId") | ||
| if nested is not None and nested.text: | ||
| return nested.text | ||
|
|
||
| direct = find_child(root, "RequestId") | ||
| return direct.text or None if direct is not None else None |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could we make this a little more explicit that the generated Python name is being escaped because
response_metadatais reserved for SDK response metadata?