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 array #8937

Closed
marcoboerner opened this issue Sep 21, 2024 · 3 comments
Assignees
Labels
bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made. p2 This is a standard priority issue

Comments

@marcoboerner
Copy link

marcoboerner commented Sep 21, 2024

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.

Possible Solution

No response

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()]),

CLI version used

aws-cli/2.17.56 Python/3.12.6 Darwin/24.1.0 source/arm64

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 bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 21, 2024
@tim-finnigan tim-finnigan self-assigned this Sep 23, 2024
@tim-finnigan
Copy link
Contributor

Thanks for reaching out. I think this was intended for an AWS Amplify repository, such as amplify-cli or amplify-swift.

This repository is for the AWS CLI — here is the command reference documentation: https://awscli.amazonaws.com/v2/documentation/api/latest/index.html

@tim-finnigan tim-finnigan added closing-soon This issue will automatically close in 4 days unless further comments are made. p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Sep 23, 2024
@marcoboerner
Copy link
Author

@tim-finnigan thanks for the info! Will close this issue.

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

2 participants