Skip to content

Commit b5f34ca

Browse files
author
Eyar Zilberman
authoredApr 6, 2022
add constructor to handle equal sign (=) (#104)
Equal sign (=) was not parsed properly by pyyaml. Added constructor to parse equal sign as string. Related issue: #103
1 parent 932b35d commit b5f34ca

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed
 

‎scripts/openapi2jsonschema.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def allow_null_optional_fields(data, parent=None, grand_parent=None, key=None):
7272
elif isinstance(v, str):
7373
is_non_null_type = k == "type" and v != "null"
7474
has_required_fields = grand_parent and "required" in grand_parent
75-
if is_non_null_type and not has_required_field:
75+
if is_non_null_type and not has_required_fields:
7676
new_v = [v, "null"]
7777
new[k] = new_v
7878
return new
@@ -106,6 +106,16 @@ def write_schema_file(schema, filename):
106106
print("JSON schema written to {filename}".format(filename=filename))
107107

108108

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

110120

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

0 commit comments

Comments
 (0)