Skip to content

Commit

Permalink
Merge pull request #231 from macisamuele/maci-not-assume-that-body-is…
Browse files Browse the repository at this point in the history
…-present

pyramid-swagger should not assume that body is present
  • Loading branch information
macisamuele committed May 24, 2018
2 parents 2b91cb0 + 0f595b2 commit 4075554
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyramid_swagger/load_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def validate(self, values):
"""Validate a :class:`dict` of values. If `self.schema` is falsy this
is a noop.
"""
if not self.schema:
if not self.schema or (values is None and not self.schema.get('required', False)):
return
self.validator.validate(values)

Expand Down
5 changes: 4 additions & 1 deletion pyramid_swagger/tween.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ def files(self):
return result

def json(self, **kwargs):
return getattr(self.request, 'json_body', {})
if self.request.is_body_readable:
return getattr(self.request, 'json_body', {})
else:
return None


class PyramidSwaggerResponse(OutgoingResponse):
Expand Down
6 changes: 6 additions & 0 deletions tests/acceptance/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def date_view(request):
return request.swagger_data['body']


@view_config(route_name='post_endpoint_with_optional_body', request_method='POST', renderer='pyramid_swagger')
def post_endpoint_with_optional_body(request):
return request.content_length


@view_config(route_name='swagger_undefined', renderer='json')
def swagger_undefined(request):
return {}
Expand Down Expand Up @@ -77,6 +82,7 @@ def main(global_config, **settings):

config.add_route('echo_date', '/echo_date')
config.add_route('echo_date_json_renderer', '/echo_date_json_renderer')
config.add_route('post_endpoint_with_optional_body', '/post_endpoint_with_optional_body')

config.scan()
return config.make_wsgi_app()
Expand Down
17 changes: 16 additions & 1 deletion tests/acceptance/request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
from _pytest.fixtures import FixtureRequest
from mock import Mock
from pyramid.httpexceptions import exception_response
from webtest.utils import NoDefault

from pyramid_swagger import exceptions


# Parameterize pyramid_swagger.swagger_versions
@pytest.fixture(params=[['1.2'], ['2.0'], ['1.2', '2.0']])
@pytest.fixture(
params=[['1.2'], ['2.0'], ['1.2', '2.0']],
ids=['1.2', '2.0', '1.2-2.0'],
)
def test_app(request, **overrides):
"""Fixture for setting up a test test_app with particular settings."""
from .app import main
Expand Down Expand Up @@ -79,6 +83,17 @@ def test_echo_date_with_json_renderer(test_app):
assert response.json == input_object


@pytest.mark.parametrize(
'body, expected_length',
[
[NoDefault, 0],
[{}, 2],
],
)
def test_post_endpoint_with_optional_body(test_app, body, expected_length):
assert test_app.post_json('/post_endpoint_with_optional_body', body).json == expected_length


def test_200_with_form_params(test_app):
assert test_app.post(
'/post_with_form_params',
Expand Down
3 changes: 2 additions & 1 deletion tests/ingest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_get_swagger_schema_default():
}

swagger_schema = get_swagger_schema(settings)
assert len(swagger_schema.pyramid_endpoints) == 5
assert len(swagger_schema.pyramid_endpoints) == 6
assert swagger_schema.resource_validators


Expand All @@ -84,6 +84,7 @@ def test_generate_resource_listing():
{'path': '/echo_date'},
{'path': '/no_models'},
{'path': '/other_sample'},
{'path': '/post_endpoint_with_optional_body'},
{'path': '/sample'},
]
}
Expand Down
4 changes: 4 additions & 0 deletions tests/sample_schemas/good_app/api_docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
{
"path": "/echo_date",
"description": "Echoes the input body in the response. Endpoint used for verifying PyramidSwaggerRendererFactory"
},
{
"path": "/post_endpoint_with_optional_body",
"description": "Returns the request body length. Used to verify that optional body parameters are well handled"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"apiVersion": "0.1",
"swaggerVersion": "1.2",
"basePath": "http://localhost:9999/post_endpoint_with_optional_body",
"apis": [
{
"path": "/post_endpoint_with_optional_body",
"operations": [
{
"method": "POST",
"nickname": "post_endpoint_with_optional_body",
"type": "integer",
"parameters": [
{
"name": "body",
"paramType": "body",
"required": false,
"type": "object"
}
]
}
]
}
],
"models": {
"object": {
"id": "object",
"properties": {
}
}
}
}
26 changes: 26 additions & 0 deletions tests/sample_schemas/good_app/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"$ref": "#/definitions/object_with_formats"
}
Expand All @@ -39,13 +40,38 @@
"operationId": "echo_date"
}
},
"/post_endpoint_with_optional_body": {
"post": {
"description": "Returns the request body length. Used to verify that optional body parameters are well handled",
"parameters": [
{
"in": "body",
"name": "body",
"required": false,
"schema": {
"type": "object"
}
}
],
"responses": {
"200": {
"description": "HTTP/200 OK. Return request body length",
"schema": {
"type": "integer"
}
}
},
"operationId": "post_endpoint_with_optional_body"
}
},
"/echo_date_json_renderer": {
"post": {
"description": "This endpoint is used in tests/acceptance/request_test.py and requires to be identical to /echo_date endpoint",
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"$ref": "#/definitions/object_with_formats"
}
Expand Down

0 comments on commit 4075554

Please sign in to comment.