swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.08k stars 8.88k forks source link

"Maximum call stack size exceeded" in SwaggerEditor's "Generate Client > html2" and "File > Download Resolved YAML" functions. #9761

Open jorgemk85 opened 4 months ago

jorgemk85 commented 4 months ago

Q&A (please complete the following information)

Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.0.1
info:
  title: Test API
  version: v1
paths:
  /v{version}/projects/{projectId}/compounds:
    get:
      tags:
        - Compounds
      summary: Get all compounds.
      operationId: GetCompounds
      parameters:
        - name: version
          in: path
          description: 'API version (e.g.: 1)'
          required: true
          schema:
            type: string
        - name: projectId
          in: path
          description: The project id
          required: true
          schema:
            type: integer
            format: int32
          example: 15
      responses:
        '200':
          description: Success
          content:
            text/plain:
              schema:
                $ref: '#/components/schemas/ArrayJobDto'
            application/json:
              schema:
                $ref: '#/components/schemas/ArrayJobDto'
            text/json:
              schema:
                $ref: '#/components/schemas/ArrayJobDto'
        '202':
          description: Accepted
          content:
            text/plain:
              schema:
                $ref: '#/components/schemas/ArrayJobDto'
            application/json:
              schema:
                $ref: '#/components/schemas/ArrayJobDto'
            text/json:
              schema:
                $ref: '#/components/schemas/ArrayJobDto'
components:
  schemas:
    ArrayJobDto:
      type: object
      properties:
        id:
          type: string
          format: uuid
        status:
          allOf:
            - $ref: '#/components/schemas/ArrayJobDto'
        error:
          allOf:
            - $ref: '#/components/schemas/ArrayJobDto'
          nullable: true
        payload:
          type: array
          items:
            $ref: '#/components/schemas/ArrayJobDto'
          nullable: true
      additionalProperties: false

Swagger-UI configuration options:

N/A
N/A

Describe the bug you're encountering

Generator Client for "html2" is failing to render the schemas when they have circular references. Also, SwaggerEditor fails when trying to "Download Resolved JSON/YAML". The error shown is "Maximum call stack size exceeded".

To reproduce...

Steps to reproduce the behavior: Scenario 1:

  1. Go to https://editor-next.swagger.io/ or https://editor.swagger.io/
  2. Paste the provided OpenAPI spec content in the corresponding area of the editor.
  3. Click on "Generate Client > html2" to trigger the download.
  4. Save the zip file in your computer and extract.
  5. Open index.html file.
  6. Open browser's console to observe the errors.

Scenario 2:

  1. Go to https://editor-next.swagger.io/ or https://editor.swagger.io/
  2. Paste the provided OpenAPI spec content in the corresponding area of the editor.
  3. Click on "File > Download Resolved YAML".
  4. Observe the error thrown by the web app.

Expected behavior

Scenario 1: Browser's console doesn't display any "Maximum call stack size exceeded" errors, meaning that it processes Circular References correctly and schemas are rendered properly. (Consider using a dictionary of the visited nodes).

Scenario 2: Browser throws a file to download that includes the resolved YAML file.

Screenshots

Scenario 1: image image

Scenario 2: image

Additional context or thoughts

A similar problemas was resolved in Issue #8537, but they exclusively impacted the Editor's UI and the issue persists in (at least) this other functions. @char0n your input is greatly appreciated. The provided spec file was created specifically to replicate this issue.

char0n commented 3 months ago

Hi @jorgemk85,

Your definition contains cycles and even though https://editor-next.swagger.io/ (SwaggerEditor@5) is based on ApiDOM which can handle cycles easily, we still seeing that error.

The issue is that for resolving the definitions, we currently use dereference algorithm (reference removal). Dereferencing can create cycles (circular references) and cycles cannot be serialized into JSON or YAML. In order to resolve this issue properly we need to use bundling algorithm. We've already started some initial work on bundling in https://github.com/swagger-api/apidom/issues/692.

What we can do for now to mitigate the issue (before we have bundling) is to run the dereferencing in replace mode and use circularReplacer to make the circular reference absolute and skip resolving it.

jorgemk85 commented 3 months ago

Hello @char0n,

Thank you for your reply.

I am using this "https://generator3.swagger.io/index.html#/clients/generate" endpoint to procedurally transform my spec files into "html2" static websites. Could you please indicate how to specify the dereferencing property in the request body? I have tried the following without making any difference:


{
   "lang":"html2",
   "spec":"...",
   "specURL":null,
   "type":"CLIENT",
   "codegenVersion":"V3",
   "options":{
      "dereference":{
         "circular":"replace"
      }
   }
}