diff --git a/openapi2jsonschema/command.py b/openapi2jsonschema/command.py index 8261f2a..305235b 100644 --- a/openapi2jsonschema/command.py +++ b/openapi2jsonschema/command.py @@ -16,6 +16,7 @@ allow_null_optional_fields, change_dict_values, append_no_duplicates, + get_request_and_response_body_components_from_paths, ) from openapi2jsonschema.errors import UnsupportedError @@ -48,8 +49,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,8 +135,13 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict): else: components = data["components"]["schemas"] + if include_bodies: + components.update( + get_request_and_response_body_components_from_paths(data["paths"]), + ) + for title in components: - kind = title.split(".")[-1].lower() + kind = title.split(".")[-1] if kubernetes: group = title.split(".")[-3].lower() api_version = title.split(".")[-2].lower() @@ -155,8 +175,7 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict): if ( kubernetes and stand_alone - and kind - in [ + and kind.lower() in [ "jsonschemaprops", "jsonschemapropsorarray", "customresourcevalidation", diff --git a/openapi2jsonschema/util.py b/openapi2jsonschema/util.py index 68fd8d4..2e881c7 100644 --- a/openapi2jsonschema/util.py +++ b/openapi2jsonschema/util.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +import re + def iteritems(d): if hasattr(dict, "iteritems"): @@ -111,3 +113,49 @@ 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.get("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 + + +def get_request_and_response_body_components_from_paths(paths): + components = {} + for path, path_definition in 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), + )) + return components