Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header support #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions openapi2jsonschema/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
allow_null_optional_fields,
change_dict_values,
append_no_duplicates,
parse_headers,
)
from openapi2jsonschema.errors import UnsupportedError

Expand All @@ -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"
)
Expand All @@ -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
"""
Expand All @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions openapi2jsonschema/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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