amplify-codegen
amplify-codegen copied to clipboard
Gen 2 codegen generates wrong getters for "id" field in a model
Description
In a scenario that we have a CDK backend with a new L3 construct, the new Amplify codegen does not generate the "id" property properly from a schema file. When the id is an integer it assumes it is a String and the generated code is broken.
Categories
- [ ] Analytics
- [ ] API (REST)
- [X] API (GraphQL)
- [ ] Auth
- [ ] Authenticator
- [ ] DataStore
- [ ] Notifications (Push)
- [ ] Storage
Steps to Reproduce
- Create a schema and a model with a field called "id" with type int.
- Run the following command with proper fields
npx @aws-amplify/cli codegen models --model-schema <schema-path> --target flutter --output-dir ./lib/models
- Check the generated file.
Following lines will fail:
@Deprecated('[getId] is being deprecated in favor of custom primary key feature. Use getter [modelIdentifier] to get model identifier.')
@override
String getId() => id;
return ModelName._internal(
id: id == null ? amplify_core.UUID.getUUID() : id,
ModelName.fromJson(Map<String, dynamic> json)
: id = (json['id'] as num?)?.toInt(),
Screenshots
No response
Platforms
- [ ] iOS
- [ ] Android
- [ ] Web
- [ ] macOS
- [ ] Windows
- [ ] Linux
Flutter Version
3.16.9
Amplify Flutter Version
1.6.1
Deployment Method
Amplify CLI
Schema
type Puzzle
@model
@auth(
rules: [{ allow: public, operations: [read, update] }, { allow: owner }]
) {
id: Int! @primaryKey
question: String!
solution: Solution @hasOne(references: ["puzzleId"])
submissions: [Submission!]! @hasMany(references: ["puzzleId"])
votes: [String!]!
createdAt: AWSDate @refersTo(name: "created_at")
}
type Solution
@model
@auth(rules: [{ allow: public, operations: [read] }, { allow: owner }]) {
id: Int! @primaryKey
solutionText: String! @refersTo(name: "solution_text")
language: String!
createdAt: AWSDate @refersTo(name: "created_at")
puzzleId: String @refersTo(name: "puzzle_id")
}
type Submission
@model
@auth(
rules: [{ allow: public, operations: [read, update] }, { allow: owner }]
) {
id: Int! @primaryKey
votes: [String!]!
createdAt: AWSDate @refersTo(name: "created_at")
puzzleId: String! @refersTo(name: "puzzle_id")
}
Hey @salihgueler, thanks for finding this. I've transferred your issue to the Amplify CLI team for better support.
apologies on this additional transfer, the API repository maybe the appropriate place for better assistance 🙂
I was able to reproduce this issue and I marking this as a bug.