Skip to content

Problem with as_dict() when schema contains properties of type object #35

Description

@brodellwalker

Unexpected behavior when calling as_dict on the JSON Schema object if the object contains a property/properties of type "object". Specifically, all other data of the object is serialized out as a dictionary (including arrays of objects) but the object(s) are not serialized properly. Instead resulting in something like "<abc_object attributes: attr1, attr2, attr3,...>".

To better illustrate the problem consider the following schema and expected output from calling as_dict() on the object created from the schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "sample",
  "description": "sample description",
  "properties":
  {
    "mystr":
    {
      "description": "string sample",
      "type": "string"
    },
    "myobj":
    {
      "description": "object sample",
      "type": "object",
      "properties":
      {
        "attr1":
        {
          "type": "string"
        },
        "attr2":
        {
          "type": "number"
        }
      }
    }
  }
}

Expected output dictionary:
{"mystr": "test string value", "myobj": {"attr1": "test value", "attr2": 12345}}

Note, that if I modify my schema such that "myobj" is an array whose items are objects produces a dictionary which matches what I would expect as the output value (i.e. "myobj" is an array of objects and the objects are properly serialized within the array).

This can be solved by modifying line 38 in classbuilder.py to be:

out[prop] = propval.as_dict() if hasattr(propval, 'as_dict') else propval

Instead of:

out[prop] = propval

I can send a pull request with the above fix, if you wish. But since it is a one-liner, hopefully you can add the change yourself. If I am misunderstanding the as_dict() method and my expected values are not correct, could you please provide some additional information about the method and what is expected?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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