Skip to content

Commit

Permalink
Fix schema for timedelta as number, instead of str (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo authored and samuelcolvin committed Dec 23, 2018
1 parent 1f270dd commit 81f1558
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Expand Up @@ -3,6 +3,10 @@
History
-------

v0.x.x (xxx-xx-xx)
..................
* fix schema for ``timedelta`` as number, by @tiangolo

v0.16.1 (2018-12-10)
....................
* fix ``create_model`` to correctly use the passed ``__config__``, #320 by @hugoduncan
Expand Down
4 changes: 2 additions & 2 deletions docs/schema_mapping.py
Expand Up @@ -228,9 +228,9 @@
],
[
'timedelta',
'string',
'number',
'{"format": "time-delta"}',
'Pydantic standard "format" extension',
'Difference in seconds (a ``float``), with Pydantic standard "format" extension',
'Suggested in JSON Schema repository\'s issues by maintainer.'
],
[
Expand Down
2 changes: 1 addition & 1 deletion pydantic/schema.py
Expand Up @@ -569,7 +569,7 @@ def field_singleton_sub_fields_schema(
(datetime, {'type': 'string', 'format': 'date-time'}),
(date, {'type': 'string', 'format': 'date'}),
(time, {'type': 'string', 'format': 'time'}),
(timedelta, {'type': 'string', 'format': 'time-delta'}),
(timedelta, {'type': 'number', 'format': 'time-delta'}),
(Json, {'type': 'string', 'format': 'json-string'}),
)

Expand Down
19 changes: 11 additions & 8 deletions tests/test_schema.py
Expand Up @@ -371,19 +371,22 @@ class Model(BaseModel):


@pytest.mark.parametrize(
'field_type,expected_schema', [(datetime, 'date-time'), (date, 'date'), (time, 'time'), (timedelta, 'time-delta')]
'field_type,expected_schema',
[
(datetime, {'type': 'string', 'format': 'date-time'}),
(date, {'type': 'string', 'format': 'date'}),
(time, {'type': 'string', 'format': 'time'}),
(timedelta, {'type': 'number', 'format': 'time-delta'}),
],
)
def test_date_types(field_type, expected_schema):
class Model(BaseModel):
a: field_type

base_schema = {
'title': 'Model',
'type': 'object',
'properties': {'a': {'title': 'A', 'type': 'string', 'format': ''}},
'required': ['a'],
}
base_schema['properties']['a']['format'] = expected_schema
attribute_schema = {'title': 'A'}
attribute_schema.update(expected_schema)

base_schema = {'title': 'Model', 'type': 'object', 'properties': {'a': attribute_schema}, 'required': ['a']}

assert Model.schema() == base_schema

Expand Down

0 comments on commit 81f1558

Please sign in to comment.