Skip to content

Conversation

@Zomzog
Copy link
Contributor

@Zomzog Zomzog commented Sep 4, 2022

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

  • 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/utils/export_docs_generators.sh
    
    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*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (6.1.0) (minor release - breaking changes with fallbacks), 7.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.

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)

Copy link
Contributor

Choose a reason for hiding this comment

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

Possible to avoid * ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the template

Copy link
Contributor

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?

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've added some code to check if there is an enum in the models.

@welshm
Copy link
Contributor

welshm commented Sep 7, 2022

@poulad FYI - might be of interest to you

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.

Change looks great! Some generated code I wouldn't expect to see...

Copy link
Contributor

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

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 removed them.

Copy link
Contributor

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...

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've rerurun all generators. I've fixed it by restarting from master.

@Zomzog Zomzog closed this Sep 13, 2022
@Zomzog Zomzog force-pushed the zomzog/12874_enum_converter branch from 14aa3fd to 8e66294 Compare September 13, 2022 07:12
@Zomzog Zomzog reopened this Sep 13, 2022
}
}

private boolean containsEnums() {
Copy link
Contributor

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());
      }

Copy link
Contributor

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

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'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 {
Copy link
Contributor

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

Copy link
Contributor

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

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'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;
Copy link
Member

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;

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 change for {{configPackage}}

return new Converter<String, {{name}}>() {
@Override
public {{name}} convert(String source) {
return {{name}}.fromValue(source);
Copy link
Member

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

public static {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{dataType}}} value) {

Copy link
Contributor Author

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() {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's changed.

@Zomzog
Copy link
Contributor Author

Zomzog commented Oct 25, 2022

Any chance it can be merged?

@sagarsarwe
Copy link

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

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.

[BUG] Java Generator for Enum produces invalid fromValue method

6 participants