Closed MelleD closed 1 year ago
@wing328 Maybe now it's time to discuss my PR feedback in https://github.com/OpenAPITools/jackson-databind-nullable/pull/35#issuecomment-1294854904?
@tofi86 I'm not sure what did not work for you?
But here is a example with output that the Bean validation is working with 0.24 when the JsonNullable is not undefined
public class JsonNullableTest extends WebBaseTest {
public static class MyBean {
@Valid
private JsonNullable<Set<Long>> setJsonNullable = JsonNullable.of( Set.of( 1L ) );
@Valid
private final JsonNullable<Set<MyBean2>> beans2 = JsonNullable.of( Set.of( new MyBean2() ) );
@Valid
private final JsonNullable<MyBean2> bean2 = JsonNullable.of( new MyBean2() );
public void setSetJsonNullable( final JsonNullable<Set<Long>> setJsonNullable ) {
this.setJsonNullable = setJsonNullable;
}
@Size( min = 2 )
public JsonNullable<Set<Long>> getSetJsonNullable() {
return setJsonNullable;
}
@Valid
public JsonNullable<MyBean2> getBean2() {
return bean2;
}
@Valid
@Size( min = 2 )
public JsonNullable<Set<MyBean2>> getBeans2() {
return beans2;
}
}
public static class MyBean2 {
@NotNull
private String name;
private JsonNullable<String> name2 = JsonNullable.of( "b" );
public void setName( final String name ) {
this.name = name;
}
public String getName() {
return name;
}
@Size( min = 5 )
@Pattern( regexp = "a*" )
public JsonNullable<String> getName2() {
return name2;
}
public void setName2( final JsonNullable<String> name2 ) {
this.name2 = name2;
}
}
@Autowired
ValidationService validationService;
@Test
void test() {
final MyBean bean = new MyBean();
try {
validationService.validate( bean );
} catch ( final ConstraintViolationException e ) {
e.getConstraintViolations().stream()
.forEach( constraintViolation -> System.out.println( constraintViolation.getPropertyPath() + ": " + constraintViolation.getMessage() ) );
}
}
}
}
setJsonNullable: size must be between 2 and 2147483647
beans2: size must be between 2 and 2147483647
bean2.name2: size must be between 5 and 2147483647
bean2.name: must not be null
bean2.name2: must match "a*"
@tofi86 Or is this the root cause?
@Valid
private final JsonNullable<Set<MyBean2>> beans2 = JsonNullable.of( Set.of( new MyBean2() ) );
That the elements in MyBean2(NotNull, Pattern etc) are not checked?
But is the same for Optional, correct?
@Valid
private final Optional<@Valid Set<MyBean2>> beansOptional = Optional.of( Set.of( new MyBean2() ) );
@tofi86 me again :)
IMHO this is a Bug into the generator because the validation works. I think you can revert you change. The @Valid have to inside the Set then wit works.
Example:
public class JsonNullableTest extends WebBaseTest {
public static class MyBean {
private final JsonNullable<Set<@Valid MyBean2>> beansNullable = JsonNullable.of( Set.of( new MyBean2() ) );
private final Optional<Set<@Valid MyBean2>> beansOptional = Optional.of( Set.of( new MyBean2() ) );
public JsonNullable<Set<MyBean2>> getBeansNullable() {
return beansNullable;
}
public Optional<Set<MyBean2>> getBeansOptional() {
return beansOptional;
}
}
public static class MyBean2 {
@NotNull
private String name;
private JsonNullable<String> name2 = JsonNullable.of( "b" );
public void setName( final String name ) {
this.name = name;
}
public String getName() {
return name;
}
@Size( min = 5 )
@Pattern( regexp = "a*" )
public JsonNullable<String> getName2() {
return name2;
}
public void setName2( final JsonNullable<String> name2 ) {
this.name2 = name2;
}
}
@Autowired
ValidationService validationService;
@Test
void test() {
final MyBean bean = new MyBean();
try {
validationService.validate( bean );
} catch ( final ConstraintViolationException e ) {
e.getConstraintViolations().stream()
.forEach( constraintViolation -> System.out.println( constraintViolation.getPropertyPath() + ": " + constraintViolation.getMessage() ) );
}
}
}
beansNullable[].name2: must match "a*"
beansNullable[].name2: size must be between 5 and 2147483647
beansNullable[].name: must not be null
beansOptional[].name2: size must be between 5 and 2147483647
beansOptional[].name: must not be null
beansOptional[].name2: must match "a*"
So IMHO we have just to change the generator to set the @Valid on the right position.
@wing328 what do you think?
@wing328 & @tofi86
IMHO we should revert the change and discuss the solution on the generator side https://github.com/OpenAPITools/openapi-generator/issues/14723
IMHO this is a Bug into the generator because the validation works. I think you can revert you change. The @Valid have to inside the Set then wit works.
Sounds good to me. Thanks for taking the lead on this issue @MelleD!
@tofi86 Or is this the root cause?
@Valid private final JsonNullable<Set<MyBean2>> beans2 = JsonNullable.of( Set.of( new MyBean2() ) );
That the elements in MyBean2(NotNull, Pattern etc) are not checked?
Yes exactly, that was one of the issues PR #35 tried to solve. Also getting the exact validation path of the failed item.
With the version >0.25 the Collection handling is broken.
When you use for example JsonNullable<Set> the extractor (JsonNullableValueExtractorHelper) throws a error.
Example:
@tofi86 Think it is related to you change JsonNullable handling
EDIT: Ok i see there is a longer message, but no solution. https://github.com/OpenAPITools/jackson-databind-nullable/pull/35
But currently this crashes the open api generator with spring boot.