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
[codegen models] add 'required' modifier in constructors when a field is non-nullable #665
Comments
Hey @dJani97, sorry you're having this issue. Codegen should indeed mark that field as required. I tried generating a simple model using the same versions as you and codegen marked it required as expected. Can you provide the schema you're working with? |
Hi @dJani97 The codegen generated code may unnecessarily conform all linter rules as the comment inserted in the generated file:
We recommend you to do so to avoid linter noise. |
Hey @dJani97, are you still facing this issue? If so can you please provide your schema? And are you using any feature flags with Amplify CLI? |
@HuiSF
@Equartey type SomeModel {
id: String
name: String
} generates the following model: @immutable
class SomeModel {
final String id;
final String? _name;
String? get name {
return _name;
}
const SomeModel._internal({this.id, name}): _name = name;
// ...
} Even though both fields are using the Now I understand that this could be fixed by updating the model like so: type SomeModel {
id: String!
name: String
} ... but still, this behavior is confusing and if amplify will make all Edit: I consulted with backend, and they say that this field IS actually nullable on their side. So this is why they added it to the schema this way. Why does Amplify CLI make this decision for us, and treat all |
@dJani97 - I am not able to reproduce this. The following schema: type SomeModel @model {
id: String
name: String
} produces the following generated code: class SomeModel extends Model {
static const classType = const _SomeModelModelType();
final String id;
final String? _name;
@override
getInstanceType() => classType;
@Deprecated('[getId] is being deprecated in favor of custom primary key feature. Use getter [modelIdentifier] to get model identifier.')
@override
String getId() => id;
SomeModelModelIdentifier get modelIdentifier {
return SomeModelModelIdentifier(
id: id
);
}
String? get name {
return _name;
}
const SomeModel._internal({required this.id, name}): _name = name;
// rest of class definition
} Can you provide the output of running |
Can you also provide the model definition of the model you are having this issue with? I know you provided an example model definition, but seeing the actual definition might help us reproduce this and understand the use case better. Thanks. |
Before writing the previous comment, I added this exact I noticed that you're using the |
I see. I am able to reproduce that. It seems that codegen assumes that if there is an Possible workarounds:
Reproduction steps:
type MyModel @model {
id: ID!
name: String
someModel: MyType
}
type MyType {
id: String
name: String
}
|
Description
The models generated by
amplify codegen models
won't compile because of the following error:The generated field definition looks like this:
And the generated constructor looks like this:
Since this field is non-nullable, the constructor should be generated like so:
This causes us some overhead as we have to update the generated files manually each time.
We're on CLI version
12.0.3
and Flutter version3.10.2
.Categories
Steps to Reproduce
Have a model with a non-nullable field.
Run
amplify codegen models
.Try compiling they project with the latest version of Flutter.
Screenshots
No response
Platforms
Flutter Version
3.10.2
Amplify Flutter Version
1.1.0
Deployment Method
Amplify CLI
Schema
No response
The text was updated successfully, but these errors were encountered: