swagger-api / swagger-api/swagger-codegen

[C# DOTNET] Incorrect example JSON generated for aspnetcore server stub

Open
#10,477 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The generated exampleJson for response body is incorrectly escaped if an "example" tag is provided on the property level, when using the .NET Core 3.0 generated code for server stubs.

The API spec:

      responses:
        '200':
          description: >
            All good
          content:
            application/json:
              schema:
                type: object
                
                properties:
                  pv-installation:
                    type: string
                  links:
                    type: object
                    example: 
                      economics: 
                        href: >-
                          /pvInstallation/economics?roof-size=100&irradiation=1234&tilt=45&azimuth=274
                    properties:
                      economics:
                        $ref: '#/components/schemas/Link'

will produce an exampleJson with incorrect escaped quotes in the controller (DefaultAPI.cs) which does not deserialize :

exampleJson = "{\n  \"links\" : \"{\\"economics\\":{\\"href\\":\\"/pvInstallation/economics?roof-size=100&irradiation=1234&tilt=45&azimuth=274\\"}}\",\n  \"pv-installation\" : \"pv-installation\"\n}";

By contrast, it works nicely if the example is provided for all properties on schema level, such as:

      responses:
        '200':
          description: >
            All good
          content:
            application/json:
              schema:
                type: object
                example: 
                  pv-installation: Tjoho
                  economics: 
                    href: >-
                      /pvInstallation/economics?roof-size=100&irradiation=1234&tilt=45&azimuth=274
            
                properties:
                  pv-installation:
                    type: string
                  links:
                    type: object

                    properties:
                      economics:
                        $ref: '#/components/schemas/Link'

or for each property in the property definition.
Both these options will produce a correct exampleJson of:

exampleJson = "{\n  \"pv-installation\" : \"Tjoho\",\n  \"economics\" : {\n    \"href\" : \"/pvInstallation/economics?roof-size=100&irradiation=1234&tilt=45&azimuth=274\"\n  }\n}";
Swagger-codegen version

3.0.21 via swaggerhub

Swagger declaration file content or url

Open API 3.0 specification for reproduction: https://gist.github.com/AndersAko/76c97f7baca0693fbb8298a1d8e3206d

Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

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

Use the linked OpenAPI 3.0 reproduction and inspect the generated DefaultAPI.cs for the response body's exampleJson. Compare output from a property-level example with the working schema-level examples, then verify that the generated JSON no longer contains incorrectly escaped nested quotes and can be deserialized.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.