amplify-codegen icon indicating copy to clipboard operation
amplify-codegen copied to clipboard

Swift modelgen creates wrong decoding initializer with optional custom type in required arra

Open marcoboerner opened this issue 1 year ago • 1 comments

Environment information

System:
  OS: macOS 15.1
  CPU: (12) arm64 Apple M3 Pro
  Memory: 123.80 MB / 36.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 22.9.0 - /opt/homebrew/bin/node
  Yarn: undefined - undefined
  npm: 10.8.3 - /opt/homebrew/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.0
  @aws-amplify/backend: 1.2.1
  @aws-amplify/backend-auth: 1.1.4
  @aws-amplify/backend-cli: 1.2.6
  @aws-amplify/backend-data: 1.1.3
  @aws-amplify/backend-deployer: 1.1.2
  @aws-amplify/backend-function: 1.4.0
  @aws-amplify/backend-output-schemas: 1.2.0
  @aws-amplify/backend-output-storage: 1.1.1
  @aws-amplify/backend-secret: 1.1.1
  @aws-amplify/backend-storage: 1.1.2
  @aws-amplify/cli-core: 1.1.2
  @aws-amplify/client-config: 1.3.0
  @aws-amplify/deployed-backend-client: 1.4.0
  @aws-amplify/form-generator: 1.0.1
  @aws-amplify/model-generator: 1.0.6
  @aws-amplify/platform-core: 1.1.0
  @aws-amplify/plugin-types: 1.2.1
  @aws-amplify/sandbox: 1.2.1
  @aws-amplify/schema-generator: 1.2.2
  aws-amplify: 6.6.0
  aws-cdk: 2.158.0
  aws-cdk-lib: 2.158.0
  typescript: 5.6.2
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Describe the bug

When running npx ampx generate graphql-client-code --format modelgen --model-target swift on my Amplify Swift project, depending on the models defined in data/resource.ts the decoding initializer does not compile due to a Cannot assign value of type '[MyCustomType]?' to type '[MyCustomType?] error in Xcode.

Expected Behavior

myCustomTypes = try values.decode([MyCustomType?].self, forKey: .myCustomTypes) should be generated

Current Behavior

myCustomTypes = try? values.decode([MyCustomType].self, forKey: .myCustomTypes) is generated

Reproduction steps

Define the following models in data/resource.ts:

// Custom types

MyCustomType: a.customType({
  context: a.string(),
}),

// Models

MyModel: a.model({
  context: a.string(),
  myCustomTypes: a.ref('MyCustomType').array().required(),
  myOtherModelId: a.id().required(),
  myOtherModel: a.belongsTo('MyOtherModel', 'myOtherModelId')
})
  .authorization(allow => [allow.owner()]),

MyOtherModel: a.model({
  context: a.string(),
  myModel: a.hasOne('MyModel', 'myOtherModelId')
})
  .authorization(allow => [allow.owner()]),

And run npx ampx generate graphql-client-code --format modelgen --model-target swift to create the models.

Additional Information/Context

When changing the custom type in the array to a non-optional, the decoding is done correctly.

// Custom types

MyCustomType: a.customType({
  context: a.string(),
}),

// Models

MyModel: a.model({
  context: a.string(),
  myCustomTypes: a.ref('MyCustomType').required().array().required(), // <-- changed !!!
  myOtherModelId: a.id().required(),
  myOtherModel: a.belongsTo('MyOtherModel', 'myOtherModelId')
})
  .authorization(allow => [allow.owner()]),

MyOtherModel: a.model({
  context: a.string(),
  myModel: a.hasOne('MyModel', 'myOtherModelId')
})
  .authorization(allow => [allow.owner()]),

Environment details (OS name and version, etc.)

macOS 15.1, Xcode 16.1 beta 2, swift-tools-version: 6.0, swiftLanguageModes v5

marcoboerner avatar Sep 23 '24 17:09 marcoboerner

Hey, thanks for raising this! I'm going to transfer this over to our codegen repository for better assistance.

ykethan avatar Sep 23 '24 19:09 ykethan