Skip to content

Commit 88cc3e3

Browse files
authored
Merge pull request python-openapi#111 from p1c2u/feature/byte-string-format
byte string format
2 parents 449baf1 + 17b1868 commit 88cc3e3

File tree

5 files changed

+59
-23
lines changed

5 files changed

+59
-23
lines changed

openapi_core/schema/schemas/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import attr
33
import functools
44
import logging
5+
from base64 import b64decode, b64encode
56
from collections import defaultdict
67
from datetime import date, datetime
8+
from uuid import UUID
79
import re
810
import warnings
911

1012
from six import iteritems, integer_types, binary_type, text_type
11-
from uuid import UUID
1213

1314
from openapi_core.extensions.models.factories import ModelFactory
1415
from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType
@@ -44,10 +45,12 @@ class Schema(object):
4445

4546
STRING_FORMAT_CALLABLE_GETTER = {
4647
SchemaFormat.NONE: Format(text_type, TypeValidator(text_type)),
47-
SchemaFormat.DATE: Format(format_date, TypeValidator(date, exclude=datetime)),
48+
SchemaFormat.DATE: Format(
49+
format_date, TypeValidator(date, exclude=datetime)),
4850
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
4951
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
5052
SchemaFormat.UUID: Format(UUID, TypeValidator(UUID)),
53+
SchemaFormat.BYTE: Format(b64decode, TypeValidator(binary_type)),
5154
}
5255

5356
TYPE_VALIDATOR_CALLABLE_GETTER = {

tests/integration/data/v3.0/petstore.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ paths:
7878
- name: api_key
7979
in: header
8080
schema:
81-
type: integer
82-
format: int32
81+
type: string
82+
format: byte
8383
required: true
8484
- name: user
8585
in: cookie

tests/integration/test_petstore.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import pytest
3+
from base64 import b64encode
34
from uuid import UUID
45
from six import iteritems
56

@@ -30,6 +31,12 @@
3031

3132
class TestPetstore(object):
3233

34+
api_key = b'12345'
35+
36+
@property
37+
def api_key_encoded(self):
38+
return b64encode(self.api_key)
39+
3340
@pytest.fixture
3441
def spec_dict(self, factory):
3542
return factory.spec_from_file("data/v3.0/petstore.yaml")
@@ -424,7 +431,7 @@ def test_post_birds(self, spec, spec_dict):
424431
}
425432
data = json.dumps(data_json)
426433
headers = {
427-
'api_key': '12345',
434+
'api_key': self.api_key_encoded,
428435
}
429436
cookies = {
430437
'user': '123',
@@ -440,7 +447,7 @@ def test_post_birds(self, spec, spec_dict):
440447

441448
assert parameters == {
442449
'header': {
443-
'api_key': 12345,
450+
'api_key': self.api_key,
444451
},
445452
'cookie': {
446453
'user': 123,
@@ -484,7 +491,7 @@ def test_post_cats(self, spec, spec_dict):
484491
}
485492
data = json.dumps(data_json)
486493
headers = {
487-
'api_key': '12345',
494+
'api_key': self.api_key_encoded,
488495
}
489496
cookies = {
490497
'user': '123',
@@ -500,7 +507,7 @@ def test_post_cats(self, spec, spec_dict):
500507

501508
assert parameters == {
502509
'header': {
503-
'api_key': 12345,
510+
'api_key': self.api_key,
504511
},
505512
'cookie': {
506513
'user': 123,
@@ -544,7 +551,7 @@ def test_post_cats_boolean_string(self, spec, spec_dict):
544551
}
545552
data = json.dumps(data_json)
546553
headers = {
547-
'api_key': '12345',
554+
'api_key': self.api_key_encoded,
548555
}
549556
cookies = {
550557
'user': '123',
@@ -560,7 +567,7 @@ def test_post_cats_boolean_string(self, spec, spec_dict):
560567

561568
assert parameters == {
562569
'header': {
563-
'api_key': 12345,
570+
'api_key': self.api_key,
564571
},
565572
'cookie': {
566573
'user': 123,
@@ -592,7 +599,7 @@ def test_post_no_one_of_schema(self, spec, spec_dict):
592599
}
593600
data = json.dumps(data_json)
594601
headers = {
595-
'api_key': '12345',
602+
'api_key': self.api_key_encoded,
596603
}
597604
cookies = {
598605
'user': '123',
@@ -608,7 +615,7 @@ def test_post_no_one_of_schema(self, spec, spec_dict):
608615

609616
assert parameters == {
610617
'header': {
611-
'api_key': 12345,
618+
'api_key': self.api_key,
612619
},
613620
'cookie': {
614621
'user': 123,
@@ -631,7 +638,7 @@ def test_post_cats_only_required_body(self, spec, spec_dict):
631638
}
632639
data = json.dumps(data_json)
633640
headers = {
634-
'api_key': '12345',
641+
'api_key': self.api_key_encoded,
635642
}
636643
cookies = {
637644
'user': '123',
@@ -647,7 +654,7 @@ def test_post_cats_only_required_body(self, spec, spec_dict):
647654

648655
assert parameters == {
649656
'header': {
650-
'api_key': 12345,
657+
'api_key': self.api_key,
651658
},
652659
'cookie': {
653660
'user': 123,
@@ -672,7 +679,7 @@ def test_post_pets_raises_invalid_mimetype(self, spec):
672679
}
673680
data = json.dumps(data_json)
674681
headers = {
675-
'api_key': '12345',
682+
'api_key': self.api_key_encoded,
676683
}
677684
cookies = {
678685
'user': '123',
@@ -688,7 +695,7 @@ def test_post_pets_raises_invalid_mimetype(self, spec):
688695

689696
assert parameters == {
690697
'header': {
691-
'api_key': 12345,
698+
'api_key': self.api_key,
692699
},
693700
'cookie': {
694701
'user': 123,
@@ -711,7 +718,7 @@ def test_post_pets_missing_cookie(self, spec, spec_dict):
711718
}
712719
data = json.dumps(data_json)
713720
headers = {
714-
'api_key': '12345',
721+
'api_key': self.api_key_encoded,
715722
}
716723

717724
request = MockRequest(

tests/integration/test_validators.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from base64 import b64encode
12
import json
23
import pytest
34

@@ -22,6 +23,12 @@ class TestRequestValidator(object):
2223

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

26+
api_key = b'12345'
27+
28+
@property
29+
def api_key_encoded(self):
30+
return b64encode(self.api_key)
31+
2532
@pytest.fixture
2633
def spec_dict(self, factory):
2734
return factory.spec_from_file("data/v3.0/petstore.yaml")
@@ -88,7 +95,7 @@ def test_get_pets(self, validator):
8895

8996
def test_missing_body(self, validator):
9097
headers = {
91-
'api_key': '12345',
98+
'api_key': self.api_key_encoded,
9299
}
93100
cookies = {
94101
'user': '123',
@@ -106,7 +113,7 @@ def test_missing_body(self, validator):
106113
assert result.body is None
107114
assert result.parameters == {
108115
'header': {
109-
'api_key': 12345,
116+
'api_key': self.api_key,
110117
},
111118
'cookie': {
112119
'user': 123,
@@ -115,7 +122,7 @@ def test_missing_body(self, validator):
115122

116123
def test_invalid_content_type(self, validator):
117124
headers = {
118-
'api_key': '12345',
125+
'api_key': self.api_key_encoded,
119126
}
120127
cookies = {
121128
'user': '123',
@@ -133,7 +140,7 @@ def test_invalid_content_type(self, validator):
133140
assert result.body is None
134141
assert result.parameters == {
135142
'header': {
136-
'api_key': 12345,
143+
'api_key': self.api_key,
137144
},
138145
'cookie': {
139146
'user': 123,
@@ -159,7 +166,7 @@ def test_post_pets(self, validator, spec_dict):
159166
}
160167
data = json.dumps(data_json)
161168
headers = {
162-
'api_key': '12345',
169+
'api_key': self.api_key_encoded,
163170
}
164171
cookies = {
165172
'user': '123',
@@ -175,7 +182,7 @@ def test_post_pets(self, validator, spec_dict):
175182
assert result.errors == []
176183
assert result.parameters == {
177184
'header': {
178-
'api_key': 12345,
185+
'api_key': self.api_key,
179186
},
180187
'cookie': {
181188
'user': 123,

tests/unit/schema/test_schemas.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,25 @@ def test_string_format_binary(self, value):
478478

479479
assert result == value
480480

481+
@pytest.mark.parametrize('value', [
482+
u('tsssst'), u('dGVzdA=='),
483+
])
484+
def test_string_format_byte_invalid(self, value):
485+
schema = Schema('string', schema_format='byte')
486+
487+
with pytest.raises(OpenAPISchemaError):
488+
schema.validate(value)
489+
490+
@pytest.mark.parametrize('value', [
491+
b('tsssst'), b('dGVzdA=='),
492+
])
493+
def test_string_format_byte(self, value):
494+
schema = Schema('string', schema_format='byte')
495+
496+
result = schema.validate(value)
497+
498+
assert result == value
499+
481500
@pytest.mark.parametrize('value', [
482501
u('test'), b('stream'), datetime.date(1989, 1, 2),
483502
datetime.datetime(1989, 1, 2, 0, 0, 0),

0 commit comments

Comments
 (0)