OpenAPITools / OpenAPITools/openapi-generator
[BUG] Dart includeIfNull set to false even if property is required.
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- [ X] Have you provided a full/minimal spec to reproduce the issue?
- [ X] Have you validated the input using an OpenAPI validator?
- [ X] Have you tested with the latest master to confirm the issue still exists?
- [ X] Have you searched for related issues/PRs?
- [X ] What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Right now the dart version with the following configuration:
generatorName: dart-dio
additionalProperties:
pubName: api
pubVersion: 0.0.1
pubDescription: "API client for Dart"
serializationLibrary: json_serializable
enablePostProcessFile: true
enumPropertyNaming: original
useEnumExtension: true
useSingleRequestParameter: true
withSeparateModelsAndApi: true
modelPropertyNaming: original
supportNullable: true # Ensure OpenAPI nullable rules are respected
legacyDiscriminatorBehavior: false
strictNullChecks: true
nullableReferenceTypes: true # If using C# generation
includeIfNull on properties is set to null even if the required flag is true on the property. This is invalid, because if the property is required, even if nullable, then includeIfNull should be true.
openapi-generator version
7.12.0 - Always had this issue
OpenAPI declaration file content or url
"ContactRefDto": {
"type": "object",
"description": "Contact Reference",
"additionalProperties": false,
"required": [
"id",
"name",
"avatarUrl",
"contactType"
],
"properties": {
"id": {
"type": "string",
"description": "Id",
"format": "uuid",
"nullable": false
},
"name": {
"type": "string",
"description": "Name",
"nullable": false
},
"avatarUrl": {
"type": "string",
"description": "Avatar Url",
"nullable": true
},
"contactType": {
"description": "Contact Type",
"nullable": false,
"$ref": "#/components/schemas/ContactTypes"
}
}
},
results in:
@JsonSerializable(
checked: true,
createToJson: true,
disallowUnrecognizedKeys: false,
explicitToJson: true,
)
class ContactRefDto {
/// Returns a new [ContactRefDto] instance.
ContactRefDto({
required this.id,
required this.name,
required this.avatarUrl,
required this.contactType,
});
/// Id
@JsonKey(
name: r'id',
required: true,
includeIfNull: false,
)
final String id;
/// Name
@JsonKey(
name: r'name',
required: true,
includeIfNull: false,
)
final String name;
/// Avatar Url
@JsonKey(
name: r'avatarUrl',
required: true,
includeIfNull: true,
)
final String? avatarUrl;
@JsonKey(
name: r'contactType',
required: true,
includeIfNull: false,
)
final ContactTypes contactType;
@override
bool operator ==(Object other) => identical(this, other) || other is ContactRefDto &&
other.id == id &&
other.name == name &&
other.avatarUrl == avatarUrl &&
other.contactType == contactType;
@override
int get hashCode =>
id.hashCode +
name.hashCode +
(avatarUrl == null ? 0 : avatarUrl.hashCode) +
contactType.hashCode;
factory ContactRefDto.fromJson(Map<String, dynamic> json) => _$ContactRefDtoFromJson(json);
Map<String, dynamic> toJson() => _$ContactRefDtoToJson(this);
@override
String toString() {
return toJson().toString();
}
}
Generation Details
"@openapitools/openapi-generator-cli",
"generate",
"-i",
"http://localhost:16049/help/v1/openapi.json",
"-g",
"dart-dio",
"-c",
"open-generator-config.yaml",
"--enable-post-process-file"
Steps to reproduce
Generate using the snippet provided that shows the required fields on the object. includeIfNull will be false for all of the nullable: true fields, but because they're required, it should be includeIfNull: true
Related issues/PRs
There are several PRs to add the includeIfNull functionality but nothing that fixes this or reports it that I can find.
Suggest a fix
Check the required fields on the object, and if field exists in there, then includeIfNull should be set to true.
In addition, if nullable is false, it's still creating nullable values if required = false. It should be creating non-nullable fields. This scenario in backends means that you don't have to pass it, it will be set to a default value if you don't, but the property if you do pass it is not nullable.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the dart-dio generator entry point and reproduce the issue using the supplied ContactRefDto schema and configuration. Inspect how required and nullable properties produce Dart @JsonKey annotations; done means required properties emit includeIfNull: true while preserving the expected nullable behavior for optional properties.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100