Skip to content

Commit 5391b65

Browse files
fix: Java Constraint Generator omits @Valid annotation disallowing cascading validation
- Add missing `@Valid` annotations - Update tests Updates #2175
1 parent 69e49c9 commit 5391b65

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

examples/java-generate-jakarta-constraint-annotation/__snapshots__/index.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Array [
99
@NotNull
1010
@Max(99)
1111
private double maxNumberProp;
12+
@Valid
1213
@Size(min=2, max=3)
1314
private Object[] arrayProp;
1415
@Pattern(regexp=\\"^I_\\")

examples/java-generate-javax-constraint-annotation/__snapshots__/index.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Array [
99
@NotNull
1010
@Max(99)
1111
private double maxNumberProp;
12+
@Valid
1213
@Size(min=2, max=3)
1314
private Object[] arrayProp;
1415
@Pattern(regexp=\\"^I_\\")

src/generators/java/presets/ConstraintsPreset.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {
22
ConstrainedArrayModel,
33
ConstrainedFloatModel,
44
ConstrainedIntegerModel,
5+
ConstrainedObjectModel,
6+
ConstrainedReferenceModel,
57
ConstrainedStringModel
68
} from '../../../models';
79
import { JavaPreset } from '../JavaPreset';
@@ -33,6 +35,15 @@ export const JAVA_CONSTRAINTS_PRESET: JavaPreset<JavaConstraintsPresetOptions> =
3335

3436
const annotations: string[] = [];
3537

38+
// needs cascade validation
39+
if (
40+
property.property instanceof ConstrainedReferenceModel ||
41+
property.property instanceof ConstrainedObjectModel ||
42+
property.property instanceof ConstrainedArrayModel
43+
) {
44+
annotations.push(renderer.renderAnnotation('Valid'));
45+
}
46+
3647
if (property.required) {
3748
annotations.push(renderer.renderAnnotation('NotNull'));
3849
}

test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public interface Vehicle {
196196
exports[`JavaGenerator oneOf/discriminator with jackson preset date-time format should render java.time.OffsetDateTime 1`] = `
197197
Array [
198198
"public class Event {
199+
@Valid
199200
@NotNull
200201
@JsonProperty(\\"action\\")
201202
private Action action;
@@ -311,6 +312,7 @@ public interface Pet {
311312
@NotNull
312313
@JsonProperty(\\"specversion\\")
313314
private final String specversion = \\"1.0\\";
315+
@Valid
314316
@NotNull
315317
@JsonProperty(\\"type\\")
316318
private final CloudEventType type = CloudEventType.DOG;
@@ -431,6 +433,7 @@ public interface Pet {
431433
@NotNull
432434
@JsonProperty(\\"specversion\\")
433435
private final String specversion = \\"1.0\\";
436+
@Valid
434437
@NotNull
435438
@JsonProperty(\\"type\\")
436439
private final CloudEventType type = CloudEventType.CAT;
@@ -521,6 +524,7 @@ Array [
521524
@NotNull
522525
@JsonProperty(\\"id\\")
523526
private String id;
527+
@Valid
524528
@NotNull
525529
@JsonProperty(\\"type\\")
526530
private final CloudEventType type = CloudEventType.DOG;
@@ -623,6 +627,7 @@ public interface Pet {
623627
624628
}",
625629
"public class Dog implements Pet {
630+
@Valid
626631
@NotNull
627632
@JsonProperty(\\"type\\")
628633
private final DogType type = DogType.DOG;
@@ -704,6 +709,7 @@ public interface Pet {
704709
}
705710
}",
706711
"public class Cat implements Pet {
712+
@Valid
707713
@NotNull
708714
@JsonProperty(\\"type\\")
709715
private final CatType type = CatType.CAT;
@@ -801,6 +807,7 @@ public interface Vehicle {
801807
VehicleType getVehicleType();
802808
}",
803809
"public class Cargo {
810+
@Valid
804811
@JsonProperty(\\"vehicle\\")
805812
@JsonInclude(JsonInclude.Include.NON_NULL)
806813
private Vehicle vehicle;
@@ -854,6 +861,7 @@ public interface Vehicle {
854861
}
855862
}",
856863
"public class Car implements Vehicle {
864+
@Valid
857865
@NotNull
858866
@JsonProperty(\\"vehicleType\\")
859867
private final VehicleType vehicleType = VehicleType.CAR;
@@ -935,6 +943,7 @@ public interface Vehicle {
935943
}
936944
}",
937945
"public class Truck implements Vehicle {
946+
@Valid
938947
@NotNull
939948
@JsonProperty(\\"vehicleType\\")
940949
private final VehicleType vehicleType = VehicleType.TRUCK;

test/generators/java/presets/__snapshots__/ConstraintsPreset.spec.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ exports[`JAVA_CONSTRAINTS_PRESET should render jakarta constraints annotations f
3030
@NotNull
3131
@Max(99)
3232
private double maxNumberProp;
33+
@Valid
3334
@Size(min=2, max=3)
3435
private Object[] arrayProp;
3536
@Pattern(regexp=\\"^\\\\\\\\w+(\\\\\\"\\\\\\\\.\\\\\\\\w+)*$\\")
@@ -62,6 +63,7 @@ exports[`JAVA_CONSTRAINTS_PRESET should render javax constraints annotations by
6263
@NotNull
6364
@Max(99)
6465
private double maxNumberProp;
66+
@Valid
6567
@Size(min=2, max=3)
6668
private Object[] arrayProp;
6769
@Pattern(regexp=\\"^\\\\\\\\w+(\\\\\\"\\\\\\\\.\\\\\\\\w+)*$\\")
@@ -94,6 +96,7 @@ exports[`JAVA_CONSTRAINTS_PRESET should render javax constraints annotations whe
9496
@NotNull
9597
@Max(99)
9698
private double maxNumberProp;
99+
@Valid
97100
@Size(min=2, max=3)
98101
private Object[] arrayProp;
99102
@Pattern(regexp=\\"^\\\\\\\\w+(\\\\\\"\\\\\\\\.\\\\\\\\w+)*$\\")

0 commit comments

Comments
 (0)