-
Notifications
You must be signed in to change notification settings - Fork 74
Open
Labels
Description
Summary
Description might be needed for a JSON schema to add comments for developers. It should be an optional parameter as it is not the case for everyone.
Example
I created a POC project, not usable for production, that generates documentation for YML files. I add description property to any JSON property that has a type, so I can ignore arrays, etc. It recursively visits schema, as a dictionary, and adds description property if a value has type.
def generate_json_schema(inputPath: str, outputPath: str, useAnnotations: bool = False, verbose: bool = False) -> None:
if (verbose):
print("Loading JSON file from path: " + inputPath)
jsonObj = json.load(open(inputPath))
if (verbose):
print("Generating schema")
builder: SchemaBuilder = SchemaBuilder()
builder.add_object(jsonObj)
schema: dict = builder.to_schema()
# Here comes the magic
add_description_property(schema) # type: ignore
if (useAnnotations):
schema["title"] = TITLE
schema["description"] = DESCRIPTION
with open(outputPath, 'w') as json_file:
json.dump(schema, json_file, indent=4)
if (verbose):
print("Saved schema to path: " + outputPath)
def add_description_property(d: dict) -> None:
if ("type" in d):
d["description"] = "# Description title\n\nDescription placeholder with *bold* and _italic_ text. This is a `code snippet`."
for key, value in d.items():
if (type(value) == dict):
add_description_property(value)