Skip to content

Commit

Permalink
add constructor to handle equal sign (=) (#104)
Browse files Browse the repository at this point in the history
Equal sign (=) was not parsed properly by pyyaml.
Added constructor to parse equal sign as string.
Related issue: #103
  • Loading branch information
Eyar Zilberman authored Apr 6, 2022
1 parent 932b35d commit b5f34ca
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion scripts/openapi2jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def allow_null_optional_fields(data, parent=None, grand_parent=None, key=None):
elif isinstance(v, str):
is_non_null_type = k == "type" and v != "null"
has_required_fields = grand_parent and "required" in grand_parent
if is_non_null_type and not has_required_field:
if is_non_null_type and not has_required_fields:
new_v = [v, "null"]
new[k] = new_v
return new
Expand Down Expand Up @@ -106,6 +106,16 @@ def write_schema_file(schema, filename):
print("JSON schema written to {filename}".format(filename=filename))


def construct_value(load, node):
# Handle nodes that start with '='
# See https://github.com/yaml/pyyaml/issues/89
if not isinstance(node, yaml.ScalarNode):
raise yaml.constructor.ConstructorError(
"while constructing a value",
node.start_mark,
"expected a scalar, but found %s" % node.id, node.start_mark
)
yield str(node.value)


if __name__ == "__main__":
Expand All @@ -120,6 +130,7 @@ def write_schema_file(schema, filename):
f = open(crdFile)
with f:
defs = []
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:value', construct_value)
for y in yaml.load_all(f, Loader=yaml.SafeLoader):
if y is None:
continue
Expand Down

0 comments on commit b5f34ca

Please sign in to comment.