Skip to content

Fix trait codegen for structure's string member with @idRef#2728

Merged
joewyz merged 1 commit into
mainfrom
joewyz/fix-codegen-for-structure-member-with-idref
Aug 1, 2025
Merged

Fix trait codegen for structure's string member with @idRef#2728
joewyz merged 1 commit into
mainfrom
joewyz/fix-codegen-for-structure-member-with-idref

Conversation

@joewyz
Copy link
Copy Markdown
Contributor

@joewyz joewyz commented Jul 31, 2025

Background

String shapes with @idRef are converted into ShapeId objects during the trait codegen. However, the generated fromNode() method and Builder setters for string members with @idRef within structures are incorrect.
Because the @idReft info is binding with the member shape and those incorrect methods are generated based on member shape's targets.

For example the following smithy model:

$version: "2.0"

namespace test.smithy.traitcodegen.structures

@trait
structure StructWithIdrefMember {
    @idRef(selector: "structure")
    idRefMemberA: String
    idRefMemberB: IdRefMemberB
}

@idRef(selector: "structure")
string IdRefMemberB

The generated fromNode() and builder's setter methods are incorrect.

@SmithyGenerated
public final class StructWithIdrefMemberTrait extends AbstractTrait implements ToSmithyBuilder<StructWithIdrefMemberTrait> {
    public static final ShapeId ID = ShapeId.from("example.traits#StructWithIdrefMember");

    private final ShapeId idRefMemberA;
    private final ShapeId idRefMemberB;
    /**
     * Creates a {@link StructWithIdrefMemberTrait} from a {@link Node}.
     *
     * @param node Node to create the StructWithIdrefMemberTrait from.
     * @return Returns the created StructWithIdrefMemberTrait.
     * @throws ExpectationNotMetException if the given Node is invalid.
     */
    public static StructWithIdrefMemberTrait fromNode(Node node) {
        Builder builder = builder();
        node.expectObjectNode()
            .getStringMember("idRefMemberA", builder::idRefMemberA)
            .getMember("idRefMemberB", n -> ShapeId.fromNode(n), builder::idRefMemberB);
        return builder.build();
    }
    
    public static final class Builder extends AbstractTraitBuilder<StructWithIdrefMemberTrait, Builder> {
        private ShapeId idRefMemberA;
        private ShapeId idRefMemberB;

        private Builder() {}

        public Builder idRefMemberA(String idRefMemberA) {
            this.idRefMemberA = idRefMemberA;
            return this;
        }
    }
}

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

@joewyz joewyz requested a review from a team as a code owner July 31, 2025 21:27
@joewyz joewyz requested a review from kstich July 31, 2025 21:27
@joewyz joewyz merged commit 8159e00 into main Aug 1, 2025
9 checks passed
@joewyz joewyz deleted the joewyz/fix-codegen-for-structure-member-with-idref branch August 1, 2025 16:36
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.

2 participants