| 1 |
"""Utilities to get elements of generated spec""" |
| 2 |
|
| 3 |
from apispec import exceptions |
| 4 |
from apispec.core import APISpec |
| 5 |
from apispec.utils import build_reference |
| 6 |
|
| 7 |
|
| 8 |
def get_schemas(spec): |
| 9 |
if spec.openapi_version.major < 3: |
| 10 |
return spec.to_dict().get("definitions", {}) |
| 11 |
return spec.to_dict()["components"].get("schemas", {}) |
| 12 |
|
| 13 |
|
| 14 |
def get_responses(spec): |
| 15 |
if spec.openapi_version.major < 3: |
| 16 |
return spec.to_dict().get("responses", {}) |
| 17 |
return spec.to_dict()["components"].get("responses", {}) |
| 18 |
|
| 19 |
|
| 20 |
def get_parameters(spec): |
| 21 |
if spec.openapi_version.major < 3: |
| 22 |
return spec.to_dict().get("parameters", {}) |
| 23 |
return spec.to_dict()["components"].get("parameters", {}) |
| 24 |
|
| 25 |
|
| 26 |
def get_headers(spec): |
| 27 |
if spec.openapi_version.major < 3: |
| 28 |
return spec.to_dict().get("headers", {}) |
| 29 |
return spec.to_dict()["components"].get("headers", {}) |
| 30 |
|
| 31 |
|
| 32 |
def get_examples(spec): |
| 33 |
return spec.to_dict()["components"].get("examples", {}) |
| 34 |
|
| 35 |
|
| 36 |
def get_links(spec): |
| 37 |
return spec.to_dict()["components"].get("links", {}) |
| 38 |
|
| 39 |
|
| 40 |
def get_security_schemes(spec): |
| 41 |
if spec.openapi_version.major < 3: |
| 42 |
return spec.to_dict().get("securityDefinitions", {}) |
| 43 |
return spec.to_dict()["components"].get("securitySchemes", {}) |
| 44 |
|
| 45 |
|
| 46 |
def get_paths(spec): |
| 47 |
return spec.to_dict()["paths"] |
| 48 |
|
| 49 |
|
| 50 |
def build_ref(spec, component_type, obj): |
| 51 |
return build_reference(component_type, spec.openapi_version.major, obj) |