Skip to content

Commit

Permalink
Merge pull request #117 from p1c2u/fix/string-byte-format-fix
Browse files Browse the repository at this point in the history
String byte format fix
  • Loading branch information
p1c2u authored Mar 22, 2019
2 parents c846b2e + f8e4e34 commit e7fa45b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
5 changes: 2 additions & 3 deletions openapi_core/schema/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
UndefinedItemsSchema, InvalidCustomFormatSchemaValue, InvalidSchemaProperty,
)
from openapi_core.schema.schemas.util import (
forcebool, format_date, format_datetime,
format_uuid,
forcebool, format_date, format_datetime, format_byte, format_uuid,
)
from openapi_core.schema.schemas.validators import (
TypeValidator, AttributeValidator,
Expand Down Expand Up @@ -48,7 +47,7 @@ class Schema(object):
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
SchemaFormat.UUID: Format(format_uuid, TypeValidator(UUID)),
SchemaFormat.BYTE: Format(b64decode, TypeValidator(binary_type)),
SchemaFormat.BYTE: Format(format_byte, TypeValidator(text_type)),
}

TYPE_VALIDATOR_CALLABLE_GETTER = {
Expand Down
7 changes: 6 additions & 1 deletion openapi_core/schema/schemas/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""OpenAPI core schemas util module"""
from base64 import b64decode
import datetime
from distutils.util import strtobool
from json import dumps
from six import string_types
from six import string_types, text_type
import strict_rfc3339
from uuid import UUID

Expand Down Expand Up @@ -31,3 +32,7 @@ def format_uuid(value):
if isinstance(value, UUID):
return value
return UUID(value)


def format_byte(value, encoding='utf8'):
return text_type(b64decode(value), encoding)
8 changes: 5 additions & 3 deletions tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
from base64 import b64encode
from uuid import UUID
from six import iteritems
from six import iteritems, text_type

from openapi_core.extensions.models.models import BaseModel
from openapi_core.schema.media_types.exceptions import (
Expand Down Expand Up @@ -32,11 +32,13 @@

class TestPetstore(object):

api_key = b'12345'
api_key = '12345'

@property
def api_key_encoded(self):
return b64encode(self.api_key)
api_key_bytes = self.api_key.encode('utf8')
api_key_bytes_enc = b64encode(api_key_bytes)
return text_type(api_key_bytes_enc, 'utf8')

@pytest.fixture
def spec_dict(self, factory):
Expand Down
7 changes: 5 additions & 2 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from base64 import b64encode
import json
import pytest
from six import text_type

from openapi_core.schema.media_types.exceptions import (
InvalidContentType, InvalidMediaTypeValue,
Expand All @@ -23,11 +24,13 @@ class TestRequestValidator(object):

host_url = 'http://petstore.swagger.io'

api_key = b'12345'
api_key = '12345'

@property
def api_key_encoded(self):
return b64encode(self.api_key)
api_key_bytes = self.api_key.encode('utf8')
api_key_bytes_enc = b64encode(api_key_bytes)
return text_type(api_key_bytes_enc, 'utf8')

@pytest.fixture
def spec_dict(self, factory):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/schema/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def test_string_format_binary(self, value):
assert result == value

@pytest.mark.parametrize('value', [
u('tsssst'), u('dGVzdA=='),
b('tsssst'), b('dGVzdA=='),
])
def test_string_format_byte_invalid(self, value):
schema = Schema('string', schema_format='byte')
Expand All @@ -586,7 +586,7 @@ def test_string_format_byte_invalid(self, value):
schema.validate(value)

@pytest.mark.parametrize('value', [
b('tsssst'), b('dGVzdA=='),
u('tsssst'), u('dGVzdA=='),
])
def test_string_format_byte(self, value):
schema = Schema('string', schema_format='byte')
Expand Down

0 comments on commit e7fa45b

Please sign in to comment.