In my Swagger spec file, I want to return example responses, for that I can add examples
in response. But that makes my spec file quite big and error prone. Is there a way to refer to a file containing JSON of an example object?
I tried something like below but it doesn't seem to work.
get:
tags:
- businesses
summary: Get Taxable Entities details
description: ''
operationId: getTaxableEntities
produces:
- application/json
parameters:
- name: business_id
in: path
required: true
type: integer
format: int32
- name: gstIn
in: query
required: false
type: integer
format: int32
responses:
'200':
description: Taxable Entities
schema:
type: file
default:
$ref: taxable_entity_example.json
'401':
description: You are not authorised to view this Taxable Entity
The
example
keyword does not support$ref
, and OpenAPI 2.0 does not have a way to reference external examples.You need OpenAPI 3.0 (
openapi: 3.0.0
) to be able to reference external examples. OAS3 provides theexternalValue
keyword for this purpose:externalValue
can be an absolute or relative URL. Note that relativeexternalValue
URLs are resolved against the API server URL (servers[*].url
) and not the location of the API definition file.Note for Swagger UI and Swagger Editor users: Currently (as of December 2019) the content of
externalValue
examples is not displayed. Follow this issue for updates.