From e550de38f937e6ec05908d3ee1d6c15cea70b42d Mon Sep 17 00:00:00 2001 From: Samu Toimela Date: Thu, 19 Dec 2019 14:52:36 +0200 Subject: [PATCH] Add header support Add support for adding extra headers with '-H' or '--header'. The flag can be specified multiple times to add multiple headers --- README.md | 2 ++ openapi2jsonschema/command.py | 15 +++++++++++++-- openapi2jsonschema/util.py | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 39a4a9d..50a9de1 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ Options: -o, --output PATH Directory to store schema files -p, --prefix TEXT Prefix for JSON references (only for OpenAPI versions before 3.0) + -H, --header TEXT Extra header to use when getting a schema. May be + specified multiple times. --stand-alone Whether or not to de-reference JSON schemas --kubernetes Enable Kubernetes specific processors --strict Prohibits properties not in the schema diff --git a/openapi2jsonschema/command.py b/openapi2jsonschema/command.py index 9cd1bfc..5628b2d 100644 --- a/openapi2jsonschema/command.py +++ b/openapi2jsonschema/command.py @@ -16,6 +16,7 @@ allow_null_optional_fields, change_dict_values, append_no_duplicates, + parse_headers, ) from openapi2jsonschema.errors import UnsupportedError @@ -34,6 +35,12 @@ default="_definitions.json", help="Prefix for JSON references (only for OpenAPI versions before 3.0)", ) +@click.option( + "-H", + "--header", + multiple=True, + help="Extra header to use when getting a schema. May be specified multiple times.", +) @click.option( "--stand-alone", is_flag=True, help="Whether or not to de-reference JSON schemas" ) @@ -49,7 +56,7 @@ help="Prohibits properties not in the schema (additionalProperties: false)", ) @click.argument("schema", metavar="SCHEMA_URL") -def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict): +def default(output, schema, prefix, header, stand_alone, expanded, kubernetes, strict): """ Converts a valid OpenAPI specification into a set of JSON Schema files """ @@ -59,7 +66,11 @@ def default(output, schema, prefix, stand_alone, expanded, kubernetes, strict): else: if os.path.isfile(schema): schema = "file://" + os.path.realpath(schema) - req = urllib.request.Request(schema) + if header: + headers = parse_headers(header) + req = urllib.request.Request(schema, headers=headers) + else: + req = urllib.request.Request(schema) response = urllib.request.urlopen(req) info("Parsing schema") diff --git a/openapi2jsonschema/util.py b/openapi2jsonschema/util.py index 34270d2..636002f 100644 --- a/openapi2jsonschema/util.py +++ b/openapi2jsonschema/util.py @@ -108,3 +108,18 @@ def append_no_duplicates(obj, key, value): obj[key] = [] if value not in obj[key]: obj[key].append(value) + + +def parse_headers(header): + """ + Argument is a tuple of header strings. + e.g. ('Content-Type: application/json', 'Accept: application/text') + Invalid headers are ignored. + The function returns a dictionary of the headers. + """ + res = {} + for h in header: + h = h.split(":", 1) + if len(h) == 2: + res[h[0].strip()] = h[1].strip() + return res