Skip to content

Optional Field Incorrectly Generated for Required Parameter in Python Codegen #329

Open
@rangerthegood

Description

@rangerthegood

Steps to reproduce:

cd smithy-python/codegen
./gradlew smithy-python-codegen-test:build

This produces the file: smithy-python/codegen/smithy-python-codegen-test/build/smithyprojections/smithy-python-codegen-test/source/python-client-codegen/weather/models.py.

Looking at the GetCityInput structure you will see showing that city_id is not required because it allows None.

class GetCityInput:

    city_id: str | None = None

The smithy model for GetCityInput shows:

@http(method: "GET", uri: "/cities/{cityId}")
operation GetCity {
    input := {
        // "cityId" provides the identifier for the resource and
        // has to be marked as required.
        @required
        @httpLabel
        cityId: CityId
    }

The determination to see if a field is required is done in StructureGenerator.java.

        var required = new ArrayList<MemberShape>();
        var optional = new ArrayList<MemberShape>();
        var index = NullableIndex.of(context.model());
        for (MemberShape member: shape.members()) {
            if (index.isMemberNullable(member) || member.hasTrait(DefaultTrait.class)) {
                optional.add(member);
            } else {
                required.add(member);
            }
        }

isMemberNullable is returning True for the cityId, and therefore is marked as optional.

I'll PR adding an additional check to see if the RequiredTrait.class is set on the member.

When that is in place the structure then becomes:

class GetCityInput:

    city_id: str

Or maybe this is by design? #182

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions