aws-amplify / aws-amplify/amplify-codegen

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

Open
#790 3 comments 0 reactions 0 assignees View on GitHub
bug Gen 2 transferred
Dominant language
TypeScript
Stars
59
Forks
64
PR merge metrics
No merged PRs in 30d

Description

### 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
```bash
npx @aws-amplify/cli codegen models --model-schema --target flutter --output-dir ./lib/models
```
3. Check the generated file.

Following lines will fail:

```dart
@Deprecated('[getId] is being deprecated in favor of custom primary key feature. Use getter [modelIdentifier] to get model identifier.')
@override
String getId() => id;
```

```dart
return ModelName._internal(
id: id == null ? amplify_core.UUID.getUUID() : id,
```

```dart
ModelName.fromJson(Map 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

```GraphQL
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")
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.