Skip to content

Commit 90d4231

Browse files
committed
1 parent c28a4d2 commit 90d4231

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

docs/modules/ROOT/pages/processor/bean-validation.adoc

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,80 @@
11
= Bean Validation
22
include::partial$links.adoc[]
33

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+
478
== WebFlux
579

680
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:

docs/modules/ROOT/pages/processor/configuration.adoc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ compatibility:
4040
bean-validation-valid-on-reactive: false
4141
identifier-word-break-from-digit-to-letter: false
4242
identifier-prefix-invalid-enum-start: false
43-
43+
bean-validation:
44+
# bean-validation annotation to "custom" classes map
4445
map:
4546
# java type mappings
4647
----
@@ -533,6 +534,26 @@ keep the pre-2024.2 behavior. See xref:processor/bean-validation.adoc[Bean Valid
533534

534535
*Invalid characters* (e.g. numbers or underscore) at the start of enum values are prefixed with `"V"` (for value) to generate valid java identifiers. The old behaviour (pre-2025.4.1) was to strip the invalid start characters.
535536

537+
[#_bean_validation-map]
538+
== bean-validation: ([.badge .badge-since]+since 2022.6+)
539+
540+
Opt-In for custom classes that are supported by specific bean-validation annotations.
541+
542+
This is a simple map of bean-validation annotation to a list of additionally supported classes.
543+
544+
[source,yaml,subs=attributes+]
545+
----
546+
openapi-processor-mapping: {var-mapping-version}
547+
548+
bean-validation:
549+
jakarta.validation.constraints.DecimalMin:
550+
- my.custom.Integer
551+
jakarta.validation.constraints.DecimalMax:
552+
- my.custom.Integer
553+
----
554+
555+
See xref:processor/bean-validation.adoc[Bean Validation] for an explanation when this may be needed.
556+
536557
== map:
537558

538559
Using type mapping, we can tell the processor to map types (schemas) from an `openapi.yaml` description to a specific existing java type instead of generating a model class from the source OpenAPI type.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
:var-mapping-version: v13
1+
:var-mapping-version: v14
22
:var-openapi-version: 3.1.0

0 commit comments

Comments
 (0)