From c34f8cbccd44a88a302999a19f70cfd3bff5cbef Mon Sep 17 00:00:00 2001 From: Joost van der Hoff Date: Fri, 3 May 2019 12:37:02 +0200 Subject: [PATCH] Add option to include request and response body schemas Include schemas from: paths///requestBody/content//schema paths///responses//content//schema Closes #25 --- openapi2jsonschema/command.py | 45 ++++++++++++++++++++++++++++++++++- openapi2jsonschema/util.py | 16 +++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/openapi2jsonschema/command.py b/openapi2jsonschema/command.py index 8261f2a..0e721cb 100644 --- a/openapi2jsonschema/command.py +++ b/openapi2jsonschema/command.py @@ -5,6 +5,7 @@ import urllib import os import sys +import re from jsonref import JsonRef # type: ignore import click @@ -16,6 +17,7 @@ allow_null_optional_fields, change_dict_values, append_no_duplicates, + get_components_from_body_definition, ) from openapi2jsonschema.errors import UnsupportedError @@ -48,8 +50,22 @@ is_flag=True, help="Prohibits properties not in the schema (additionalProperties: false)", ) +@click.option( + "--include-bodies", + is_flag=True, + help="Include request and response bodies as if they are components", +) @click.argument("schema", metavar="SCHEMA_URL") -def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict): +def default( + output, + schema, + prefix, + stand_alone, + expanded, + kubernetes, + strict, + include_bodies, +): """ Converts a valid OpenAPI specification into a set of JSON Schema files """ @@ -120,6 +136,33 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict): else: components = data["components"]["schemas"] + if include_bodies: + for path, path_definition in data["paths"].items(): + for http_method, http_method_definition in path_definition.items(): + name_prefix_fmt = "paths_{:s}_{:s}_{{:s}}_".format( + # Paths "/" and "/root" will conflict, + # no idea how to solve this elegantly. + path.lstrip("/").replace("/", "_") or "root", + http_method.upper(), + ) + name_prefix_fmt = re.sub( + r"\{([^:\}]+)\}", + r"_\1_", + name_prefix_fmt, + ) + if "requestBody" in http_method_definition: + components.update(get_components_from_body_definition( + http_method_definition["requestBody"], + prefix=name_prefix_fmt.format("request") + )) + responses = http_method_definition["responses"] + for response_code, response in responses.items(): + response_name_part = "response_{}".format(response_code) + components.update(get_components_from_body_definition( + response, + prefix=name_prefix_fmt.format(response_name_part), + )) + for title in components: kind = title.split(".")[-1].lower() if kubernetes: diff --git a/openapi2jsonschema/util.py b/openapi2jsonschema/util.py index 68fd8d4..9370bbd 100644 --- a/openapi2jsonschema/util.py +++ b/openapi2jsonschema/util.py @@ -111,3 +111,19 @@ def append_no_duplicates(obj, key, value): obj[key] = [] if value not in obj[key]: obj[key].append(value) + + +def get_components_from_body_definition(body_definition, prefix=""): + MIMETYPE_TO_TYPENAME_MAP = { + "application/json": "json", + "application/vnd.api+json": "jsonapi", + } + result = {} + for mimetype, definition in body_definition["content"].items(): + type_name = MIMETYPE_TO_TYPENAME_MAP.get( + mimetype, + mimetype.replace("/", "_"), + ) + if "schema" in definition: + result["{:s}{:s}".format(prefix, type_name)] = definition["schema"] + return result