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

Gen 2 codegen generates wrong getters for "id" field in a model

Open salihgueler opened this issue 11 months ago • 3 comments

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

  1. Create a schema and a model with a field called "id" with type int.
  2. Run the following command with proper fields
npx @aws-amplify/cli codegen models   --model-schema <schema-path>   --target flutter   --output-dir ./lib/models
  1. 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")
}

salihgueler avatar Feb 26 '24 20:02 salihgueler

Hey @salihgueler, thanks for finding this. I've transferred your issue to the Amplify CLI team for better support.

Equartey avatar Feb 27 '24 15:02 Equartey

apologies on this additional transfer, the API repository maybe the appropriate place for better assistance 🙂

ykethan avatar Feb 27 '24 16:02 ykethan

I was able to reproduce this issue and I marking this as a bug.

dpilch avatar Feb 27 '24 18:02 dpilch