Skip to content

nesteddict2yaml renders empty dict value '{}' to null #106

@sakurai-youhei

Description

@sakurai-youhei

Issue: Empty dict values in the definitions are rendered not to {} but to null incorrectly.

PoC code:

from json import loads
from aiohttp import web
from aiohttp_swagger.helpers import generate_doc_from_each_end_point

app = web.Application()
definitions = dict(
    test={
        "type": "object",
        "properties": {"prop": {"type": "object", "default": dict()}},
    }
)
doc = generate_doc_from_each_end_point(app, definitions=definitions)
print(doc)

test1 = definitions["test"]
test2 = loads(doc)["definitions"]["test"]

print(test1)
print(test2)
assert test1 == test2

Expectation:

  • The generated doc shall show "default": {}.
  • test2 extracted from the generated doc shall be equal to test1 from the original definitions.
{"swagger": "2.0", "info": {"description": "Swagger API definition\n", "version": "1.0.0", "title": "Swagger API"}, "basePath": "/", "schemes": ["http", "https"], "definitions": {"test": {"type": "object", "properties": {"prop": {"type": "object", "default": {}}}}}, "paths": {}}
{'type': 'object', 'properties': {'prop': {'type': 'object', 'default': {}}}}
{'type': 'object', 'properties': {'prop': {'type': 'object', 'default': {}}}}

Observation:

  • The generated doc shows "default": null.
  • test2 extracted from the generated doc isn't equal to test1 from the original definitions.
{"swagger": "2.0", "info": {"description": "Swagger API definition\n", "version": "1.0.0", "title": "Swagger API"}, "basePath": "/", "schemes": ["http", "https"], "definitions": {"test": {"type": "object", "properties": {"prop": {"type": "object", "default": null}}}}, "paths": {}}
{'type': 'object', 'properties': {'prop': {'type': 'object', 'default': {}}}}
{'type': 'object', 'properties': {'prop': {'type': 'object', 'default': None}}}
Traceback (most recent call last):
  File "C:\Users\YouheiSakurai\Desktop\poc.py", line 20, in <module>
    assert test1 == test2
           ^^^^^^^^^^^^^^
AssertionError

Environment

  • Python 3.12.7
  • aiohttp-swagger 1.0.16

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions