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

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

Open
marcoboerner opened this issue Sep 23, 2024 · 1 comment
Labels
bug Something isn't working Gen 2 transferred Issue has been transferred from another Amplify repository

Comments

@marcoboerner
Copy link

marcoboerner commented Sep 23, 2024

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 marcoboerner added the pending-triage Issues that need further discussion to determine label Sep 23, 2024
@ykethan
Copy link
Member

ykethan commented Sep 23, 2024

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

@ykethan ykethan transferred this issue from aws-amplify/amplify-backend Sep 23, 2024
@ykethan ykethan added transferred Issue has been transferred from another Amplify repository Gen 2 labels Sep 23, 2024
@dpilch dpilch added the bug Something isn't working label Oct 4, 2024
@dpilch dpilch removed the pending-triage Issues that need further discussion to determine label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Gen 2 transferred Issue has been transferred from another Amplify repository
Projects
None yet
Development

No branches or pull requests

3 participants