Repository navigation
Closed
Labels
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- 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 nullable property is required, the generated class has a non-nullable field and getter.
openapi-generator version
7.24.0
OpenAPI declaration file content or url
This is an example spec that I used to reproduce:
{
"openapi": "3.1.0",
"info": {
"title": "Reproduce",
"version": "1.0.0"
},
"paths": {
"/thing": {
"get": {
"operationId": "getThing",
"responses": {
"200": {
"description": "ok",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Thing"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Thing": {
"type": "object",
"required": [
"name",
"description"
],
"properties": {
"name": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"description": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
}
}
}
}
}
}Generation Details
Java 17
JAR=openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar
"$JAVA_HOME/bin/java" -jar "$JAR" generate \
-i spec.json \
-g spring \
-o out \
--additional-properties=useJspecify=true,hideGenerationTimestamp=true,openApiNullable=falseSteps to reproduce
Run the generator tool on the spec above with the provided options. Observe that the resulting class does not have the right nullability marks:
public class Thing {
private String name = null; // field should be @Nullable
/**
* Get name
* @return name
*/
@NotNull // wrong annotation, should be @Nullable
@Schema(name = "name", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) { // parameter should be @Nullable
this.name = name;
}
Activity
jpfinne commentedon Aug 12, 2026
@mdaul-tgtg this is not a issue with the anyof in 3.1. I reproduce the issue in 3.0.3:
I get these results:
void setName(JsonNullable<String> name)JsonNullable<String> getName()void setName(JsonNullable<String> name)JsonNullable<String> getName()void setName(@jakarta.annotation.Nullable String name)Thing name(@jakarta.annotation.Nullable String name)@jakarta.annotation.Nullable String getName()Missing
Spring + useOptionalandrestclient + openanApiNullableIt means that:
[-][BUG] [Java] Properties that are both required and nullable are marked NotNull[/-][+][BUG] [Java] [spring] Properties that are both required and nullable are marked NotNull[/+]mdaul-tgtg commentedon Aug 12, 2026
@jpfinne I added spring in the title since it's the one I am interested in, and where the bug has always existed.
But this is what I used to reproduce. Didn't you get the same results as I did with my spec?
jpfinne commentedon Aug 12, 2026
@mdaul-tgtg I think we have the same issue using 3.0.x and 3.1.x. It means that the bug is not in the new normalizer rules.
Quick solution. use these templates for spring
beanValidation.mustache
meaning that
@NotNullis not added for jspecify.nullableAnnotation_jspecify.mustache
meaning that
required && isNullable && && !openapiNullable->@NullableaddedThere was no unit test for this use case, that why I missed it in the implementation of JSpecify.
mdaul-tgtg commentedon Aug 12, 2026
Thank you! For now, I'm just using
additionalModelTypeAnnotationsto add@NullUnmarkedon the generated classes and ignore the issues until this is fixed.jpfinne commentedon Aug 12, 2026
@mdaul-tgtg Then you get something like the class below. How does it solve your issue? Don't you want
@Nullableannotations?mdaul-tgtg commentedon Aug 12, 2026
@jpfinne I do! I added NullUnmarked which means unspecified nullness - so it is not treated as non-nullable by the rest of the code. But this is a temporary workaround, in the end, I want the right annotations and nullability handling. Sorry if it was confusing.
itsmib commentedon Aug 15, 2026
Hi, I'd like to work on this fix. @jpfinne .. are you planning to submit a PR for the template changes you outlined? If not, I'd like to take a shot at it.
jpfinne commentedon Aug 15, 2026
@itsmib I have a draft PR #24711 .
I still need to check what to do with the ‘@NotNull’ annotation. See the regression found by Cubic.
I think it is needed for Hibernate validation on required and non nullable field.
For required and nullable I can introduce an annotation like ’@JsonNullableRequired’
jpfinne commentedon Aug 15, 2026
@itsmib @mdaul-tgtg can your review #24711?
And if ok, please approve
@itsmib using your sample, the generated code becomes: