OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java] additionalProperties with properties generates incorrect/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
When a type in schema has a property that has both additionalProperties and properties, the Java engine generates broken code. For the given example, the following code was generated:
public Pet putFoobarItem(String key, String foobarItem) {
if (this.foobar == null) {
this.foobar = ;
}
this.foobar.put(key, foobarItem);
return this;
}
It is obvious that the code is broken.
For other engines, the generated code also does not do what the schema specifies, i.e. typechecks for specified properties, and the presence check for required properties.
openapi-generator version
5.4.0
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:
type: string
properties:
fooint:
type: integer
fooboolean:
type: boolean
foostring:
type: string
barint:
type: integer
required:
- fooint
- fooboolean
- foostring
Full YAML file: https://gist.github.com/visualage/202b8865fb684097fb5a8d9bd7c7b2ff
Generation Details
java -jar openapi-generator-cli-5.4.0.jar generate -g java -i bad.yaml -o java
Steps to reproduce
Use the command line above. Check the generated file java/src/main/java/org/openapitools/client/model/Pet.java.
Related issues/PRs
None.
Suggest a fix
We should generate a specific model class when the property type is object and has properties defined, regardless if additionalProperties is there or not. If additionalProperties is there, we need to enhance the class so that any unrecognized properties would be put into a special map property. This can be done in Java Jackson by @JsonAnyGetter and @JsonAnySetter annotations. I am not familiar with other engine/language/libraries, so can't speak for them.
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
Reproduce the issue with the provided YAML and the Java generation command, then inspect java/src/main/java/org/openapitools/client/model/Pet.java for the broken initialization and missing property handling. Done means the generated model correctly represents declared properties, required-property checks, and additional properties without broken Java code.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100