问题
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
回答1:
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 the externalValue keyword for this purpose:
openapi: 3.0.2
...
responses:
'200':
description: Taxable Entities
content:
application/json:
schema:
type: object
examples:
myExample: # arbitrary name/label
externalValue: 'https://myserver.com/examples/taxable_entity_example.json'
externalValue
can be an absolute or relative URL. Note that relative externalValue
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.
来源:https://stackoverflow.com/questions/41393982/how-to-refer-to-an-external-json-file-containing-response-examples-in-swagger