Micronaut-core: @Valid doesn't trigger contstraints with @Data from lombok

Created on 8 Dec 2020  路  4Comments  路  Source: micronaut-projects/micronaut-core

Task List

  • [x] Steps to reproduce provided
  • [ ] Stacktrace (if present) provided
  • [x] Example that reproduces the problem uploaded to Github
  • [x] Full description of the issue provided (see below)

Steps to Reproduce

  1. Create a new project with maven and openapi features.
  2. Install and configure lombok as it says here
  3. Add controllers and models with their respective validations (@Min, @NotNull, etc)
  4. Running the project or its tests.

Expected Behaviour

In a POST request the @Valid annotation must be triggered and show the errors of the restrictions set in the POJO class configured with lombok.

Actual Behaviour

The @Valid annotation doesn't validate the class configured with lombok unless it has the getters set as you can see in the example below.
The User class does not have the getters set and @Valid does not trigger. On the other hand, the Pet class, having its getters, @Valid triggers the validation.

Environment Information

  • Operating System: Artix Linux x86_64
  • Micronaut Version: 2.2.1
  • JDK Version: OpenJDK 14.0.2
notabug

All 4 comments

@alvarosanchez This is specific to Maven. It seems the annotation processor order is not respected. I created an equivalent gradle build and it works as expected.

The problem is this line: https://github.com/glats/lombok-validator-example/blob/master/pom.xml#L110

It must be:

          <annotationProcessorPaths combine.self="override">
            <path>
              <!-- must precede micronaut-inject-java -->
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>${lombok.version}</version>
            </path>
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-inject-java</artifactId>
              <version>${micronaut.version}</version>
            </path>
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-validation</artifactId>
              <version>${micronaut.version}</version>
            </path>
            <path>
              <groupId>io.micronaut.openapi</groupId>
              <artifactId>micronaut-openapi</artifactId>
              <version>${micronaut.openapi.version}</version>
            </path>
          </annotationProcessorPaths>

Micronaut Launch generates the correct POM.

I see. Thank for your answer. However, is it possible to add this to the official documentation?

Sure. I created #4701

Was this page helpful?
0 / 5 - 0 ratings