swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Refs from separate files are not cached

Open grinspins opened this issue 1 year ago • 1 comments

Q&A (please complete the following information)

  • OS: Windows 10
  • Browser: chrome
  • Version: 119.0.6045.160
  • Method of installation: CDN
  • Swagger-UI version: 5.10.0
  • Swagger/OpenAPI version: OpenAPI 3.1

Content & configuration

Example Swagger/OpenAPI definition: Spec folder: spec/ ├─ openapi.yml ├─ tray.yml ├─ common.yml

# openapi.yml
openapi: 3.1.0
servers:
  - description: MyAPI
    url: /api
info:
  description: My Api
  version: "1.0.0"
  title: My Api
  contact:
    email: [email protected]
tags:
  - name: tray
    description: Tray services

  /v1/tray:
    $ref: "tray.yml#/tray"
  /v1/tray/{tray_id}/note:
    $ref: "tray.yml#/trayNote"
  /v1/tray/{tray_id}/history:
    $ref: "tray.yml#/trayHistory"
  /v1/tray/{tray_id}:
    $ref: "tray.yml#/trayById"
# tray.yml
tray:
  post:
    tags:
      - tray
    responses:
      '400':
        description: tray failed validation
        content:
          application/json:
            schema:
              $ref: 'common.yml#/schemas/Exception'

trayById:
  get:
    tags:
      - tray
    parameters:
      - $ref: "tray.yml#/parameters/TrayId"
    responses:
      '400':
        description: Bad request.
        content:
          application/json:
            schema:
              $ref: 'common.yml#/schemas/Exception'
  put:
    tags:
      - tray
    parameters:
      - $ref: "tray.yml#/parameters/TrayId"
    responses:
      '400':
        description: tray failed validation
        content:
          application/json:
            schema:
              $ref: 'common.yml#/schemas/Exception'
  delete:
    tags:
      - tray
    parameters:
      - $ref: "tray.yml#/parameters/TrayId"
    responses:
      '400':
        description: Bad request.
        content:
          application/json:
            schema:
              $ref: 'common.yml#/schemas/Exception'

trayNote:
  get:
    tags:
      - tray
    parameters:
      - $ref: "tray.yml#/parameters/TrayId"
    responses:
      '400':
        description: Bad request.
        content:
          application/json:
            schema:
              $ref: 'common.yml#/schemas/Exception'
  post:
    tags:
      - tray
    parameters:
      - $ref: "tray.yml#/parameters/TrayId"
    responses:
      '400':
        description: Bad request.
        content:
          application/json:
            schema:
              $ref: 'common.yml#/schemas/Exception'

trayHistory:
  get:
    tags:
      - tray
    parameters:
      - $ref: "tray.yml#/parameters/TrayId"
    responses:
      '400':
        description: Bad request.
        content:
          application/json:
            schema:
              $ref: 'common.yml#/schemas/Exception'
  post:
    tags:
      - tray
    parameters:
      - $ref: "tray.yml#/parameters/TrayId"
    responses:
      '400':
        description: Bad request.
        content:
          application/json:
            schema:
              $ref: 'common.yml#/schemas/Exception'

parameters:
  TrayId:
    name: tray_id
    in: path
    description: id of Themis tray
    required: true
    schema:
      type: integer
# common.yml
schemas:
  Exception:
    type: array
    items:
      type: object
      properties:
        exception:
          type: string
          examples:
            - ValidationException
        message:
          type: string
          examples:
            - Field Thing is not valid for some helpful reason.
        field:
          type: string
          examples:
            - thing

Swagger-UI configuration options:

<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.0/swagger-ui-bundle.js"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.10.0/swagger-ui-standalone-preset.js"> </script>
<script>
    window.onload = function() {
        const ui = SwaggerUIBundle({
            url: "http://localhost:8000/api/spec/openapi.yml",
            dom_id: '#swagger-ui',
            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIStandalonePreset
            ],
            validatorUrl: null,
            layout: "StandaloneLayout"
        })
        window.ui = ui
    }
</script>

Describe the bug you're encountering

When resolving refs that point to other files, swagger-ui requests those files from the server. In the 4.x branch every file was requested exactly once. In the 5.x branch the same files are recursively requested again and again whenever a ref from the main file is resolved. The openapi spec given above yields the following network requests:

image

To reproduce...

Serve example openapi spec via the url argument to SwaggerUIBundle.

Expected behavior

Each individual yml file should be requested exactly once and then the cached file should be used.

Actual behavior

Each individual yml file is requested from the server whenever a ref to it is encountered.

grinspins avatar Nov 20 '23 15:11 grinspins

Is there a workaround to resolve this issue? It prevents anyone from working on the schema and verifying changes via swagger ui by simply running a http server like simplehttpserver, as the UI is just frozen and keeps reloading files. To verify any change, the schema has to be bundled to replace the $refs.

nflaig avatar Jan 18 '24 10:01 nflaig