openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][SPRING] Example string generation does not compile when using "$ref"

Open benfonty opened this issue 4 years ago • 2 comments

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] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating "spring" code for an endpoint that returns an array and the object definition is a "$ref" and the object definition has an example, the generated code contains a string that doesn't compile. The backslashes of the example are wrongly escaped.

openapi-generator version

5.2.0

OpenAPI declaration file content or url

https://gist.github.com/benfonty/98c445d969b84896019d6c09bcb25ccd

Generation Details

openapi-generator generate -i test-api.json -g spring -o ./generated

Steps to reproduce

openapi-generator generate -i test-api.json -g spring -o ./generated

Try to build the generated code.

There is an error in ./generated/src/main/java/org/openapitools/api/TestApi.java.

String exampleString = "{ \"adminUser\" : \"{\\"userName\\":\\"[email protected]\\"}\", \"id\" : \"id\" }";

benfonty avatar Jul 19 '21 09:07 benfonty

The issue is still present in v6.2.0, can someone have a look please ?

slerouvillois avatar Sep 27 '22 07:09 slerouvillois

This issue is still present in v7.6.0

With double quotes in the json example in the openapi yaml:

    items:
      type: array
      items:
        $ref: '#/components/schemas/item'
      example: |
        [
          {
            "code": "0018",
            "name": "Hefshuizen",
            "start": "01-01-1979",
            "end": "01-01-1992"
          }
        ]

will results in a Java file which does not complie. illegal character: '\'. There is should be one \ to escape the double quote.

String exampleString = "\"[\n {\n \\"code\\": \\"0017\\",\n \\"name\\": \\"Hefshuizen\\",\n \\"start\\": \\"01-01-1979\\",\n \\"end\\": \\"01-01-1992\\"\n }\n]\n\"";

Removing the doubel quotes in the openapi yaml

    items:
      type: array
      items:
        $ref: '#/components/schemas/item'
      example: |
        [
          {
            code: 0018,
            name: Hefshuizen,
            start: 01-01-1979,
            end: 01-01-1992
          }
        ]

could be a workaround

String exampleString = "\"[\n {\n code: 0018,\n name: Hefshuizen,\n start: 01-01-1979,\n end: 01-01-1992\n }\n]\n\"";

erik-meuwese-topicus avatar May 21 '24 00:05 erik-meuwese-topicus