Skip to content

Commit f8e4e34

Browse files
committed
String byte format fix
1 parent c846b2e commit f8e4e34

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

openapi_core/schema/schemas/models.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
UndefinedItemsSchema, InvalidCustomFormatSchemaValue, InvalidSchemaProperty,
2020
)
2121
from openapi_core.schema.schemas.util import (
22-
forcebool, format_date, format_datetime,
23-
format_uuid,
22+
forcebool, format_date, format_datetime, format_byte, format_uuid,
2423
)
2524
from openapi_core.schema.schemas.validators import (
2625
TypeValidator, AttributeValidator,
@@ -48,7 +47,7 @@ class Schema(object):
4847
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
4948
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
5049
SchemaFormat.UUID: Format(format_uuid, TypeValidator(UUID)),
51-
SchemaFormat.BYTE: Format(b64decode, TypeValidator(binary_type)),
50+
SchemaFormat.BYTE: Format(format_byte, TypeValidator(text_type)),
5251
}
5352

5453
TYPE_VALIDATOR_CALLABLE_GETTER = {

openapi_core/schema/schemas/util.py

+6-1
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
from uuid import UUID
89

@@ -31,3 +32,7 @@ def format_uuid(value):
3132
if isinstance(value, UUID):
3233
return value
3334
return UUID(value)
35+
36+
37+
def format_byte(value, encoding='utf8'):
38+
return text_type(b64decode(value), encoding)

tests/integration/test_petstore.py

+5-3
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
66

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

3333
class TestPetstore(object):
3434

35-
api_key = b'12345'
35+
api_key = '12345'
3636

3737
@property
3838
def api_key_encoded(self):
39-
return b64encode(self.api_key)
39+
api_key_bytes = self.api_key.encode('utf8')
40+
api_key_bytes_enc = b64encode(api_key_bytes)
41+
return text_type(api_key_bytes_enc, 'utf8')
4042

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

tests/integration/test_validators.py

+5-2
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
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 = self.api_key.encode('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

+2-2
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ def test_string_format_binary(self, value):
577577
assert result == value
578578

579579
@pytest.mark.parametrize('value', [
580-
u('tsssst'), u('dGVzdA=='),
580+
b('tsssst'), b('dGVzdA=='),
581581
])
582582
def test_string_format_byte_invalid(self, value):
583583
schema = Schema('string', schema_format='byte')
@@ -586,7 +586,7 @@ def test_string_format_byte_invalid(self, value):
586586
schema.validate(value)
587587

588588
@pytest.mark.parametrize('value', [
589-
b('tsssst'), b('dGVzdA=='),
589+
u('tsssst'), u('dGVzdA=='),
590590
])
591591
def test_string_format_byte(self, value):
592592
schema = Schema('string', schema_format='byte')

0 commit comments

Comments
 (0)