Skip to content

[BUG] Dart includeIfNull set to false even if property is required. #21508

Description

@JohnGalt1717

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions