micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.01k stars 1.05k forks source link

List field with constraint on type is not validated #1814

Open ilopmar opened 5 years ago

ilopmar commented 5 years ago

Steps to Reproduce

Create a POJO:

@Introspected
public class MyPojo {

    private List<@NotBlank String> myList;
    // constructors, getters and setters
}

Create a test:

@MicronautTest
class MyPojoConstraintsSpec extends Specification {

    @Inject
    Validator validator

    void 'test custom validator'() {
        given:
        MyPojo myPojo = new MyPojo(myList: ['', ' '])

        when:
        Set<ConstraintViolation<MyPojo>> constraintViolations = validator.validate(myPojo)

        then:
        constraintViolations.size() == 1
        constraintViolations.first().message == 'must not be blank'
    }
}

Expected Behaviour

The test should pass

Actual Behaviour

The test fails:

constraintViolations.size() == 1
|                    |      |
[]                   0      false

Environment Information

Example Application

https://github.com/ilopmar/mn-bean-validator-data-class. Run MyPojoConstraintsSpec test

Additional info

It seems that the issue is @Introspected doesn't add the necessary information on myList field. So when retrieving the constraints here: https://github.com/micronaut-projects/micronaut-core/blob/40d2f1f11f763b0a20f2e5d245a4f8b9daf84087/validation/src/main/java/io/micronaut/validation/validator/DefaultValidator.java#L129-L130 the list is empty.

Also, annotation myList with @Valid triggers some validation but again, the information metadata is not found.

graemerocher commented 5 years ago

The introspection API currently does not support annotation metadata on generic type arguments. Until this is supported we cannot support this use case. This is not in scope for 1.2.0 however.

You can however implement this with:

  @Valid
  @NotBlank
  private List<String> myList;
praneeth-nayak commented 1 month ago

I have a similar issue with javax @Size annotation Miconaut version: 3.9.4

@Valid
@Size(max = 2) List<@Valid @NotBlank @Size(max = 10) String> stringList;

The problem with above snipped is the outer limit of maximum size of list i.e. 2, is getting applied on inside string items and it's not allowing more than 2 characters as strings

dstepanov commented 1 month ago

The validation was rewritten in Micronaut 4