Skip to content

Commit 408291c

Browse files
committed
String byte format fix
1 parent 395f68b commit 408291c

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

openapi_core/schema/schemas/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
UndefinedItemsSchema, InvalidCustomFormatSchemaValue, InvalidSchemaProperty,
2020
)
2121
from openapi_core.schema.schemas.util import (
22-
forcebool, format_date, format_datetime,
22+
forcebool, format_date, format_datetime, format_byte,
2323
)
2424
from openapi_core.schema.schemas.validators import (
2525
TypeValidator, AttributeValidator,
@@ -50,7 +50,7 @@ class Schema(object):
5050
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
5151
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
5252
SchemaFormat.UUID: Format(UUID, TypeValidator(UUID)),
53-
SchemaFormat.BYTE: Format(b64decode, TypeValidator(binary_type)),
53+
SchemaFormat.BYTE: Format(format_byte, TypeValidator(text_type)),
5454
}
5555

5656
TYPE_VALIDATOR_CALLABLE_GETTER = {

openapi_core/schema/schemas/util.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""OpenAPI core schemas util module"""
2+
from base64 import b64decode
23
import datetime
34
from distutils.util import strtobool
45
from json import dumps
5-
from six import string_types
6+
from six import string_types, text_type
67
import strict_rfc3339
78

89

@@ -24,3 +25,7 @@ def format_date(value):
2425
def format_datetime(value):
2526
timestamp = strict_rfc3339.rfc3339_to_timestamp(value)
2627
return datetime.datetime.utcfromtimestamp(timestamp)
28+
29+
30+
def format_byte(value, encoding='utf8'):
31+
return text_type(b64decode(value), encoding)

tests/integration/test_petstore.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33
from base64 import b64encode
44
from uuid import UUID
5-
from six import iteritems
5+
from six import iteritems, text_type, binary_type
66

77
from openapi_core.extensions.models.models import BaseModel
88
from openapi_core.schema.media_types.exceptions import (
@@ -31,11 +31,13 @@
3131

3232
class TestPetstore(object):
3333

34-
api_key = b'12345'
34+
api_key = '12345'
3535

3636
@property
3737
def api_key_encoded(self):
38-
return b64encode(self.api_key)
38+
api_key_bytes = binary_type(self.api_key, 'utf8')
39+
api_key_bytes_enc = b64encode(api_key_bytes)
40+
return text_type(api_key_bytes_enc, 'utf8')
3941

4042
@pytest.fixture
4143
def spec_dict(self, factory):

tests/integration/test_validators.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from base64 import b64encode
22
import json
33
import pytest
4+
from six import text_type, binary_type
45

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

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

26-
api_key = b'12345'
27+
api_key = '12345'
2728

2829
@property
2930
def api_key_encoded(self):
30-
return b64encode(self.api_key)
31+
api_key_bytes = binary_type(self.api_key, 'utf8')
32+
api_key_bytes_enc = b64encode(api_key_bytes)
33+
return text_type(api_key_bytes_enc, 'utf8')
3134

3235
@pytest.fixture
3336
def spec_dict(self, factory):

tests/unit/schema/test_schemas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def test_string_format_binary(self, value):
479479
assert result == value
480480

481481
@pytest.mark.parametrize('value', [
482-
u('tsssst'), u('dGVzdA=='),
482+
b('tsssst'), b('dGVzdA=='),
483483
])
484484
def test_string_format_byte_invalid(self, value):
485485
schema = Schema('string', schema_format='byte')
@@ -488,7 +488,7 @@ def test_string_format_byte_invalid(self, value):
488488
schema.validate(value)
489489

490490
@pytest.mark.parametrize('value', [
491-
b('tsssst'), b('dGVzdA=='),
491+
u('tsssst'), u('dGVzdA=='),
492492
])
493493
def test_string_format_byte(self, value):
494494
schema = Schema('string', schema_format='byte')

0 commit comments

Comments
 (0)