OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.73k stars 6.32k forks source link

[BUG][Java][Spring] Syntax error in generated API/Controller code when using example definition #3140

Open gspetrou opened 5 years ago

gspetrou commented 5 years ago

Bug Report Checklist

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.

gspetrou commented 5 years ago

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.

ghost commented 5 years ago

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?

swordmaster2k commented 4 years ago

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: '\'"

bbortt commented 1 year ago

error still exists in 6.6.0.

kingofdisasterr commented 1 month ago

error still exists in 6.6.0.

and still in 7.6.0