Skip to content

Date-time format unmarshal tz fix #237

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

Merged
merged 1 commit into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions openapi_core/unmarshalling/schemas/unmarshallers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from functools import partial
import logging

from isodate.isodatetime import parse_datetime

from openapi_schema_validator._types import (
is_array, is_bool, is_integer,
is_object, is_number, is_string,
Expand All @@ -20,7 +22,7 @@
)
from openapi_core.unmarshalling.schemas.formatters import Formatter
from openapi_core.unmarshalling.schemas.util import (
forcebool, format_date, format_datetime, format_byte, format_uuid,
forcebool, format_date, format_byte, format_uuid,
format_number,
)

Expand Down Expand Up @@ -77,7 +79,7 @@ class StringUnmarshaller(PrimitiveTypeUnmarshaller):
partial(oas30_format_checker.check, format='date'), format_date),
SchemaFormat.DATETIME: Formatter.from_callables(
partial(oas30_format_checker.check, format='date-time'),
format_datetime),
parse_datetime),
SchemaFormat.BINARY: Formatter.from_callables(
partial(oas30_format_checker.check, format='binary'), binary_type),
SchemaFormat.UUID: Formatter.from_callables(
Expand Down
6 changes: 0 additions & 6 deletions openapi_core/unmarshalling/schemas/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import datetime
from distutils.util import strtobool
from six import string_types, text_type, integer_types
import strict_rfc3339
from uuid import UUID


Expand All @@ -18,11 +17,6 @@ def format_date(value):
return datetime.datetime.strptime(value, '%Y-%m-%d').date()


def format_datetime(value):
timestamp = strict_rfc3339.rfc3339_to_timestamp(value)
return datetime.datetime.utcfromtimestamp(timestamp)


def format_uuid(value):
if isinstance(value, UUID):
return value
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
isodate==0.6.0
openapi-spec-validator
openapi-schema-validator
six
Expand Down
1 change: 1 addition & 0 deletions requirements_2.7.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
isodate==0.6.0
openapi-spec-validator
openapi-schema-validator
six
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ python_requires = >= 2.7, != 3.0.*, != 3.1.*, != 3.2.*, != 3.3.*, != 3.4.*
setup_requires =
setuptools
install_requires =
isodate
openapi-spec-validator
openapi-schema-validator
six
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/validation/test_petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
from base64 import b64encode
from uuid import UUID
from isodate.tzinfo import UTC
from six import text_type

from openapi_core.casting.schemas.exceptions import CastError
Expand Down Expand Up @@ -1090,7 +1091,7 @@ def test_post_tags_created_datetime(

assert parameters == RequestParameters()
assert isinstance(body, BaseModel)
assert body.created == datetime(2016, 4, 16, 16, 6, 5)
assert body.created == datetime(2016, 4, 16, 16, 6, 5, tzinfo=UTC)
assert body.name == pet_name

code = 400
Expand Down
22 changes: 20 additions & 2 deletions tests/unit/unmarshalling/test_unmarshal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import uuid

from isodate.tzinfo import UTC, FixedOffset
import pytest

from openapi_core.schema.media_types.models import MediaType
Expand Down Expand Up @@ -199,13 +200,30 @@ def test_string_format_date(self, unmarshaller_factory):

assert result == datetime.date(2018, 1, 2)

def test_string_format_datetime(self, unmarshaller_factory):
def test_string_format_datetime_invalid(self, unmarshaller_factory):
schema = Schema('string', schema_format='date-time')
value = '2018-01-02T00:00:00'

with pytest.raises(InvalidSchemaValue):
unmarshaller_factory(schema)(value)

def test_string_format_datetime_utc(self, unmarshaller_factory):
schema = Schema('string', schema_format='date-time')
value = '2018-01-02T00:00:00Z'

result = unmarshaller_factory(schema)(value)

assert result == datetime.datetime(2018, 1, 2, 0, 0)
tzinfo = UTC
assert result == datetime.datetime(2018, 1, 2, 0, 0, tzinfo=tzinfo)

def test_string_format_datetime_tz(self, unmarshaller_factory):
schema = Schema('string', schema_format='date-time')
value = '2020-04-01T12:00:00+02:00'

result = unmarshaller_factory(schema)(value)

tzinfo = FixedOffset(2)
assert result == datetime.datetime(2020, 4, 1, 12, 0, 0, tzinfo=tzinfo)

def test_string_format_custom(self, unmarshaller_factory):
formatted = 'x-custom'
Expand Down