OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Invalid code generated: non-existent ApiClient is referred in toUrlQueryString
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
After upgrading from v7.6.0 to v7.7.0, we have noticed that the generated code is invalid.
- The generated model class has an import of a class
ApiClientwhich is not generated. - The generated model class has a function
toUrlQueryString(String)that uses a static functionvalueToStringfrom the classApiClientwhich is not generated.
The old code was
joiner.add(String.format("%sfoo%s=%s", prefix, suffix, URLEncoder.encode(String.valueOf(getFoo()), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
The new code is
joiner.add(String.format("%sfoo%s=%s", prefix, suffix, URLEncoder.encode(ApiClient.valueToString(getFoo()), StandardCharsets.UTF_8).replaceAll("\\+", "%20")));
openapi-generator version
7.7.0
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Example API v1
description: A reference API
contact: {}
version: 2.0.0
paths:
/demo:
get:
responses:
'200':
description: Test
content:
'application/json':
schema:
type: object
properties:
foo:
type: string
Generation Details
language: java
library: native
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.7.0</version>
<configuration>
<generatorName>java</generatorName>
<skipValidateSpec>false</skipValidateSpec>
<generateApis>false</generateApis>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<output>${project.build.directory}/generated-sources</output>
<configOptions>
<sourceFolder>openapi</sourceFolder>
<useJakartaEe>true</useJakartaEe>
<withXml>true</withXml>
<serializationLibrary>jackson</serializationLibrary>
<library>native</library>
<dateLibrary>java8</dateLibrary>
<disallowAdditionalPropertiesIfNotPresent>false</disallowAdditionalPropertiesIfNotPresent>
</configOptions>
</configuration>
<executions>
<execution>
<id>demo</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<inputSpec>src/main/openapi/demo.yaml</inputSpec>
<configOptions>
<modelPackage>de.demo</modelPackage>
</configOptions>
</configuration>
</execution>
</executions>
Steps to reproduce
- save the file above as
src/main/openapi/demo.yaml - add the above plugin configuration to a maven pom
- run
mvn generate-sources - check
target/generated-sources/openapi/de/demo/DemoGet200Response.java
Related issues/PRs
seems to be a regression from #18743
https://github.com/OpenAPITools/openapi-generator/pull/18743/files#diff-2eefc7a98c8e6aff3fa4589d8f2c364e7214c35e98806786e8dae207c42352df
Workaround
Set supportUrlQuery to false when toUrlQueryString is not needed.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the regression with the supplied demo.yaml and Maven configuration by running mvn generate-sources, then inspect the Java generator changes associated with PR #18743 and supportUrlQuery. Done means the generated DemoGet200Response.java no longer imports or calls the non-existent ApiClient when support files are disabled, while preserving the intended URL-query behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100