Skip to content
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

fix: Ignore httpQuery trait on response members but don't ignore member #723

Merged
merged 3 commits into from
May 15, 2024

Conversation

jbelkins
Copy link
Contributor

@jbelkins jbelkins commented May 15, 2024

Issue #

awslabs/aws-sdk-swift#1500

Description of changes

  • Generate deserialization for a response member bearing the @httpQuery trait just like for any other member
  • When comparing the result of a response protocol test, compare all members & don't ignore members with the @httpQuery trait
  • Update logic for when to create a reader in the response binding method so that a reader is only created when it is actually used. This reduces dead code and eliminates compiler warnings.

Scope

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.


import ClientRuntime

public struct EndpointParams {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated new file added by codegen changes in another PR.

@@ -7,9 +7,6 @@ import SmithyReadWrite
extension InvokeOutput {

static func httpOutput(from httpResponse: ClientRuntime.HttpResponse) async throws -> InvokeOutput {
let data = try await httpResponse.data()
let responseReader = try SmithyJSON.Reader.from(data: data)
let reader = responseReader
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This reader is not used; the improved logic in this PR does not create it.

@@ -6,6 +6,8 @@ import WeatherSDK
extension ListCitiesInput: Swift.Equatable {

public static func ==(lhs: ListCitiesInput, rhs: ListCitiesInput) -> Bool {
if lhs.nextToken != rhs.nextToken { return false }
if lhs.pageSize != rhs.pageSize { return false }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are members that have the @httpQuery trait and were previously not included in the comparison

@@ -172,7 +171,7 @@ open class HttpProtocolUnitTestResponseGenerator protected constructor(builder:
writer.openBlock("public static func ==(lhs: \$L, rhs: \$L) -> Bool {", "}", symbol.fullName, symbol.fullName) {
when (shape) {
is StructureShape -> {
shape.members().filter { !it.hasTrait<HttpQueryTrait>() }.forEach { member ->
shape.members().forEach { member ->
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Members with the @httpQuery trait are no longer excluded when comparing equality in response tests.

@@ -66,7 +65,6 @@ class HTTPResponseBindingErrorInitGenerator(
HTTPResponseHeaders(ctx, true, headerBindings, customizations.defaultTimestampFormat, writer).render()
HTTPResponsePrefixHeaders(ctx, responseBindings, writer).render()
httpResponseTraitPayload(ctx, responseBindings, structureShape, writer)
HTTPResponseTraitQueryParams(ctx, responseBindings, writer).render()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Response members bound to a @httpQuery trait no longer receive any special rendering; instead they are rendered along with the payload members.

val targetShape = ctx.model.expectShape(binding.member.target)
return binding.location == HttpBinding.Location.PAYLOAD &&
(targetShape is StructureShape || targetShape is UnionShape) &&
!targetShape.members().isEmpty()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This new logic in the needsAReader() function prevents creating unused readers (and the compiler warnings that come with them) by rendering reader creation only when a payload is a structure or union that has at least one member.

Other payloads (a string, an enum, a blob, smithy Document, etc.) do not require a reader. Also a reader is not required for a structure or union that does not have at least one member.

* SPDX-License-Identifier: Apache-2.0.
*/

package software.amazon.smithy.swift.codegen.integration.httpResponse.bindingTraits
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This used to render a statement to set a member to nil if it is in the response and has the @httpQuery trait. This was incorrect.

Instead, this renderer is removed and a member with the @httpQuery trait will be rendered to the response like any other member.

val bodyMembersWithoutQueryTrait = bodyMembers
.filter { !it.member.hasTrait(HttpQueryTrait::class.java) }
.toMutableSet()
val bodyMembers = responseBindings.filter { it.location == HttpBinding.Location.DOCUMENT }.toSet()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified this logic since there is no need to separate members with the @httpQuery trait from other members.

@@ -106,9 +106,6 @@ extension HttpResponseCodeOutput {
extension InlineDocumentAsPayloadOutput {

static func httpOutput(from httpResponse: ClientRuntime.HttpResponse) async throws -> InlineDocumentAsPayloadOutput {
let data = try await httpResponse.data()
let responseReader = try SmithyJSON.Reader.from(data: data)
let reader = responseReader
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This codegen test no longer expects an unused reader to be rendered.

@@ -16,7 +16,7 @@ import shouldSyntacticSanityCheck
class HttpResponseBindingIgnoreQuery {

@Test
fun `001 Output httpResponseBinding sets query to nil`() {
fun `001 Output httpResponseBinding ignores query trait & decodes field`() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The premise of this test is incorrect. The member with @httpQuery trait should not be set to nil; it should be read from the response.

The name of the test & the expectation are adjusted accordingly.

@jbelkins jbelkins marked this pull request as ready for review May 15, 2024 04:37
@jbelkins jbelkins requested review from sichanyoo and dayaffe May 15, 2024 04:38
@jbelkins jbelkins merged commit d3f2990 into main May 15, 2024
17 checks passed
@jbelkins jbelkins deleted the jbe/response_urlquery_fix branch May 15, 2024 15:10
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.

3 participants