-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[4947][java]: adds support for validation of primitives in arrays #17165
Conversation
bf53b15
to
b5cdc5d
Compare
ce7e022
to
c668a11
Compare
There was a problem hiding this 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.
...generator/src/test/java/org/openapitools/codegen/java/JavaValidationArrayPrimitivesTest.java
Outdated
Show resolved
Hide resolved
...generator/src/test/java/org/openapitools/codegen/java/JavaValidationArrayPrimitivesTest.java
Outdated
Show resolved
Hide resolved
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
Outdated
Show resolved
Hide resolved
.../openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Outdated
Show resolved
Hide resolved
LGTM. |
@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*)*+", ""); | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; | |||
} | |||
|
There was a problem hiding this comment.
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)
There was a problem hiding this 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()); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checking now
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
just merged the PR. thanks again for the contribution. |
@Aliaksie just noticed that this PR seems to break the CI tests. In
can you please take a look when you've time? |
@wing328 thanks for let me know! I briefly looked into the problem you mentioned... and it looks like we probably can exclude the library from the dependency
what do you think @wing328 ? |
If it's not a lot of work, can you please file a PR with the suggested fix? |
This MR seems to break the Java I think it misses this block {{#useBeanValidation}}
import {{javaxPackage}}.validation.constraints.*;
import {{javaxPackage}}.validation.Valid;
{{/useBeanValidation}} |
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
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.
master
(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)@wing328 @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg