-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[SPRING] Add converters for enums #13349
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
[SPRING] Add converters for enums #13349
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.
Possible to avoid * ?
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.
Fixed in the template
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 will create an empty configuration file if there are no enums. Might be confusing to users who generate the project and then don't understand why this file exists - maybe conditionally add this file if there are any enums present?
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've added some code to check if there is an enum in the models.
|
@poulad FYI - might be of interest to you |
welshm
left a comment
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.
Change looks great! Some generated code I wouldn't expect to see...
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.
Your IDE seems to have added a lot of auto-formatting - you may want to disable that and/or revert that. Makes it hard to review
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 removed them.
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.
Is this generated? I don't see why your PR would be creating this...
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've rerurun all generators. I've fixed it by restarting from master.
14aa3fd to
8e66294
Compare
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java
Outdated
Show resolved
Hide resolved
| } | ||
| } | ||
|
|
||
| private boolean containsEnums() { |
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.
Proposal for more better readability. WDYT?
private boolean containsEnum() {
if (openAPI == null) {
return false;
}
Components components = openAPI.getComponents();
if (components == null || components.getSchemas() == null) {
return false;
}
return components.anyMatch(component -> component.getEnum() != null && !component.getEnum().isEmpty());
}
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.
Also, further thinking about this, would be good to add unit tests 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.
I've added a test, but it's more a test-by-side-effect with the file creation.
| import org.springframework.core.convert.converter.Converter; | ||
|
|
||
| @Configuration | ||
| public class EnumConverters { |
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 would rename it to EnumConverterConfiguration
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.
Additional the config is just working if you import it manually, activate component scan or create a auto config file. Documentation would be good, what is expected to work
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've changed the name.
What kind of documentation is expected? I suppose the scan is done as it is done for others @Configurationno?
| @@ -0,0 +1,34 @@ | |||
| package org.openapitools.configuration; | |||
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 this has to be smth like package {{package}}.configuration;
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 change for {{configPackage}}
| return new Converter<String, {{name}}>() { | ||
| @Override | ||
| public {{name}} convert(String source) { | ||
| return {{name}}.fromValue(source); |
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.
enums can have other data type except String. Note {{dataType}} for fromValu param
openapi-generator/modules/openapi-generator/src/main/resources/JavaSpring/enumClass.mustache
Line 42 in f3fa675
| public static {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{dataType}}} value) { |
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.
Ok, I've done the change.
| public class EnumConverterConfiguration { | ||
|
|
||
| @Bean | ||
| Converter<String, EnumClass> EnumClassConverter() { |
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.
nit - would expect method name to start with lowercase for Java convention
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.
It's changed.
|
Any chance it can be merged? |
|
Did this fix the issue #12874 for only springboot project or non-spring boot (java spring) project as well because I am using a non spring boot project and still facing the issue |
We need to add converters because spring doesn't use jackson for path and query params (cf spring-projects/spring-boot#24233 )
This will fix #12874
PR checklist
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*.For Windows users, please run the script in Git BASH.
master(6.1.0) (minor release - breaking changes with fallbacks),7.0.x(breaking changes without fallbacks)Spring committee : @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02)