OpenAPITools / OpenAPITools/openapi-generator
[BUG] additionalProperties with nullable generates broken model
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
Using "additionalProperties: true" together with "nullable: true" on "type: object" or "type: array" generates a broken model class which can not compile:
incompatible types: cannot infer type arguments for java.util.HashMap<>
cannot find symbol
Because it uses an Map together with JsonNullable:
@JsonProperty("foobar")
@Valid
private JsonNullable<Map<String, Object>> foobar = JsonNullable.undefined();
But generates an put method without JsonNullable:
public Pet putFoobarItem(String key, Object foobarItem) {
if (this.foobar == null) {
this.foobar = new HashMap<>();
}
this.foobar.put(key, foobarItem);
return this;
}
openapi-generator version
5.3.1
OpenAPI declaration file content or url
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
foobar:
type: object
additionalProperties: true
nullable: true
Generation Details
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.3.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/petstore.yml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>com.baeldung.openapi.api</apiPackage>
<modelPackage>com.baeldung.openapi.model</modelPackage>
<supportingFilesToGenerate>
ApiUtil.java
</supportingFilesToGenerate>
<configOptions>
<delegatePattern>true</delegatePattern>
<sourceFolder>src/gen/java/main</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Steps to reproduce
https://github.com/noshua/openapi-additionalPropertiesWithNullable.git
Related issues/PRs
Suggest a fix
Generated put method should look something like this:
public NosJobShort putExtraOptsItem(String key, Object extraOptsItem) {
if (!this.extraOpts.isPresent()) {
this.extraOpts = JsonNullable.of(new HashMap<>());
}
this.extraOpts.get().put(key, extraOptsItem);
return this;
}
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 linked reproduction repository and generate the Spring model from its petstore.yml using the stated openapi-generator Maven configuration. Compare the nullable additionalProperties field with its generated put method; done means the generated Java model compiles and handles the nullable map correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- backend-api-design, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100