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

feat(fakers): ability to add fakers #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion openapi2jsonschema/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ def error(message):
@click.option('--stand-alone', is_flag=True, help='Whether or not to de-reference JSON schemas')
@click.option('--kubernetes', is_flag=True, help='Enable Kubernetes specific processors')
@click.option('--strict', is_flag=True, help='Prohibits properties not in the schema (additionalProperties: false)')
@click.option('--fakers', required=False, help='Enables registration of custom fakers. See https://git.io/vFAhp')
@click.argument('schema', metavar='SCHEMA_URL')
def default(output, schema, prefix, stand_alone, kubernetes, strict):
def default(output, schema, fakers, prefix, stand_alone, kubernetes, strict):
"""
Converts a valid OpenAPI specification into a set of JSON Schema files
"""
Expand All @@ -126,6 +127,11 @@ def default(output, schema, prefix, stand_alone, kubernetes, strict):
# Note that JSON is valid YAML, so we can use the YAML parser whether
# the schema is stored in JSON or YAML
data = yaml.load(response.read())
fakers_defined = len(fakers) != 0
if fakers_defined:
info("Loading fakers")
with open(fakers, 'r') as stream:
fakers_definitions = yaml.safe_load(stream)

if not os.path.exists(output):
os.makedirs(output)
Expand All @@ -142,6 +148,14 @@ def default(output, schema, prefix, stand_alone, kubernetes, strict):
{'type': 'string'},
{'type': 'integer'},
]}

if fakers_defined:
for schema_type, faker in fakers_definitions.iteritems():
if schema_type in definitions:
definitions[schema_type]['faker'] = faker
else:
definitions[schema_type] = {'faker': faker}

definitions_file.write(json.dumps({"definitions": definitions}, indent=2))

types = []
Expand Down