OpenAPITools / OpenAPITools/openapi-generator

[BUG] [DART] [6.0.1] Strange generation of model classes with optional and/or nullable array and object properties

Open
#13,320 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The inability of the dart generator to handle model-class generation properly with respect to required, nullable object and array properties has forced us to stick to version 6.0.0 which has a behavior that does not cause as much trouble for us, while not really optimal either.

Our main problem is that the server we communicate with using our generated Dart client does not accept null for optional properties. The value should be non-null or just not present. Also, that optional array properties do not generate into nullable lists forces us to declare these arrays as nullable just to be able to omit them (works when using 6.0.0, but breaks for us in 6.0.1).

openapi-generator version

Using 6.0.1.

6.0.0 had a different behavior in the JSON-generation which does not break our project in the same way.

OpenAPI declaration file content or url
component:
  schemas:
    MySchema:
      type: object
      required:
        - object1
        - array1
      properties:
        object1:
          $ref: "#/components/schemas/MyOtherSchema"
        object2:
          $ref: "#/components/schemas/MyOtherSchema"
        object3:
          nullable: true
          $ref: "#/components/schemas/MyOtherSchema"
        array1:
          type: array
          items:
            type: string
        array2:
          type: array
          items:
            type: string
        array3:
          nullable: true
          type: array
          items:
            type: string

    MyOtherSchema:
      type: object
      properties:
        a:
          type: number
        b:
          type: number
Generation Details

Run i shell script using (actual values omitted):

npm exec -y -- @openapitools/openapi-generator-cli generate \
-i <our-spec>.yaml \
-g dart \
-c <our-config>.json \
--additional-properties=pubVersion=<our version> \
-o <our output path>

Our config (actual values omitted):

{
  "enablePostProcessFile": true,
  "pubAuthor": "...",
  "pubAuthorEmail": "...",
  "pubDescription": "...",
  "pubHomepage": "...",
  "pubName": "..."
}

Our openapitools.json:

{
  "$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "6.0.1"
  }
}
Steps to reproduce
  1. Generate Dart clients using provided details.
  2. Inspect generated model class for MySchema
  3. Observe comments and declarations for generated class attributes and the toJson() method.

Problems identified:

  1. The documentation of object2 and object3 states that they "should have been non-nullable!".
    This seems incorrect.
    • object2 is not among the required properties so there must be some value to omit it. null seems reasonable.
    • object3 is declared as nullable so indeed it should be a nullable type declaration.
  2. array2 is not given a nullable type declaration. It is not among the required properties so there should be a way
    to omit it, using a nullable type. (array1 is correctly a non-nullable array, since it is a among the required
    properties.
  3. The value null is sent for object2 if it is null. This seems incorrect. Since it is not among the required
    properties, omitting it should not mean null. It should just not be present in the generated JSON.
    object3 is nullable, so a value of null is valid to present in the generated JSON.
Related issues/PRs

None found.

Suggest a fix
  • Non-required properties should always generate nullable class attributes (including arrays) so that they can really be omitted.
  • Non-required properties that are "omitted" by setting them to null should by default not be generated as null values in JSON.
  • Properties that are declared as nullable and set to null in the Dart model object are just fine to generate as null values in JSON. Since null is a valid value to pass in the JSON, it cannot be omitted as should be the case in the non-required, non-nullable case.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided OpenAPI YAML and run the Dart generation command using version 6.0.1. Inspect the generated MySchema model comments, declarations, and toJson() output for object2, object3, array2, and array3. Done means optional properties can be omitted, nullable properties serialize null when set, and required properties retain their required behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.