OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Invalid code generated: non-existent ApiClient is referred in toUrlQueryString

Open
#19,142 15 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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 ApiClient which is not generated.
  • The generated model class has a function toUrlQueryString(String) that uses a static function valueToString from the class ApiClient which 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
  1. save the file above as src/main/openapi/demo.yaml
  2. add the above plugin configuration to a maven pom
  3. run mvn generate-sources
  4. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.