Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Java Constraint Generator omits @Valid annotation disallowing cascading validation #2175

Closed
2 tasks done
istvan-nagy-epam opened this issue Feb 7, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@istvan-nagy-epam
Copy link
Contributor

istvan-nagy-epam commented Feb 7, 2025

Describe the bug.

When the Java Generator is used with JAVA_CONSTRAINTS_PRESET preset as seen here:
https://github.com/istvan-nagy-epam/modelina/blob/0fc569bacafaface734b5b3eec171fa930f17847/examples/java-generate-jakarta-constraint-annotation/index.ts#L3-L13

Using a schema like this:
https://github.com/istvan-nagy-epam/modelina/blob/0fc569bacafaface734b5b3eec171fa930f17847/examples/java-generate-jakarta-constraint-annotation/index.ts#L14-L65

Will generate code snippets without the @Valid annotation in case of the Object or Collection types fields. This prevents cascading validation using the generated Java code. Meaning, that only the Person object would be validated, but neither the Website nor the Address objects would be validated (neither as a single object nor as an element of the generated list)

The currently generated code looks like this:

public class Person {
  @NotNull
  private String firstName;
  @NotNull
  private String lastName;
  @NotNull
  private double age;
  private Website website;
  @NotNull
  private Address homeAddress;
  @Size(min=1, max=5)
  private List<Address> workAddresses;

  public String getFirstName() { return this.firstName; }
  public void setFirstName(String firstName) { this.firstName = firstName; }

  public String getLastName() { return this.lastName; }
  public void setLastName(String lastName) { this.lastName = lastName; }

  public double getAge() { return this.age; }
  public void setAge(double age) { this.age = age; }

  public Website getWebsite() { return this.website; }
  public void setWebsite(Website website) { this.website = website; }

  public Address getHomeAddress() { return this.homeAddress; }
  public void setHomeAddress(Address homeAddress) { this.homeAddress = homeAddress; }

  public List<Address> getWorkAddresses() { return this.workAddresses; }
  public void setWorkAddresses(List<Address> workAddresses) { this.workAddresses = workAddresses; }
}
public class Website {
  @NotNull
  private String name;
  @NotNull
  private String url;

  public String getName() { return this.name; }
  public void setName(String name) { this.name = name; }

  public String getUrl() { return this.url; }
  public void setUrl(String url) { this.url = url; }
}
public class Address {
  @NotNull
  private String streetAddress;
  @NotNull
  private String city;
  private String state;

  public String getStreetAddress() { return this.streetAddress; }
  public void setStreetAddress(String streetAddress) { this.streetAddress = streetAddress; }

  public String getCity() { return this.city; }
  public void setCity(String city) { this.city = city; }

  public String getState() { return this.state; }
  public void setState(String state) { this.state = state; }
}

Expected behavior

The expected output would contain the @Valid annotations like this in case of the Person class:

public class Person {
  @NotNull
  private String firstName;
  @NotNull
  private String lastName;
  @NotNull
  private double age;
  @Valid
  private Website website;
  @Valid
  @NotNull
  private Address homeAddress;
  @Valid
  @Size(min=1, max=5)
  private List<Address> workAddresses;
//getters and setters...
}

Screenshots

Please see example project provided here: istvan-nagy-epam@0fc569b

How to Reproduce

  1. Use example project
  2. Generate models for the provided schema
  3. Observe results

🥦 Browser

None

👀 Have you checked for similar open issues?

  • I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

None

@istvan-nagy-epam istvan-nagy-epam added the bug Something isn't working label Feb 7, 2025
Copy link
Contributor

github-actions bot commented Feb 7, 2025

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

istvan-nagy-epam added a commit to istvan-nagy-epam/modelina that referenced this issue Feb 7, 2025
…cascading validation

- Add missing `@Valid` annotations
- Update tests

Updates asyncapi#2175
istvan-nagy-epam added a commit to istvan-nagy-epam/modelina that referenced this issue Feb 7, 2025
…cascading validation

- Add missing `@Valid` annotations
- Update tests

Updates asyncapi#2175
nagyesta added a commit to nagyesta/modelina that referenced this issue Feb 8, 2025
- Add missing annotation
- Update tests

Resolves asyncapi#2175

Signed-off-by: Esta Nagy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants