|
1 | 1 | = Bean Validation |
2 | 2 | include::partial$links.adoc[] |
3 | 3 |
|
| 4 | +== supported target classes |
| 5 | + |
| 6 | +=== avoiding compilation errors |
| 7 | + |
| 8 | +[.badge .badge-since]+since 2022.6+ |
| 9 | + |
| 10 | +Each bean-validation annotations has a limited number of supported classes that can be annotated by it. For example, `jakarta.validation.constraints.DecimalMin` does only support number-like classes. |
| 11 | + |
| 12 | +If placed on another non-number class compilation will fail. |
| 13 | + |
| 14 | +This can happen if bean-validation is combined with type mapping. A simple example is number representing a year. |
| 15 | + |
| 16 | +An OpenAPI with an integer parameter and bean validation enabled would add `@DecimalMin` & `@DecimalMax` annotations to the parameter in the generated code. |
| 17 | + |
| 18 | +[source,yaml,subs=attributes+] |
| 19 | +---- |
| 20 | +openapi: 3.1.0 |
| 21 | +info: |
| 22 | + title: drop bean validation annotation if mapped to unsupported type |
| 23 | + version: 1.0.0 |
| 24 | +
|
| 25 | +paths: |
| 26 | + /foo: |
| 27 | + get: |
| 28 | + parameters: |
| 29 | + - in: query |
| 30 | + name: year |
| 31 | + schema: |
| 32 | + type: integer |
| 33 | + format: year |
| 34 | + minimum: 1970 |
| 35 | + maximum: 2099 |
| 36 | +---- |
| 37 | + |
| 38 | +This is an issue if the parameter type is mapped to a different Java type. Since the value is representing a year it makes sense to map it `java.time.Year`. |
| 39 | + |
| 40 | +[source,yaml,subs=attributes+] |
| 41 | +---- |
| 42 | +openapi-processor-mapping: v14 |
| 43 | +
|
| 44 | +options: |
| 45 | + package-name: generated |
| 46 | + bean-validation: jakarta |
| 47 | +
|
| 48 | +map: |
| 49 | + types: |
| 50 | + - type: integer:year => java.time.Year |
| 51 | +---- |
| 52 | + |
| 53 | +Adding `@DecimalMin` & `@DecimalMax` to the parameter breaks compilation because both annotations do not support `java.time.Year`. |
| 54 | + |
| 55 | +=== supported custom classes |
| 56 | + |
| 57 | +In case the target type is not recognized automatically (and the annotations are dropped), for example on a custom `java.lang.Number` implementation, it is possible to tell the processor that an annotation is valid on that type. |
| 58 | + |
| 59 | +[source,yaml,subs=attributes+] |
| 60 | +---- |
| 61 | +openapi-processor-mapping: v14 |
| 62 | +
|
| 63 | +options: |
| 64 | +# ... |
| 65 | +
|
| 66 | +map: |
| 67 | +# ... |
| 68 | +
|
| 69 | +bean-validation: |
| 70 | + jakarta.validation.constraints.DecimalMin: |
| 71 | + - other.CustomInteger |
| 72 | + jakarta.validation.constraints.DecimalMax: |
| 73 | + - other.CustomInteger |
| 74 | +---- |
| 75 | + |
| 76 | +See also xref:processor/configuration.adoc#_bean_validation-map[bean validation] configuration. |
| 77 | + |
4 | 78 | == WebFlux |
5 | 79 |
|
6 | 80 | The position of the `@Valid` annotation on reactive types has changed in 2024.2. Until then the `@Valid` was placed on the generic type of the reactive wrapper, like this: |
|
0 commit comments