Skip to content

Conversation

@0marperez
Copy link
Contributor

Issue #

closes #1128

Description of changes

SerializeStructGenerator now knows when a structure is nested or not so it can process the @idempotencyToken trait correctly

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

@github-actions

This comment has been minimized.

@0marperez 0marperez marked this pull request as ready for review February 25, 2025 15:06
@0marperez 0marperez requested a review from a team as a code owner February 25, 2025 15:06
val shape = ctx.model.expectShape(op.input.get())
writer.write("val serializer = #T()", RuntimeTypes.Serde.SerdeFormUrl.FormUrlSerializer)
renderSerializerBody(ctx, shape, documentMembers, writer)
renderSerializerBody(ctx, shape, documentMembers, writer, true)
Copy link
Contributor

Choose a reason for hiding this comment

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

Style: Suggest adding the parameter name to boolean args when their meaning is unclear at the call site:

renderSerializerBody(ctx, shape, documentMembers, writer, idempotencyTokenEligible = true)

Comment on lines 65 to 103
private val model = """
${"$"}version: "2"
namespace com.test
use aws.protocols#restJson1
use aws.api#service
@restJson1
@service(sdkId: "Example")
service Example {
version: "1.0.0",
operations: [GetBarUnNested, GetBarNested]
}
@http(method: "POST", uri: "/get-bar-un-nested")
operation GetBarUnNested {
input: BarUnNested
}
structure BarUnNested {
@idempotencyToken
bar: String
}
@http(method: "POST", uri: "/get-bar-nested")
operation GetBarNested {
input: BarNested
}
structure BarNested {
bar: Nest
}
structure Nest {
@idempotencyToken
baz: String
}
""".toSmithyModel()
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Incorrect indentation. Applies to other multiline strings in this PR.

Comment on lines 617 to 618
private fun idempotencyTokenPostfix(memberShape: MemberShape): String =
if (memberShape.hasTrait<IdempotencyTokenTrait>()) {
if (memberShape.hasTrait<IdempotencyTokenTrait>() && idempotencyTokenEligible) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: I can see that this works but the connection between idempotency member eligibility and being a top-level input structure member is not at all clear in the implementation. Did you investigate trying to derive whether the shape is a top-level input field directly rather than passing around a flag?

I'm wondering about something like a new knowledge index:

class TopLevelIndex(model: Model, service: ServiceShape) {
    private val operations = TopDownIndex(ctx.model).getContainedOperations(ctx.service)
    private val inputStructs = operations.mapNotNull { it.input.getOrNull() }.map { ctx.model.expectShape(it) }
    private val inputMembers = inputStructs.flatMap { it.members() }.toSet()

    fun isTopLevelInputMember(member: Shape): Boolean = member in inputMembers
}

The question would be where to store the instance such that we're not recalculating it over and over again...

Comment on lines 617 to 618
if (memberShape.hasTrait<IdempotencyTokenTrait>()) {
if (memberShape.hasTrait<IdempotencyTokenTrait>() && idempotencyTokenEligible) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: I suggest logging an INFO or WARN about shapes with the @idempotencyToken trait which are not top-level input members and are therefore being ignored.

@github-actions
Copy link

Affected Artifacts

No artifacts changed size

private fun idempotencyTokenPostfix(memberShape: MemberShape): String =
if (memberShape.hasTrait<IdempotencyTokenTrait>()) {
// https://github.com/smithy-lang/smithy-kotlin/issues/1128
if (topLevelIndex.isTopLevelInputMember(memberShape)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, this is perfect

@0marperez 0marperez merged commit aae827c into main Feb 27, 2025
16 checks passed
@0marperez 0marperez deleted the fix-idempotency branch February 27, 2025 16:07
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.

Idempotency tokens should be ignored when applied to a nested shape

3 participants