swagger-api / swagger-api/swagger-ui

Do not resolve $refs in property examples

Open
#6,628 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

cat: $ref resolution type: bug
Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

Description

Q&A (please complete the following information)
  • OS: Windows 10
  • Browser: any
  • Method of installation: any
  • Swagger-UI version: 3.36.2
  • Swagger/OpenAPI version: 2.0, 2.0
Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.0.0
info:
  title: $ref in a property example
  version: 1.0.0

paths:
  /foo:
    get:
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MyObject'

components:
  schemas:
    MyObject:
      type: object
      properties:
        prop:
          type: object
          example:
            $ref: '#/components/examples/StringExample'

  examples:
    StringExample:
      value: test
Describe the bug you're encountering

Even though the example keyword does not support $ref, Swagger UI attempts to resolve those $refs when generating examples.

$refs should only be resolved when used directly under examples.<name> in OAS3 definitions.

Expected behavior

Response example appears as follows, with $ref treated as a literal property in the example object.

{
  "prop": {
    "$ref": "#/components/examples/StringExample"
  }
}
Actual behavior

Response example appears as

{
  "prop": {
    "value": "test"
  }
}
Additional context or thoughts

Related issues with $refs in examples:

  • #5625 - Do not resolve $ref in examples..value
  • #4765 - Do not resolve $ref in OAS 2.0 response examples

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

Reproduce the issue with the provided OpenAPI definition and inspect Swagger UI's response-example generation for property examples. Confirm that a $ref under a property example remains literal, while the expected response contains the $ref object rather than the referenced example value.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.