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

[BUG][JAVA][SPRING] EnumConverterConfiguration does not use modelNameSuffix #14302

Open
5 of 6 tasks
tschaffter opened this issue Dec 20, 2022 · 4 comments
Open
5 of 6 tasks

Comments

@tschaffter
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

OpenAPI generator generate the file EnumConverterConfiguration.java if the OA description includes at least one enum. The template does not take into account the generator configuration property modelNameSuffix, which is used to name the DTO models. The issue is then that the class EnumConverterConfiguration includes incorrect references to the DTO models.

openapi-generator version

6.2.1

OpenAPI declaration file content or url

See this OpenAPI description (note the use of enum)

Generation Details

Content of EnumConverterConfiguration.java (here the value of modelNameSuffix is not used to name the models):

package org.sagebionetworks.challenge.configuration;

import org.sagebionetworks.challenge.model.dto.ChallengeStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

@Configuration
public class EnumConverterConfiguration {

  @Bean
  Converter<String, ChallengeStatus> challengeStatusConverter() {
    return new Converter<String, ChallengeStatus>() {
      @Override
      public ChallengeStatus convert(String source) {
        return ChallengeStatus.fromValue(source);
      }
    };
  }
}

List of DTO models generated (here the value of modelNameSuffix is correctly used to name the models):

$ ls -ahl apps/challenge-service/src/main/java/org/sagebionetworks/challenge/model/dto/
total 40K
drwxr-xr-x 2 vscode vscode  219 Dec 20 22:06 .
drwxr-xr-x 7 vscode vscode   75 Dec 19 19:36 ..
-rw-r--r-- 1 vscode vscode 3.8K Dec 20 20:15 BasicErrorDto.java
-rw-r--r-- 1 vscode vscode 3.3K Dec 20 20:15 ChallengeCreateRequestDto.java
-rw-r--r-- 1 vscode vscode 1.1K Dec 20 20:15 ChallengeDifficultyDto.java
-rw-r--r-- 1 vscode vscode 5.1K Dec 20 20:15 ChallengeDto.java
-rw-r--r-- 1 vscode vscode  986 Dec 20 20:15 ChallengeStatusDto.java
-rw-r--r-- 1 vscode vscode 6.4K Dec 20 20:15 ChallengesPageDto.java
-rw-r--r-- 1 vscode vscode 5.4K Dec 20 20:15 PageMetadataDto.java
Steps to reproduce
  1. Download the OA description (see above)
  2. Download the OA generator config file (see above)
  3. Run the OA generator: openapi-generator-cli generate
Related issues/PRs
Suggest a fix
@tschaffter
Copy link
Author

A solution is to update the template converter.mustache to replace {{name}} by {{classname}}.

Before

package {{configPackage}};

{{#models}}
    {{#model}}
        {{#isEnum}}
import {{modelPackage}}.{{name}};
        {{/isEnum}}
    {{/model}}
{{/models}}

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

@Configuration
public class EnumConverterConfiguration {

{{#models}}
{{#model}}
{{#isEnum}}
    @Bean
    Converter<{{{dataType}}}, {{name}}> {{classVarName}}Converter() {
        return new Converter<{{{dataType}}}, {{name}}>() {
            @Override
            public {{name}} convert({{{dataType}}} source) {
                return {{name}}.fromValue(source);
            }
        };
    }
{{/isEnum}}
{{/model}}
{{/models}}

}

After

package {{configPackage}};

{{#models}}
    {{#model}}
        {{#isEnum}}
import {{modelPackage}}.{{classname}};
        {{/isEnum}}
    {{/model}}
{{/models}}

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

@Configuration
public class EnumConverterConfiguration {

{{#models}}
{{#model}}
{{#isEnum}}
    @Bean
    Converter<{{{dataType}}}, {{classname}}> {{classVarName}}Converter() {
        return new Converter<{{{dataType}}}, {{classname}}>() {
            @Override
            public {{classname}} convert({{{dataType}}} source) {
                return {{classname}}.fromValue(source);
            }
        };
    }
{{/isEnum}}
{{/model}}
{{/models}}

}

@borsch
Copy link
Member

borsch commented Jan 13, 2023

@tschaffter please try latest 6.3.0-SNAPSHOT

@tschaffter
Copy link
Author

It doesn't look like the snapshot version is on Maven:

$ nx openapi-generate openchallenges-organization-service

> nx run openchallenges-organization-service:openapi-generate

Download 6.3.0-SNAPSHOT ...
Download failed, because of: "Request failed with status code 404"

Response:
connection close
content-length 554
last-modified Wed, 10 Aug 2016 15:08:35 GMT
etag "1fb066da6a67c7c02962f59b4b8cd1ee"
x-amz-error-code NoSuchKey
x-amz-error-message The specified key does not exist.
x-amz-error-detail-key maven2/org/openapitools/openapi-generator-cli/6.3.0-SNAPSHOT/openapi-generator-cli-6.3.0-SNAPSHOT.jar
content-type text/html
accept-ranges bytes
date Wed, 01 Feb 2023 01:05:03 GMT
via 1.1 varnish
age 2634770
x-served-by cache-iad-kjyo7100174-IAD
x-cache HIT
x-cache-hits 1
x-timer S1675213503.486762,VS0,VE2
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
/workspaces/sage-monorepo/node_modules/@openapitools/openapi-generator-cli/main.js:685
                error ? reject(new Error(stderr)) : resolve(stdout);
                               ^
Error: Error: Unable to access jarfile /workspaces/sage-monorepo/node_modules/@openapitools/openapi-generator-cli/versions/6.3.0-SNAPSHOT.jar
    at /workspaces/sage-monorepo/node_modules/@openapitools/openapi-generator-cli/main.js:685:32
    at ChildProcess.exithandler (node:child_process:427:5)
    at ChildProcess.emit (node:events:513:28)
    at maybeClose (node:internal/child_process:1091:16)
    at Socket.<anonymous> (node:internal/child_process:449:11)
    at Socket.emit (node:events:513:28)
    at Pipe.<anonymous> (node:net:320:12)
Node.js v18.13.0

@borsch
Copy link
Member

borsch commented Feb 1, 2023

@tschaffter 6.3.0 is not released yet & 6.3.0-SNAPSHOT is not available on maven central. Seems like CLI is now aware about snapshot versions

6.3.0-SNAPSHOT can be taken from here https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/6.3.0-SNAPSHOT/
(information taken from https://github.com/OpenAPITools/openapi-generator#11---compatibility)

Seems like CLI has an option to customize maven repository which might help you. Check this please https://github.com/OpenAPITools/openapi-generator-cli#using-custom--private-maven-registry

Note: I haven't tried this, so might not work

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

No branches or pull requests

2 participants