json-schema-for-humans icon indicating copy to clipboard operation
json-schema-for-humans copied to clipboard

bug: `generate_from_file_object` doesn't work on file objects without `.name` attribute

Open VolkerH opened this issue 2 years ago • 0 comments

The generate_from_file_object function has the arguments schema_file: FileLikeType and result_file: FileLikeType with FileLikeType = Union[TextIO, TextIOWrapper, FileIO].

However in https://github.com/coveooss/json-schema-for-humans/blob/178c7b1527b059b3ffe4e83c827fc8774b495aa0/json_schema_for_humans/generate.py#L93 the code tries to access the property .name of result_file. However TextIOBase objects don't have a .name attribute.

Example code like this

schema_file_object = StringIO(json.dumps(Criterion.model_json_schema()))
result_file_object = StringIO()

generate_from_file_object(schema_file=schema_file_object,  result_file=result_file_object)

results in the following error.

\json_schema_for_humans\generate.py:93, in generate_from_file_object(schema_file, result_file, minify, deprecated_from_description, default_from_description, expand_buttons, copy_css, copy_js, link_to_reused_ref, config)
     80 """Generate the JSON schema documentation from opened file objects for both input and output files. The
     81 result_file should be opened in write mode.
     82 """
     83 config = config or get_final_config(
     84     minify=minify,
     85     deprecated_from_description=deprecated_from_description,
   (...)
     90     link_to_reused_ref=link_to_reused_ref,
     91 )
---> 93 schemas_to_render = [SchemaToRender(schema_file, result_file, Path(result_file.name).parent)]
     94 template_renderer = TemplateRenderer(config)
     95 generate_schemas_doc(schemas_to_render, template_renderer)

AttributeError: '_io.StringIO' object has no attribute 'name'

VolkerH avatar Oct 09 '23 09:10 VolkerH