OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.85k stars 6.59k forks source link

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

Open tschaffter opened 1 year ago

tschaffter commented 1 year ago

Bug Report Checklist

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 commented 1 year ago

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 commented 1 year ago

@tschaffter please try latest 6.3.0-SNAPSHOT

tschaffter commented 1 year ago

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 commented 1 year ago

@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