You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] [Java] [spring] Properties that are both required and nullable are marked NotNull #24686
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=false
Steps 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:
publicclassThing {
privateStringname = null; // field should be @Nullable/** * Get name * @return name */@NotNull// wrong annotation, should be @Nullable@Schema(name = "name", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("name")
publicStringgetName() {
returnname;
}
@JsonProperty("name")
publicvoidsetName(Stringname) { // parameter should be @Nullablethis.name = name;
}
changed the title [-][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[/+]on Aug 12, 2026
Thank you! For now, I'm just using additionalModelTypeAnnotations to add @NullUnmarked on the generated classes and ignore the issues until this is fixed.
@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.
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.
@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’
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: