Skip to content

Commit

Permalink
Merge pull request spec-first#228 from arjunrn/master
Browse files Browse the repository at this point in the history
Fix for when parameter spec is unordered.
  • Loading branch information
hjacobs committed May 17, 2016
2 parents 39edb1f + dba916c commit a83598d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
6 changes: 4 additions & 2 deletions connexion/decorators/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
language governing permissions and limitations under the License.
"""

import collections
import copy
import functools
import itertools
import logging
import sys

Expand Down Expand Up @@ -159,7 +159,9 @@ def validate_schema(self, data):

class ParameterValidator(object):
def __init__(self, parameters):
self.parameters = {k: list(g) for k, g in itertools.groupby(parameters, key=lambda p: p['in'])}
self.parameters = collections.defaultdict(list)
for p in parameters:
self.parameters[p['in']].append(p)

@staticmethod
def validate_parameter(parameter_type, value, param):
Expand Down
9 changes: 9 additions & 0 deletions tests/api/test_unordered_definition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import json


def test_app(unordered_definition_app):
app_client = unordered_definition_app.app.test_client()
response = app_client.get('/v1.0/unordered-params/1?first=first&second=2') # type: flask.Response
assert response.status_code == 400
response_data = json.loads(response.data.decode())
assert response_data['detail'] == 'Wrong type, expected \'integer\' for query parameter \'first\''
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,8 @@ def secure_endpoint_app():
@pytest.fixture(scope="session")
def secure_api_app():
return build_app_from_fixture('secure_api')


@pytest.fixture(scope="session")
def unordered_definition_app():
return build_app_from_fixture('unordered_definition')
4 changes: 4 additions & 0 deletions tests/fakeapi/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,7 @@ def get_invalid_response():
def get_custom_problem_response():
return problem(402, "You need to pay", "Missing amount",
ext={'amount': 23.0})


def unordered_params_response(first, path_param, second):
return dict(first=int(first), path_param=str(path_param), second=int(second))
30 changes: 30 additions & 0 deletions tests/fixtures/unordered_definition/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
swagger: "2.0"

info:
title: "{{title}}"
version: "1.0"

basePath: /v1.0

paths:
/unordered-params/{path_param}:
get:
summary: Mixed parameters in swagger definition
operationId: fakeapi.hello.unordered_params_response
responses:
200:
description: OK
parameters:
- name: first
in: query
type: integer
description: First Param
- name: path_param
in: path
required: true
type: string
description: Path Param
- name: second
in: query
type: integer
description: Second Param

0 comments on commit a83598d

Please sign in to comment.