jazzband / jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
http://jsonmodels.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
334 stars 51 forks source link

BaseField children as ListField items_types #42

Closed hgenru closed 9 years ago

hgenru commented 9 years ago

I try run simple code:

from jsonschema import models, fileds
>>> class TestModel(models.Base):
...     test_list_field = fields.ListField(fields.StringField())
... 
>>> test_obj = TestModel()
>>> test_obj.test_list_field = ['one', 'two', 3]
>>> test_obj.to_struct()
{'test_list_field': ['one', 'two', 3]}  # Validate pass (but should not)
>>> test_obj.to_json_schema()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/hgen/.virtualenvs/igrofy-spec/lib/python3.4/site-packages/jsonmodels/models.py", line 73, in to_json_schema
    return parsers.to_json_schema(cls)
  File "/home/hgen/.virtualenvs/igrofy-spec/lib/python3.4/site-packages/jsonmodels/parsers.py", line 98, in to_json_schema
    prop[name] = _parse_list(field)
  File "/home/hgen/.virtualenvs/igrofy-spec/lib/python3.4/site-packages/jsonmodels/parsers.py", line 63, in _parse_list
    items = cls.to_json_schema()
AttributeError: 'StringField' object has no attribute 'to_json_schema'
hgenru commented 9 years ago

It seems that ListField can take as items_types only Base model children

beregond commented 9 years ago

@hgenru try this:

from jsonmodels import models, fields

class TestModel(models.Base):

    test_list = fields.ListField(str)

fields are only to define fields, for type you can put basic types.

Yet from what I see there is problem with generation of json schema - will check it today.

beregond commented 9 years ago

Fixed in versions 1.4.1 and 2.0.1.