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

[4947][java]: adds support for validation of primitives in arrays #17165

Merged

Conversation

Aliaksie
Copy link
Contributor

@Aliaksie Aliaksie commented Nov 22, 2023

Adds Bean validation for primitive type in array see issue and issue

I'm not sure this should be added here but intuitively feel that it should be some how general for Java, also I may not have covered all possible options but I hope that this could be a good start for this story.

I'm looking forward to any recommendations and advice! and if all these changes do not have any sense, just reject this PR.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328 @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg

@Aliaksie Aliaksie force-pushed the bugfix/4947_adds_validation_in_array branch from bf53b15 to b5cdc5d Compare November 22, 2023 14:24
@Aliaksie Aliaksie force-pushed the bugfix/4947_adds_validation_in_array branch from ce7e022 to c668a11 Compare November 24, 2023 09:44
Copy link
Contributor

@martin-mfg martin-mfg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR!

I may not have covered all possible options but I hope that this could be a good start for this story.

According to https://swagger.io/docs/specification/data-models/data-types/ there are some more relevant options. e.g. multipleOf, minItems, etc.
It's up to you whether you want to support more options in this PR or not.

@martin-mfg
Copy link
Contributor

LGTM.
Let's wait for a maintainer to merge this PR. :)

@Aliaksie
Copy link
Contributor Author

Aliaksie commented Dec 4, 2023

@wing328 could you please have a look ?

// the response data types should not contains a bean validation annotation.
if (r.dataType.contains("@")) {
r.dataType = r.dataType.replaceAll("(?:(?i)@[a-z0-9]*+\\s*)*+", "");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's a must to do this in the default codegen as this code block seems to handle data type for Java generators only.

What about doing moving these check in postProcessWithModels in abstract java codegen instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure why not, please check if it looks better and i got you correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328, sorry to bother you, have you had a chance to look into it? do I need to make additional changes or can I resolve this conversation ?

@@ -1420,136 +1420,11 @@ public void setRequestMappingMode(RequestMappingMode requestMappingMode) {
this.requestMappingMode = requestMappingMode;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc spring technical committee as the change impacts spring codegen as well

@cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)

Copy link
Contributor

@welshm welshm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me from a Java Spring perspective.

Tests from #17157 should still be passing so the recently added validation should still be working 👍


private String getIntegerBeanValidation(Schema<?> items) {
if (items.getMinimum() != null && items.getMaximum() != null) {
return String.format(Locale.ROOT, "@Min(%s) @Max(%s)", items.getMinimum(), items.getMaximum());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is generating code - I don't think it should be taking in a locale, since it should be the same regardless of the machine/user Locale settings

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Locale.ROOT because I saw a lot of examples in code base, which is why I decided that this is the customary way to use it in the project... I can do it with the usual concatenation... but it seems to me that the code will become less readable... what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better for @wing328 or someone with broader context to comment it just seems odd to use a Locale to format strings which are supposed to represent code.

If that's the standard for the project, probably best to leave as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328, sorry to bother you, have you had a chance to look into it? do I need to make additional changes or can I resolve this conversation ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me take another look tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 hey hey! sorry to bother you again... any updates?

note: I noticed that the tests for some samples in the extended test checks did not pass due to the lack of imports .. that’s why I added imports to the templates .. I hope I found all the places where this is needed, let me know if it’s ok

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some java sample tests still failed. can you please take a look when you've time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sorry for that, I missed that for helidon should use rootJavaEEPackage in templates, fixed hope now all tests will passed. Thanks!

@@ -945,6 +946,121 @@ public String getTypeDeclaration(Schema p) {
return super.getTypeDeclaration(target);
}

private String getBeanValidation(Schema<?> items) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor suggestion: add a docstring for this method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@wing328 wing328 merged commit 809b333 into OpenAPITools:master Dec 11, 2023
120 checks passed
@wing328
Copy link
Member

wing328 commented Dec 11, 2023

just merged the PR. thanks again for the contribution.

@Aliaksie Aliaksie deleted the bugfix/4947_adds_validation_in_array branch December 18, 2023 19:21
@wing328
Copy link
Member

wing328 commented Dec 26, 2023

@Aliaksie just noticed that this PR seems to break the CI tests.

In samples/server/petstore/jaxrs/jersey2, run mvn clean package resulting in the following errors:

[ERROR] /Users/williamcheng/Code/openapi-generator7/samples/server/petstore/jaxrs/jersey2/src/gen/java/org/openapitools/model/ArrayTest.java:[112,50] annotation @javax.validation.Valid not applicable in this type context
[ERROR] /Users/williamcheng/Code/openapi-generator7/samples/server/petstore/jaxrs/jersey2/src/gen/java/org/openapitools/model/ArrayTest.java:[127,20] annotation @javax.validation.Valid not applicable in this type context
[ERROR] /Users/williamcheng/Code/openapi-generator7/samples/server/petstore/jaxrs/jersey2/src/gen/java/org/openapitools/model/ArrayTest.java:[131,46] annotation @javax.validation.Valid not applicable in this type context
[ERROR] /Users/williamcheng/Code/openapi-generator7/samples/server/petstore/jaxrs/jersey2/src/main/java/org/openapitools/api/impl/UserApiServiceImpl.java:[26,52] annotation @javax.validation.Valid not applicable in this type context

can you please take a look when you've time?

ref: https://github.com/OpenAPITools/openapi-generator/actions/runs/7262479579/job/19950668803?pr=17431

@Aliaksie
Copy link
Contributor Author

Aliaksie commented Dec 26, 2023

@Aliaksie just noticed that this PR seems to break the CI tests.

In samples/server/petstore/jaxrs/jersey2, run mvn clean package resulting in the following errors:

[ERROR] /Users/williamcheng/Code/openapi-generator7/samples/server/petstore/jaxrs/jersey2/src/gen/java/org/openapitools/model/ArrayTest.java:[112,50] annotation @javax.validation.Valid not applicable in this type context
[ERROR] /Users/williamcheng/Code/openapi-generator7/samples/server/petstore/jaxrs/jersey2/src/gen/java/org/openapitools/model/ArrayTest.java:[127,20] annotation @javax.validation.Valid not applicable in this type context
[ERROR] /Users/williamcheng/Code/openapi-generator7/samples/server/petstore/jaxrs/jersey2/src/gen/java/org/openapitools/model/ArrayTest.java:[131,46] annotation @javax.validation.Valid not applicable in this type context
[ERROR] /Users/williamcheng/Code/openapi-generator7/samples/server/petstore/jaxrs/jersey2/src/main/java/org/openapitools/api/impl/UserApiServiceImpl.java:[26,52] annotation @javax.validation.Valid not applicable in this type context

can you please take a look when you've time?

ref: https://github.com/OpenAPITools/openapi-generator/actions/runs/7262479579/job/19950668803?pr=17431

@wing328 thanks for let me know!

I briefly looked into the problem you mentioned... and it looks like io.swagger:swagger-jersey2-jaxrs:jar:1.6.5 is transitively pulling up the javax.validation:validation-api:jar:1.1.0.Final:compile, which is causing the compilation error. The pipeline may pass or fail due to fact that also in deps jakarta.validation:jakarta.validation-api:jar:2.0.2.

image

we probably can exclude the library from the dependency

    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-jersey2-jaxrs</artifactId>
      <scope>compile</scope>
      <version>${swagger-core-version}</version>
      <exclusions>
        <exclusion>
          <groupId>javax.validation</groupId>
          <artifactId>validation-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

what do you think @wing328 ?

@wing328
Copy link
Member

wing328 commented Dec 26, 2023

If it's not a lot of work, can you please file a PR with the suggested fix?

@Aliaksie
Copy link
Contributor Author

If it's not a lot of work, can you please file a PR with the suggested fix?

@wing328 sure, please have a look #17479 , hope it works and help!

@ChristianCiach
Copy link
Contributor

ChristianCiach commented Jan 8, 2024

This MR seems to break the Java native Client when useBeanValidation is used. The parameters now use @Valid annotations, but this annotation class (and other classes from the validation package) is never imported.

grafik

I think it misses this block

{{#useBeanValidation}}
import {{javaxPackage}}.validation.constraints.*;
import {{javaxPackage}}.validation.Valid;
{{/useBeanValidation}}

in https://github.com/Aliaksie/openapi-generator/blob/77497ba19267e7c7106126ba9f19aeb5101d1803/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants