OpenAPITools / OpenAPITools/openapi-generator
[BUG][Java][Spring] Syntax error in generated API/Controller code when using example definition
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)?
- What's the version of OpenAPI Generator used?
- Have you search for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Bounty to sponsor the fix (example)
Description
We are using OpenAPI and openapi-generator-maven-plugin with Spring Boot. We're trying to create an example object in our response. When the API/Controller code given is generated, it contains the syntax error shown below.
openapi-generator version
4.0.0
OpenAPI declaration file content or url
openapi: 3.0.2
info:
title: Flex PLM ACL API
description: MDM color app to call the Flex PLM ACL API.
version: 1.0.0
servers:
- url: https://flexplm-acl-dev.apps.an01.pcf.dcsg.com/api/v1
paths:
'/colors/{season}':
parameters:
- name: season
description: season ID
required: true
in: path
schema:
type: string
patch:
tags:
- Color
operationId: testing
description: test
responses:
'200':
description: Return existing colors in a season
content:
application/json:
schema:
type: object
properties:
testName:
type: string
example: # <--- Lines leading to error
testName: amy
Command line used for generation
A Maven Plugin was used:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.0.0</version>
<executions>
<execution>
<id>FlexPLM-ACL</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/apis/flexplm-acl.yml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>com.dcsg.pdcomm.flexplmacl.api</apiPackage>
<!-- Both of these APIs use the exact same DTOs, so we have put them into the same package -->
<modelPackage>com.dcsg.pdcomm.flexplm.dto</modelPackage>
<modelNameSuffix>DTO</modelNameSuffix>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
<configOptions>
<library>spring-boot</library>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
<interfaceOnly>true</interfaceOnly>
</configOptions>
<output>${project.build.directory}/generated-sources</output>
</configuration>
</execution>
</executions>
</plugin>
Steps to reproduce
The following is the generated code in the Controller/API. Notice the escaped backslash rather than the expected escaped quotation output.
// Given output
ApiUtil.setExampleResponse(request, "application/json", "\"{\\"testname\\":\\"amy\\"}\"");
// Expected output
ApiUtil.setExampleResponse(request, "application/json", "\"{\"testname\":\"amy\"}\"");
Related issues/PRs
None that we are aware of.
Suggest a fix
We will investigate further and come back to edit this issue.
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
Start by reproducing the issue with the provided OpenAPI declaration and the openapi-generator Maven Plugin using the Spring generator and interfaceOnly option. Inspect how the example response is converted into generated API/Controller Java code, then verify the generated source compiles and uses the expected escaping shown in the report.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100