gibahjoe / openapi-generator-dart

Openapi Generator for Dart/Flutter
BSD 3-Clause "New" or "Revised" License
111 stars 29 forks source link

Not required not nullable fields should not be generated as null in JSON #134

Open tototo23 opened 4 months ago

tototo23 commented 4 months ago

Description of the bug

In my OpenAPI yaml file i've been given, a schema describes :

    UpdateUser:
      type: object
      required: []
      properties:
        name:
          type: string
          example: testName
          nullable: false
        (...)

Meaning that : none of my fields are required, and non of the fields in the JSON of the request can be null. In other words, only fields that are set will be in the json, the unset fields will not be in the JSON object.

But using this yaml file, with Generator.dart, i'm given :

  Map<String, dynamic> toJson() {
    final json = <String, dynamic>{};
    if (this.name != null) {
      json[r'name'] = this.name;
    } else {
      json[r'name'] = null;
    }

I'm suposed to not have the else section, if the field this.nameis null, because it's not nullable (in JSON side) and not required.

Steps to reproduce

Use an openAPI yaml file with the above case, example :

openapi: 3.0.0
paths:
  "/user/{userID}":
    patch:
      parameters:
        - name: userID
          required: true
          in: path
          schema:
            format: int32
            type: integer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/UpdateUser"
components:
  schemas:
    UpdateUser:
      type: object
      required: []
      properties:
        name:
          type: string
          example: testName
          nullable: false

Generate the API code with this yaml file withthe command flutter pub run build_runner build --delete-conflicting-outputs, for example having somewhere :

@Openapi(
    additionalProperties: AdditionalProperties(pubName: 'api'),
    inputSpec: InputSpec(path: 'spec.yml'),
    generatorName: Generator.dart)

Expected behavior

If 'name' is null in my dart side, it should mean it is not set. And as this fields is not required and not nullable, the JSON result should not get "else ..=null", but just :

  Map<String, dynamic> toJson() {
    final json = <String, dynamic>{};
    if (this.name != null) {
      json[r'name'] = this.name;
    }

Logs

No response

Screenshots

No response

Platform

Windows

Library version

5.0.2

Flutter version

3.16.9

Flutter channel

stable

Additional context

Maybe my understanding of OpenAPI is not good and the yaml file it wrong itself to describe the case of "only the fields set should be set in the resulting JSON", in this case, any help would be welcome :)
alexpyc commented 1 month ago

Hi @tototo23 , I am encountering the same issue. I tried digging into the code of this library. I found that this library is largely a wrapper for https://github.com/OpenAPITools/openapi-generator

So I opened this issue to notify that repository of this issue