Our strategy to standardize default responses, schemas and other components between different API providers, is to provide those definitions in a shared and versioned file, like https://teamdigitale.github.io/openapi/0.0.5/definitions.yaml
API providers can then:
$reference reusable componentsopenapi_resolver)With a set of common components API designers can create better interfaces and ask themself questions like:
Supported reusable components can be:
schemas: data types and objectsparameters: request parameters, that may be defined in headers, query and pathresponses: http responsessecuritySchemes: security requirements to be applied to a given pathNOTE: in this course we won't go indeep on all the possibilities of OAS3, which you can see on the OAS website
In [1]:
# Exercise: creating a bundle from a $ref file
#
# You can resolve dependencies and create a bundle file with
!pip install openapi_resolver
In [2]:
# Exercise: create a bundle from the previous file with
!python -m openapi_resolver /code/notebooks/oas3/ex-05-01-bundle.yaml
YAML has a very nice feature, named anchors. They allow to define and reference given portions of a YAML file.
# the following &anchor stores the `foo` value
a: &this_is_an_anchor foo
# *star dereferences the anchor
b: *this_is_an_anchor
See anchors.yaml
In [3]:
# Check yaml file content.
from pathlib import Path
content = Path('anchors.yaml').read_text()
print(content)
In [4]:
ret = yaml.safe_load(content)
assert ret['anchored_content'], ret['other_anchor']
print(ret['foo'])
print(ret['bar'])
As every operation may have a set of predefined responses, namely:
You can put them in an x- custom parameter.which will be ignored by the OAS spec parser.
x-common-responses: &common-responses
503ServiceUnavailable:
$ref: ...
429TooManyRequests:
$ref: ...
Then use the <<: keyword and *anchor_name to reference them.
paths:
/status:
get:
...
responses:
# Add the anchored responses
<<: *common-responses
# And now the other ones.
'200':
...
In [ ]: