Skip to content
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

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

Open
tototo23 opened this issue Feb 19, 2024 · 1 comment
Open
Labels
bug Something isn't working

Comments

@tototo23
Copy link

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 :)
@tototo23 tototo23 added the bug Something isn't working label Feb 19, 2024
@alexpyc
Copy link

alexpyc commented May 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants