Skip to content

byte string format #111

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
Feb 28, 2019
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
7 changes: 5 additions & 2 deletions openapi_core/schema/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import attr
import functools
import logging
from base64 import b64decode, b64encode
from collections import defaultdict
from datetime import date, datetime
from uuid import UUID
import re
import warnings

from six import iteritems, integer_types, binary_type, text_type
from uuid import UUID

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

STRING_FORMAT_CALLABLE_GETTER = {
SchemaFormat.NONE: Format(text_type, TypeValidator(text_type)),
SchemaFormat.DATE: Format(format_date, TypeValidator(date, exclude=datetime)),
SchemaFormat.DATE: Format(
format_date, TypeValidator(date, exclude=datetime)),
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
SchemaFormat.UUID: Format(UUID, TypeValidator(UUID)),
SchemaFormat.BYTE: Format(b64decode, TypeValidator(binary_type)),
}

TYPE_VALIDATOR_CALLABLE_GETTER = {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/data/v3.0/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ paths:
- name: api_key
in: header
schema:
type: integer
format: int32
type: string
format: byte
required: true
- name: user
in: cookie
Expand Down
33 changes: 20 additions & 13 deletions tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import pytest
from base64 import b64encode
from uuid import UUID
from six import iteritems

Expand Down Expand Up @@ -30,6 +31,12 @@

class TestPetstore(object):

api_key = b'12345'

@property
def api_key_encoded(self):
return b64encode(self.api_key)

@pytest.fixture
def spec_dict(self, factory):
return factory.spec_from_file("data/v3.0/petstore.yaml")
Expand Down Expand Up @@ -424,7 +431,7 @@ def test_post_birds(self, spec, spec_dict):
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}
cookies = {
'user': '123',
Expand All @@ -440,7 +447,7 @@ def test_post_birds(self, spec, spec_dict):

assert parameters == {
'header': {
'api_key': 12345,
'api_key': self.api_key,
},
'cookie': {
'user': 123,
Expand Down Expand Up @@ -484,7 +491,7 @@ def test_post_cats(self, spec, spec_dict):
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}
cookies = {
'user': '123',
Expand All @@ -500,7 +507,7 @@ def test_post_cats(self, spec, spec_dict):

assert parameters == {
'header': {
'api_key': 12345,
'api_key': self.api_key,
},
'cookie': {
'user': 123,
Expand Down Expand Up @@ -544,7 +551,7 @@ def test_post_cats_boolean_string(self, spec, spec_dict):
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}
cookies = {
'user': '123',
Expand All @@ -560,7 +567,7 @@ def test_post_cats_boolean_string(self, spec, spec_dict):

assert parameters == {
'header': {
'api_key': 12345,
'api_key': self.api_key,
},
'cookie': {
'user': 123,
Expand Down Expand Up @@ -592,7 +599,7 @@ def test_post_no_one_of_schema(self, spec, spec_dict):
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}
cookies = {
'user': '123',
Expand All @@ -608,7 +615,7 @@ def test_post_no_one_of_schema(self, spec, spec_dict):

assert parameters == {
'header': {
'api_key': 12345,
'api_key': self.api_key,
},
'cookie': {
'user': 123,
Expand All @@ -631,7 +638,7 @@ def test_post_cats_only_required_body(self, spec, spec_dict):
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}
cookies = {
'user': '123',
Expand All @@ -647,7 +654,7 @@ def test_post_cats_only_required_body(self, spec, spec_dict):

assert parameters == {
'header': {
'api_key': 12345,
'api_key': self.api_key,
},
'cookie': {
'user': 123,
Expand All @@ -672,7 +679,7 @@ def test_post_pets_raises_invalid_mimetype(self, spec):
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}
cookies = {
'user': '123',
Expand All @@ -688,7 +695,7 @@ def test_post_pets_raises_invalid_mimetype(self, spec):

assert parameters == {
'header': {
'api_key': 12345,
'api_key': self.api_key,
},
'cookie': {
'user': 123,
Expand All @@ -711,7 +718,7 @@ def test_post_pets_missing_cookie(self, spec, spec_dict):
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}

request = MockRequest(
Expand Down
19 changes: 13 additions & 6 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from base64 import b64encode
import json
import pytest

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

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

api_key = b'12345'

@property
def api_key_encoded(self):
return b64encode(self.api_key)

@pytest.fixture
def spec_dict(self, factory):
return factory.spec_from_file("data/v3.0/petstore.yaml")
Expand Down Expand Up @@ -88,7 +95,7 @@ def test_get_pets(self, validator):

def test_missing_body(self, validator):
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}
cookies = {
'user': '123',
Expand All @@ -106,7 +113,7 @@ def test_missing_body(self, validator):
assert result.body is None
assert result.parameters == {
'header': {
'api_key': 12345,
'api_key': self.api_key,
},
'cookie': {
'user': 123,
Expand All @@ -115,7 +122,7 @@ def test_missing_body(self, validator):

def test_invalid_content_type(self, validator):
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}
cookies = {
'user': '123',
Expand All @@ -133,7 +140,7 @@ def test_invalid_content_type(self, validator):
assert result.body is None
assert result.parameters == {
'header': {
'api_key': 12345,
'api_key': self.api_key,
},
'cookie': {
'user': 123,
Expand All @@ -159,7 +166,7 @@ def test_post_pets(self, validator, spec_dict):
}
data = json.dumps(data_json)
headers = {
'api_key': '12345',
'api_key': self.api_key_encoded,
}
cookies = {
'user': '123',
Expand All @@ -175,7 +182,7 @@ def test_post_pets(self, validator, spec_dict):
assert result.errors == []
assert result.parameters == {
'header': {
'api_key': 12345,
'api_key': self.api_key,
},
'cookie': {
'user': 123,
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/schema/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,25 @@ def test_string_format_binary(self, value):

assert result == value

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

with pytest.raises(OpenAPISchemaError):
schema.validate(value)

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

result = schema.validate(value)

assert result == value

@pytest.mark.parametrize('value', [
u('test'), b('stream'), datetime.date(1989, 1, 2),
datetime.datetime(1989, 1, 2, 0, 0, 0),
Expand Down