OpenAPITools / OpenAPITools/openapi-generator
[BUG][SPRING] Using useBeanValidation together with openApiNullable creates model classes that validate null values incorrectly
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
I have a spec that contains a repsonse that contains a json object with a nullable required field (see below). When I use the spring generator with useBeanValidation: true and openApiNullable: true (they are enabled per default) it produces a model that contains the following:
public class Config {
@JsonProperty("endpoint")
private JsonNullable<String> endpoint = JsonNullable.undefined();
public Config endpoint(String endpoint) {
this.endpoint = JsonNullable.of(endpoint);
return this;
}
@ApiModelProperty(required = true, value = "")
@NotNull
public JsonNullable<String> getEndpoint() {
return endpoint;
}
public void setEndpoint(JsonNullable<String> endpoint) {
this.endpoint = endpoint;
}
When I create an instance of this model where the field endpoint is null and try to validate it, the validator says it is invalid because endpoint must not be null:
var config = new Config().setEndpoint(null);
var validator = Validation.buildDefaultValidatorFactory().getValidator();
var validationResult = validator.validate(config);
I think this happens because JsonNullable is implemented in a way that the validation constraint is applied to the wrapped value and not to the JsonNullable object itself (see https://github.com/OpenAPITools/jackson-databind-nullable)
openapi-generator version
5.3.1
I cannot upgrade to 5.4.0 because of https://github.com/OpenAPITools/openapi-generator/issues/11506
OpenAPI declaration file content or url
components:
responses:
Config:
description: success
content:
application/json:
schema:
title: Config
type: object
properties:
endpoint:
type: string
nullable: true
required:
- endpoint
Generation Details
Using the spring generator with useBeanValidation: true and openApiNullable: true
Suggest a fix
Do not apply @NotNull annotation to getters that return a JsonNullable object.
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 Spring generator's handling of useBeanValidation and openApiNullable, then reproduce the issue with the Config model shown in the report. Confirm that validation incorrectly rejects a null endpoint and that the completed change avoids applying @NotNull to getters returning JsonNullable while preserving required-field validation elsewhere.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100