OpenAPITools / OpenAPITools/openapi-generator
[BUG] wrong interpretation of missing type in anyOf/allOf/oneOf as nullable
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
Since PR #15698, the generator assumes that a schema object without a type property means that the object is nullable. That assumption is as far as I can see not support by either OAS 3.0.3, OAS 3.1 or JSON Schema.
For OAS 3.0.3 the situation is explicitly documented at https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types, which states:
null is not supported as a type
For OAS 3.1 the null type is supported, however there is nothing in OAS nor JSON Schema that mandates that a missing type equals the null type.
openapi-generator version
- 7.0.0
- from master branch
OpenAPI declaration file content or url
A OpenAPI specification as input:
openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/foo:
get:
operationId: list
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/AnyOfRequiredInObject"
components:
schemas:
AnyOfRequiredInObject:
description: to test oneOf with required
type: object
oneOf:
- required: [ field3 ]
- required: [ field4 ]
properties:
field3:
type: integer
field4:
type: integer
is translated in openapi generator to:
"schemas" : {
"AnyOfRequiredInObject" : {
"description" : "to test oneOf with required",
"nullable" : true,
"oneOf" : [ ],
"properties" : {
"field3" : {
"type" : "integer"
},
"field4" : {
"type" : "integer"
}
},
"type" : "object"
}
}
Note the missing declarations in oneOf and the erroneously added "nullable": true
Generation Details
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g openapi -i test.yaml -o out
Steps to reproduce
run the openapi generator and check the output for missing oneOf and wrong nullable
Related issues/PRs
- #15698
- https://github.com/OAI/OpenAPI-Specification/issues/3148 is somewhat related
Suggest a fix
https://github.com/karzang/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java#L678-L696 should be changed to something like e.g.:
private boolean isNullTypeSchema(Schema schema) {
if (schema == null) {
return false;
}
if ((schema.getType() == null) {
return false;
}
if ((schema.getType().equals("null")) && schema.get$ref() == null) {
return true;
}
// convert referenced enum of null only to `nullable:true`
Schema referencedSchema = ModelUtils.getReferencedSchema(openAPI, schema);
if (referencedSchema.getEnum() != null && referencedSchema.getEnum().size() == 1) {
if ("null".equals(String.valueOf(referencedSchema.getEnum().get(0)))) {
return true;
}
}
return false;
}
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 modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java around lines 678-696, then reproduce the issue with the provided OpenAPI YAML and the openapi generator command. Check how schemas without a type are handled in anyOf, allOf, and oneOf. Done means the oneOf declarations are retained and nullable is not added when type is missing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100