[SPRING] Add converters for enums#13349
Conversation
There was a problem hiding this comment.
Fixed in the template
There was a problem hiding this comment.
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.
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.
Change looks great! Some generated code I wouldn't expect to see...
There was a problem hiding this comment.
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.
Is this generated? I don't see why your PR would be creating this...
There was a problem hiding this comment.
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.
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.
Also, further thinking about this, would be good to add unit tests for this method
There was a problem hiding this comment.
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.
I would rename it to EnumConverterConfiguration
There was a problem hiding this comment.
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.
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.
probably this has to be smth like package {{package}}.configuration;
There was a problem hiding this comment.
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.
enums can have other data type except String. Note {{dataType}} for fromValu param
There was a problem hiding this comment.
Ok, I've done the change.
| public class EnumConverterConfiguration { | ||
|
|
||
| @Bean | ||
| Converter<String, EnumClass> EnumClassConverter() { |
There was a problem hiding this comment.
nit - would expect method name to start with lowercase for Java convention
|
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)