OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
22.01k stars 6.6k forks source link

[BUG][DART] Non-required non-nullable field is always assigned null value if value not present #18647

Open alexpyc opened 6 months ago

alexpyc commented 6 months ago

Bug Report Checklist

Description

Dart generator always insert null for non-required non-nullable field if a value is not provided. However, it seems not the specified behaviour.

openapi-generator version

7.5.0

OpenAPI declaration file content or url
    UpdateUser:
      type: object
      required: []
      properties:
        name:
          type: string
          example: testName
          nullable: false
        (...)
Generation Details

java -jar openapi-generator-cli.jar generate -i {input filename} -g dart -o {output directory name}

Steps to reproduce

Generate output with the above code snippet

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/12569/commits/8002e558858e252d78248bd9db51a24498780360

Suggest a fix

This PR comment should point out the issue. Non-required, non-nullable field should not by default assign a null value which should be contradictory to how JSON schema treats non-required field.

Vito0912 commented 4 months ago

Same problem here

escamoteur commented 1 month ago

what does it mean if value not present it it is a non-nullable field?

IMHO the current behavior to add null values for nullable non required fields is wrong

Dreaming-Codes commented 1 week ago

This also happens with query params with dart-dio generator and json-serializable. This is my schema

{
  "schema": {
    "type": "array",
    "items": {
      "type": "object",
      "required": [
        "messageId",
        "sender",
        "message"
      ],
      "properties": {
        "message": {
          "type": "string"
        },
        "messageId": {
          "type": "integer",
          "format": "int64"
        },
        "sender": {
          "type": "string",
          "enum": [
            "User",
            "Assistant",
            "System",
            "Function"
          ]
        }
      }
    }
  }
}

When doing

await client.getDefaultApi().getMessages(chatId: 2, limit: 10, beforeMessageId: 0)

I expect something like

GET /chats/messages?chat_id=2&before_message_id=0&limit=10

but instead the generated request is:

GET /chats/messages?chat_id=2&before_message_id=0&after_message_id&limit=10