Skip to content

Commit

Permalink
fix: the Java Constraint Generator omits @Valid annotation (#2176)
Browse files Browse the repository at this point in the history
  • Loading branch information
istvan-nagy-epam authored Feb 8, 2025
1 parent 69e49c9 commit 2387ef0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Array [
@NotNull
@Max(99)
private double maxNumberProp;
@Valid
@Size(min=2, max=3)
private Object[] arrayProp;
@Pattern(regexp=\\"^I_\\")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Array [
@NotNull
@Max(99)
private double maxNumberProp;
@Valid
@Size(min=2, max=3)
private Object[] arrayProp;
@Pattern(regexp=\\"^I_\\")
Expand Down
11 changes: 11 additions & 0 deletions src/generators/java/presets/ConstraintsPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
ConstrainedArrayModel,
ConstrainedFloatModel,
ConstrainedIntegerModel,
ConstrainedObjectModel,
ConstrainedReferenceModel,
ConstrainedStringModel
} from '../../../models';
import { JavaPreset } from '../JavaPreset';
Expand Down Expand Up @@ -33,6 +35,15 @@ export const JAVA_CONSTRAINTS_PRESET: JavaPreset<JavaConstraintsPresetOptions> =

const annotations: string[] = [];

// needs cascade validation
if (
property.property instanceof ConstrainedReferenceModel ||
property.property instanceof ConstrainedObjectModel ||
property.property instanceof ConstrainedArrayModel
) {
annotations.push(renderer.renderAnnotation('Valid'));
}

if (property.required) {
annotations.push(renderer.renderAnnotation('NotNull'));
}
Expand Down
9 changes: 9 additions & 0 deletions test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public interface Vehicle {
exports[`JavaGenerator oneOf/discriminator with jackson preset date-time format should render java.time.OffsetDateTime 1`] = `
Array [
"public class Event {
@Valid
@NotNull
@JsonProperty(\\"action\\")
private Action action;
Expand Down Expand Up @@ -311,6 +312,7 @@ public interface Pet {
@NotNull
@JsonProperty(\\"specversion\\")
private final String specversion = \\"1.0\\";
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final CloudEventType type = CloudEventType.DOG;
Expand Down Expand Up @@ -431,6 +433,7 @@ public interface Pet {
@NotNull
@JsonProperty(\\"specversion\\")
private final String specversion = \\"1.0\\";
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final CloudEventType type = CloudEventType.CAT;
Expand Down Expand Up @@ -521,6 +524,7 @@ Array [
@NotNull
@JsonProperty(\\"id\\")
private String id;
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final CloudEventType type = CloudEventType.DOG;
Expand Down Expand Up @@ -623,6 +627,7 @@ public interface Pet {
}",
"public class Dog implements Pet {
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final DogType type = DogType.DOG;
Expand Down Expand Up @@ -704,6 +709,7 @@ public interface Pet {
}
}",
"public class Cat implements Pet {
@Valid
@NotNull
@JsonProperty(\\"type\\")
private final CatType type = CatType.CAT;
Expand Down Expand Up @@ -801,6 +807,7 @@ public interface Vehicle {
VehicleType getVehicleType();
}",
"public class Cargo {
@Valid
@JsonProperty(\\"vehicle\\")
@JsonInclude(JsonInclude.Include.NON_NULL)
private Vehicle vehicle;
Expand Down Expand Up @@ -854,6 +861,7 @@ public interface Vehicle {
}
}",
"public class Car implements Vehicle {
@Valid
@NotNull
@JsonProperty(\\"vehicleType\\")
private final VehicleType vehicleType = VehicleType.CAR;
Expand Down Expand Up @@ -935,6 +943,7 @@ public interface Vehicle {
}
}",
"public class Truck implements Vehicle {
@Valid
@NotNull
@JsonProperty(\\"vehicleType\\")
private final VehicleType vehicleType = VehicleType.TRUCK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ exports[`JAVA_CONSTRAINTS_PRESET should render jakarta constraints annotations f
@NotNull
@Max(99)
private double maxNumberProp;
@Valid
@Size(min=2, max=3)
private Object[] arrayProp;
@Pattern(regexp=\\"^\\\\\\\\w+(\\\\\\"\\\\\\\\.\\\\\\\\w+)*$\\")
Expand Down Expand Up @@ -62,6 +63,7 @@ exports[`JAVA_CONSTRAINTS_PRESET should render javax constraints annotations by
@NotNull
@Max(99)
private double maxNumberProp;
@Valid
@Size(min=2, max=3)
private Object[] arrayProp;
@Pattern(regexp=\\"^\\\\\\\\w+(\\\\\\"\\\\\\\\.\\\\\\\\w+)*$\\")
Expand Down Expand Up @@ -94,6 +96,7 @@ exports[`JAVA_CONSTRAINTS_PRESET should render javax constraints annotations whe
@NotNull
@Max(99)
private double maxNumberProp;
@Valid
@Size(min=2, max=3)
private Object[] arrayProp;
@Pattern(regexp=\\"^\\\\\\\\w+(\\\\\\"\\\\\\\\.\\\\\\\\w+)*$\\")
Expand Down

0 comments on commit 2387ef0

Please sign in to comment.