OpenAPITools / OpenAPITools/openapi-generator

[BUG] Version 7.7.0: Generating java.net.URLEncoder.encode(String, String) instead of the preferable one java.net.URLEncoder.encode(String, Charset)

Open
#19,370 1 comment 0 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

Description

Version 7.7.0:
Generating java.net.URLEncoder.encode(String, String) instead of the preferable one java.net.URLEncoder.encode(String, Charset)

Problem is when I am using org.gaul:modernizer-maven-plugin:2.7.0:modernizer (modernizer) on project it is forcing the build to fail as the generated code is not using the most preferable method signature i.e. java.net.URLEncoder.encode(String, Charset)

spring-boot-starter-parent version : 2.7.7
java version : 11
openapi-generator-maven-plugin version : 7.7.0
openapi-generator-maven-plugin version : 2.7.0
generatorName : java using with "resttemplate" library.

Error:
358629247-3dab7a4e-761d-4a7c-8ee2-a2471502bf8a~2

openapi-generator version

7.7.0

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Sample API
  description: A simple API example
  version: 1.0.0
paths:
  /greet:
    get:
      summary: Greet the user
      parameters:
        - name: name
          in: query
          required: true
          schema:
            type: string
      responses:
        '200':
          description: A greeting message
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    example: Hello, World!
Generation Details
                        <plugin>
				<groupId>org.openapitools</groupId>
				<artifactId>openapi-generator-maven-plugin</artifactId>
				<version>7.7.0</version>
				<executions>
					<execution>
						<id>generate</id>
						<goals>
							<goal>generate</goal>
						</goals>
						<phase>generate-sources</phase>
					</execution>
				</executions>
				<configuration>
					<inputSpec>
						${project.basedir}/src/main/resources/api-spec.yaml</inputSpec>
					<generatorName>java</generatorName>
					<output>
						${project.build.directory}/generated-sources/openapi</output>
					<apiPackage>com.ukb.openapi.demo.api</apiPackage>
					<modelPackage>com.ukb.openapi.demo.model</modelPackage>
					<invokerPackage>com.ukb.openapi.demo.invoker</invokerPackage>
					<groupId>com.ukb.openapi.demo</groupId>
					<artifactId>open-api-demo</artifactId>
					<artifactVersion>1.0.0</artifactVersion>
					<configOptions>
						<dateLibrary>java8</dateLibrary>
						<library>resttemplate</library>
						<withXml>false</withXml>
						<sourceDirectory>
							${project.build.directory}/generated-sources/openapi/src/main/java</sourceDirectory>
					</configOptions>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.gaul</groupId>
				<artifactId>modernizer-maven-plugin</artifactId>
				<version>2.7.0</version>
				<configuration>
					<javaVersion>11</javaVersion>
				</configuration>
				<executions>
					<execution>
						<id>modernizer</id>
						<phase>verify</phase>
						<goals>
							<goal>modernizer</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
Steps to reproduce

Prepare a simple springboot project using the pom.xml and api-spec.yaml and run
mvn clean install

Error:
358629247-3dab7a4e-761d-4a7c-8ee2-a2471502bf8a~2

.zip file of the project is also included as attachment.

open-api-demo.zip

Suggest a fix
  1. Generate the code with corresponding active method signature. i.e. java.net.URLEncoder.encode(String, Charset) for java 11.

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

Start with the pom.xml and api-spec.yaml from the reproduction project, then run mvn clean install to inspect the generated Java client and the modernizer failure. Trace the generated URL-encoding call and verify that the output uses java.net.URLEncoder.encode(String, Charset) for Java 11 and passes the modernizer check.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend-api-design, 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.