openapi-generator
openapi-generator copied to clipboard
[BUG][Java][Spring] Syntax error in generated API/Controller code when using example definition
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [x] What's the version of OpenAPI Generator used?
- [x] Have you search for related issues/PRs?
- [x] 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.
Looking into this issue more we found the quote handling being done in this lambda function. We inserted a break point and noticed that the string coming into the method already has its double quotes properly escaped
"{\"testname\":\"amy\"}"
We believe this is part of the issue as the line linked above is then escaping the quotations again.
So we think we have a solution, but the problem . is that there is no tests for the code, and when we try to create tests, it's never hitting the bad code. We don't know how to cause the code we want to test to actually run.
Is there anyone out there familiar enough with the code who can tell us what we need to do to actually generate the API/Controller code?
I too have run into this problem, and am unable to use the "example" section in my API definition as the generated code doesn't have the correct escaping on the strings.
I tried using both 4.0.2 and the latest 4.1.0 versions, remains present in newer versions.
Leads to ERRORs in my Maven build with "illegal character: '\'"
error still exists in 6.6.0.
error still exists in 6.6.0.
and still in 7.6.0
7.12.0 doesn't seem to be affected by this since it does not seem to include examples in the generated code. 7.13.0 reintroduces this issue.
@AdrianDiemerDev thanks for the additional info.
Do you mind doing a git bisect to identify which commit reintroducing the issue when you've time?
@wing328 I didn't do a git bisect but going through the commit messages between 7.12 and 7.13, I think this is pretty much the only candidate: https://github.com/OpenAPITools/openapi-generator/commit/9f3a73f81a71d5386a93ac02f25c2be35f8f7b45
I don't think this helps a lot though. 7.12 was just coincidentally working since it had an additional issue that caused examples to be omitted from the generated code. This was fixed by the mentioned commit.
With the examples included in the code again, the escaping issue returned.