OpenAPITools / OpenAPITools/openapi-generator
[BUG] [JAVA] microprofile client generator makes readOnly properties de-facto required
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
Java code generated from a readOnly-marked property causes a bug when the property is not present in the response json payload.
openapi-generator version
6.0.0
OpenAPI declaration file content or url
"components": {
"schemas": {
"Foo": {
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string",
"readOnly": true
}
}
}
}
}
Full sample openapi spec: https://gist.github.com/mak-100/cb924fca623211814585f21c8e3038a1
Generation Details
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.0.0</version>
</plugin>
...
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>.../openapi.json</inputSpec>
<generatorName>java</generatorName>
<apiPackage>de.integration</apiPackage>
<modelPackage>de.integration.model</modelPackage>
<generateApis>false</generateApis>
<generateModels>true</generateModels>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<generateSupportingFiles>false</generateSupportingFiles>
<library>microprofile</library>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
</execution>
Steps to reproduce
Run mvn compile to generate the source code from the openapi spec.
The generated class Foo.java has a constructor with a non-nullable parameter "b":
@JsonbCreator
public Foo(
@JsonbProperty("b") String b
) {
this.b = b;
}
The problem is that JsonbProperty.nillable() is false by default:
package javax.json.bind.annotation;
...
@JsonbAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface JsonbProperty {
...
/**
* Switches o/off serialization of null values.
*
* @return True if field with null value should be serialized as key/value pair into JSON with null value.
*/
boolean nillable() default false;
}
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/10086
Suggest a fix
Generate constructur where properties which are readOnly but not required have the attribute nillable = true, in the example above:
@JsonbCreator
public Foo(
@JsonbProperty(value = "b", nillable = true) String b
) {
this.b = b;
}
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 by running mvn compile with the linked OpenAPI specification and inspect the generated Foo.java. Trace the Java generator's microprofile library handling of the @JsonbCreator constructor, then verify that an absent, non-required readOnly property can be deserialized without failure and that the generated annotation reflects this behavior.
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
- 45/100