Open kinlhp opened 1 year ago
@kinlhp don't get me wrong but this is slightly to complex spec to analyze :sweat_smile: Can you provide smaller/shorter spec + generation configuration so that we would be able to reproduce this issue?
No problem, I just added the smallest details in case someone needs to have my exact scenario.
The problem, from the little I've analyzed, is that when there is more than one response data structure in the same external .yaml
file, the generated Java code is not correct.
For example, I have the following two paths in the same tag:
paths:
/v1/countries:
#
# Escaped forward-slash is necessary when using JSON references.
# https://spec.openapis.org/oas/latest.html#operationref-examples
$ref: 'components/pathItems/v1/countries.yaml#/getCountries'
/v1/countries/{numeric-code}:
#
# Escaped forward-slash is necessary when using JSON references.
# https://spec.openapis.org/oas/latest.html#operationref-examples
$ref: 'components/pathItems/v1/countries.yaml#/getCountry'
tags:
- name: countries
description: Countries resource related.
The expected code is:
/**
* GET /v1/countries : GET Countries.
* ...
*/
default ResponseEntity<CountriesResponseDTO> getCountries(Integer pageIndex, Integer pageSize) throws Exception {/* ... */}
/**
* GET /v1/countries/{numeric-code} : GET Country.
* ...
*/
default ResponseEntity<CountryResponseDTO> getCountry(Integer numericCode) throws Exception {/* ... */}
But the generated code is:
/**
* GET /v1/countries : GET Countries.
* ...
*/
default ResponseEntity<CountriesResponseDTO> getCountries(Integer pageIndex, Integer pageSize) throws Exception {/* ... */}
/**
* GET /v1/countries/{numeric-code} : GET Country.
* ...
*/
default ResponseEntity<CountriesResponseDTO> getCountry(Integer numericCode) throws Exception {/* ... */}
As a plugin configuration, I have the following defined:
<!-- openapitools -->
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<id>default-generate</id>
<phase>${openapi-generator.phase}</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!-- README#openapi-generator-spring-metadata -->
<generatorName>spring</generatorName>
<!-- README#openapi-generator-spring-config-options -->
<apiPackage>${openapi-generator.base-package}.endpoint</apiPackage>
<artifactId>${project.artifactId}</artifactId>
<artifactVersion>${revision}</artifactVersion>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<groupId>${project.groupId}</groupId>
<ignoreFileOverride>.openapi-generator-ignore</ignoreFileOverride>
<inputSpec>${openapi-generator.input-spec}</inputSpec>
<invokerPackage>${openapi-generator.base-package}.invoker</invokerPackage>
<modelNameSuffix>DTO</modelNameSuffix>
<modelPackage>${openapi-generator.base-package}.payload</modelPackage>
<configOptions>
<!-- README#openapi-generator-spring-metadata -->
<generatorLanguage>Java</generatorLanguage>
<generatorType>SERVER</generatorType>
<!-- README#openapi-generator-spring-config-options -->
<artifactDescription>${project.description}</artifactDescription>
<artifactUrl>${project.url}</artifactUrl>
<basePackage>${openapi-generator.base-package}</basePackage>
<bigDecimalAsString>true</bigDecimalAsString>
<booleanGetterPrefix>is</booleanGetterPrefix>
<configPackage>${openapi-generator.base-package}.configuration</configPackage>
<delegatePattern>true</delegatePattern>
<developerEmail>lhp.kin@gmail.com</developerEmail>
<developerName>Luis Henrique Pereira</developerName>
<developerOrganization>${project.organization.name}</developerOrganization>
<developerOrganizationUrl>${project.organization.url}</developerOrganizationUrl>
<licenseName>${license.name}</licenseName>
<licenseUrl>${license.url}</licenseUrl>
<parentArtifactId>${project.parent.artifactId}</parentArtifactId>
<parentGroupId>${project.parent.groupId}</parentGroupId>
<parentVersion>${revision}</parentVersion>
<performBeanValidation>true</performBeanValidation>
<scmConnection>${project.scm.connection}</scmConnection>
<scmDeveloperConnection>${project.scm.developerConnection}</scmDeveloperConnection>
<scmUrl>${project.scm.url}</scmUrl>
<serializableModel>true</serializableModel>
<title>${project.description}</title>
<unhandledException>true</unhandledException>
<useSpringBoot3>true</useSpringBoot3>
<useSpringController>true</useSpringController>
<useTags>true</useTags>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Description
As a learning lab, I have this repository _(and until the merge happens, the code can be found in the branch feature/wip_country-api)_. In it I am exhaustively using the
$ref:
parameter in every possible place.Why am I doing this? Simply because the specification says it is possible.
Apparently there must be some cache that considers the tag and replicates the response type regardless of the endpoints contained in it.
openapi-generator version
org.openapitools:openapi-generator-maven-plugin:6.0.0
and with the latest master by building the JAR locally to see if the issue has already been addressed.OpenAPI declaration file content or url
NOTE!: The comments # Open-API generation issue. are related to another type of path resolution bug.
Command line used for generation
Steps to reproduce
Expected behavior
One endpoint with correct
default ResponseEntity<CountriesResponseDTO> getCountries(Integer pageIndex, Integer pageSize)
signature and another endpoint with correctdefault ResponseEntity<CountryResponseDTO> getCountry(Integer numericCode)
signature:Observed behavior
One endpoint with correct
default ResponseEntity<CountriesResponseDTO> getCountries(Integer pageIndex, Integer pageSize)
signature and another endpoint with wrongdefault ResponseEntity<CountriesResponseDTO> getCountry(Integer numericCode)
signature:Aditional observation
If only one of the endpoints is specified the expected behavior is the desired one. I.e:
or:
NOTE!: The same behavior of Java classes is also observed in the canonical specification.
Related issues/PRs
I looked for issues and PRs related to this behavior but found nothing.
Suggest a fix/enhancement