-
Notifications
You must be signed in to change notification settings - Fork 124
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
Swagger properties not marked as nullable should not generate nullable dart fields #650
Comments
Hi @Adam-Langley , we generate all properties as nullable, because often swagger file 'lies' and do not returns such properties. Also, if all properties are nullable, we can send it as we wish (we can send or not send any property). It was our decision to generate it like this. It's expected behavior. If you're sure that backend returns not nullable value - just use bang operator. Thanks for raising issue! |
I feel like it should be an option whether or not all properties should be nullable. Null safety can be found in many modern language like Kotlin, Rust or C#. Generated swagger files can be set up to respect nullability. It's a shame that it is lost in this library. Also note that it seems like current implementation will treat:
as |
@TortillaZHawaii ok fine reopening the issue. Will provide option to generate all nullable or not |
@TortillaZHawaii @Adam-Langley I've implemented not nullable fields. It works on my 50+ swaggers, but I can not guarantee that there are no border cases. Please raise issues in case you face it. Thanks! You can also use |
hello, |
@nsvetlakov on which swagger file you have error? |
this
this one unde_seller.swagger.dart |
Hi @nsvetlakov @TortillaZHawaii @Adam-Langley Let's summarize:
Example 1"MrControlDto": {
"required": [
"id"
],
"type": "object",
"properties": {
"id": {
"type": "string",
"nullable": "true"
}
},
"additionalProperties": false
} @JsonSerializable(explicitToJson: true)
class MrControlDto {
const MrControlDto({
required this.id,
});
string, dynamic> toJson() => _$MrControlDtoToJson(this);
@JsonKey(name: 'id', includeIfNull: false, defaultValue: '')
final String? id; // nullable and required
static const fromJsonFactory = _$MrControlDtoFromJson; Example 2"MrControlDto": {
"required": [
"id"
],
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"additionalProperties": false
} @JsonSerializable(explicitToJson: true)
class MrControlDto {
const MrControlDto({
required this.id,
});
@JsonKey(name: 'id', includeIfNull: false, defaultValue: '')
final String id; // not nullable, required
static const fromJsonFactory = _$MrControlDtoFromJson; Example 3"MrControlDto": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"additionalProperties": false
} @JsonSerializable(explicitToJson: true)
class MrControlDto {
const MrControlDto({
this.id,
});
factory MrControlDto.fromJson(Map<String, dynamic> json) =>
_$MrControlDtoFromJson(json);
static const toJsonFactory = _$MrControlDtoToJson;
Map<String, dynamic> toJson() => _$MrControlDtoToJson(this);
@JsonKey(name: 'id', includeIfNull: false, defaultValue: '')
final String? id; // nullable not required That's how it should work. If something broken - please raise new issue. This one will be closed by my next PR |
Describe the bug
Model properties marked as nullable, are producing nullable dart properties.
It appears the generator is relying solely on the 'required' list to determine nullability.
To Reproduce
Please attach swagger and code snippet of generated value
Expected behavior
In the above dart example, the 'hazardId' field should be of type 'UuidValue', not 'UuidValue?' because it is not nullable in the respective swagger.
Swagger specification link
Link to swagger/OpenApi documentation
Library version used:
2.11.10
Additional context
The differences between 'required' and 'nullable' are explained fairly well here:
https://stackoverflow.com/questions/45575493/what-does-required-in-openapi-really-mean
With the current implementation, it is possible for the JsonSerializable code to generate non-compliant json (i.e, with null assigned to fields that do not allow null)
The text was updated successfully, but these errors were encountered: