New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to generate schema with fields defined using `dict` #330

Closed
nkonin opened this Issue Dec 26, 2018 · 1 comment

Comments

2 participants
@nkonin
Copy link
Contributor

nkonin commented Dec 26, 2018

Bug

  • OS: macos Mojave 10.14.2
  • Python version: 3.7.0 (default, Sep 18 2018, 18:47:08) [Clang 10.0.0 (clang-1000.10.43.1)]
  • Pydantic version: 0.16

Code snippet:

import pydantic

class Example(pydantic.BaseModel):
    field: dict = None


Example.schema()

Expected:

In [5]: Example.schema()
Out[5]:
{'title': 'Example',
 'type': 'object',
 'properties': {'field': {'title': 'Field', 'type': 'object'}}}

Actually:

ValueError: Value not declarable with JSON Schema, field: field type=dict default=None
Traceback:

In [17]: Example.schema()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-dcbb076a11a6> in <module>
----> 1 Example.schema()

~/Documents/projects/broker.nosync/mf_credit_back/venv/lib/python3.7/site-packages/pydantic/main.py in schema(cls, by_alias)
    334         if cached is not None:
    335             return cached
--> 336         s = model_schema(cls, by_alias=by_alias)
    337         cls._schema_cache[by_alias] = s
    338         return s

~/Documents/projects/broker.nosync/mf_credit_back/venv/lib/python3.7/site-packages/pydantic/schema.py in model_schema(model, by_alias, ref_prefix)
    189     model_name_map = get_model_name_map(flat_models)
    190     m_schema, m_definitions = model_process_schema(
--> 191         model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
    192     )
    193     if m_definitions:

~/Documents/projects/broker.nosync/mf_credit_back/venv/lib/python3.7/site-packages/pydantic/schema.py in model_process_schema(model, by_alias, model_name_map, ref_prefix)
    455         s['description'] = clean_docstring(model.__doc__)
    456     m_schema, m_definitions = model_type_schema(
--> 457         model, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
    458     )
    459     s.update(m_schema)

~/Documents/projects/broker.nosync/mf_credit_back/venv/lib/python3.7/site-packages/pydantic/schema.py in model_type_schema(model, by_alias, model_name_map, ref_prefix)
    476     for k, f in model.__fields__.items():
    477         f_schema, f_definitions = field_schema(
--> 478             f, by_alias=by_alias, model_name_map=model_name_map, ref_prefix=ref_prefix
    479         )
    480         definitions.update(f_definitions)

~/Documents/projects/broker.nosync/mf_credit_back/venv/lib/python3.7/site-packages/pydantic/schema.py in field_schema(field, by_alias, model_name_map, ref_prefix)
    236         model_name_map=model_name_map,
    237         schema_overrides=schema_overrides,
--> 238         ref_prefix=ref_prefix,
    239     )
    240     # $ref will only be returned when there are no schema_overrides

~/Documents/projects/broker.nosync/mf_credit_back/venv/lib/python3.7/site-packages/pydantic/schema.py in field_type_schema(field, by_alias, model_name_map, schema_overrides, ref_prefix)
    434             model_name_map=model_name_map,
    435             schema_overrides=schema_overrides,
--> 436             ref_prefix=ref_prefix,
    437         )
    438         definitions.update(f_definitions)

~/Documents/projects/broker.nosync/mf_credit_back/venv/lib/python3.7/site-packages/pydantic/schema.py in field_singleton_schema(field, by_alias, model_name_map, schema_overrides, ref_prefix)
    632         else:
    633             return sub_schema, definitions
--> 634     raise ValueError(f'Value not declarable with JSON Schema, field: {field}')
    635
    636

ValueError: Value not declarable with JSON Schema, field: field type=dict default=None

nkonin added a commit to nkonin/pydantic that referenced this issue Dec 26, 2018

nkonin added a commit to nkonin/pydantic that referenced this issue Dec 26, 2018

samuelcolvin added a commit that referenced this issue Dec 27, 2018

Fix schema generation for fields annotated as `: dict` (#331)
* #330 Fix schema generation for fields annotated as `: dict`

* #330 update history.rst

* #330 format on linter's advice
@samuelcolvin

This comment has been minimized.

Copy link
Owner

samuelcolvin commented Dec 27, 2018

fixed in #331.

@samuelcolvin samuelcolvin referenced this issue Dec 27, 2018

Merged

Simple iterables schema generation #335

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