OpenAPITools / OpenAPITools/openapi-generator
[BUG] [DART] [6.0.1] Strange generation of model classes with optional and/or nullable array and object properties
Nobody has claimed this yet.
- 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
- Generate Dart clients using provided details.
- Inspect generated model class for
MySchema - Observe comments and declarations for generated class attributes and the
toJson()method.
Problems identified:
- The documentation of
object2andobject3states that they "should have been non-nullable!".
This seems incorrect.object2is not among therequiredproperties so there must be some value to omit it.nullseems reasonable.object3is declared asnullableso indeed it should be a nullable type declaration.
array2is not given a nullable type declaration. It is not among therequiredproperties so there should be a way
to omit it, using a nullable type. (array1is correctly a non-nullable array, since it is a among therequired
properties.- The value
nullis sent forobject2if it isnull. This seems incorrect. Since it is not among therequired
properties, omitting it should not meannull. It should just not be present in the generated JSON.
object3isnullable, so a value ofnullis 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
nullshould by default not be generated asnullvalues in JSON. - Properties that are declared as
nullableand set tonullin the Dart model object are just fine to generate asnullvalues in JSON. Sincenullis 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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