-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG][SPRING] Using useBeanValidation together with openApiNullable creates model classes that validate null values incorrectly #11969
Comments
Seems like someone else also ran into this issue here OpenAPITools/jackson-databind-nullable#17 (comment) |
I took a look into the template and I think the intention of the If |
As a workaround I created a custom validator that checks that the JsonNullable is present: import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Constraint(validatedBy = JsonNullableIsPresentValidator.class)
@Target( {ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonNullableIsPresent {
String message() default "JsonNullable is not present";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
} import org.jetbrains.annotations.Nullable;
import org.openapitools.jackson.nullable.JsonNullable;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class JsonNullableIsPresentValidator implements ConstraintValidator<JsonNullableIsPresent, @Nullable JsonNullable<?>> {
@Override
public void initialize(JsonNullableIsPresent constraintAnnotation) {}
@Override
public boolean isValid(@Nullable JsonNullable<?> jsonNullable, ConstraintValidatorContext constraintValidatorContext) {
return jsonNullable != null && jsonNullable.isPresent();
}
} Then I created a customized template and modified the
|
Also reproduced with 6.0.0 |
We also ran into this issue and went with a custom |
Bug Report Checklist
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
andopenApiNullable: true
(they are enabled per default) it produces a model that contains the following:When I create an instance of this model where the field
endpoint
isnull
and try to validate it, the validator says it is invalid becauseendpoint
must not be null: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 #11506
OpenAPI declaration file content or url
Generation Details
Using the spring generator with
useBeanValidation: true
andopenApiNullable: true
Suggest a fix
Do not apply @NotNull annotation to getters that return a JsonNullable object.
The text was updated successfully, but these errors were encountered: