Skip to content

Commit

Permalink
Merge pull request #434 from gabisurita/top-level-body-arrays
Browse files Browse the repository at this point in the history
Add support for arrays on request body top level (fixes #433)
  • Loading branch information
leplatrem committed Jan 19, 2017
2 parents 9b73c5a + c01566d commit 31bbdb9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ CHANGELOG
2.4.0 (unreleased)
==================

- Nothing changed yet.
**Enhancements**

- Add support for arrays on request body top level.


2.3.0 (2016-12-15)
Expand Down
5 changes: 3 additions & 2 deletions cornice/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def extract_cstruct(request):
request.errors.add('body', '', 'Invalid JSON: %s' % e)
return {}
else:
if not hasattr(body, 'items'):
request.errors.add('body', '', 'Should be a JSON object')
if not hasattr(body, 'items') and not isinstance(body, list):
request.errors.add('body', '',
'Should be a JSON object or an array')
return {}
else:
body = {}
Expand Down
5 changes: 1 addition & 4 deletions cornice/validators/_colander.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ def body_validator(request, schema=None, deserializer=None, **kwargs):
class RequestSchema(colander.MappingSchema):
body = _ensure_instantiated(schema)

def deserialize(self, cstruct=colander.null):
appstruct = super(RequestSchema, self).deserialize(cstruct)
return appstruct['body']

validator(request, RequestSchema(), deserializer, **kwargs)
request.validated = request.validated.get('body', {})


def validator(request, schema=None, deserializer=None, **kwargs):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ def test_valid_nonstandard_json(self):
)
self.assertEqual(response.json['username'], 'man')

def test_valid_json_array(self):
app = self.make_ordinary_app()
response = app.post_json(
'/group_signup',
[{'username': 'hey'}, {'username': 'how'}]
)
self.assertEqual(response.json['data'],
[{'username': 'hey'}, {'username': 'how'}])

def test_invalid_json(self):
app = self.make_ordinary_app()
response = app.post('/signup',
Expand Down
10 changes: 10 additions & 0 deletions tests/validationapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ class SignupSchema(MappingSchema):
def signup_post(request):
return request.validated

group_signup = Service(name="group signup", path="/group_signup")

class GroupSignupSchema(SequenceSchema):
user = SignupSchema()

@group_signup.post(schema=GroupSignupSchema(),
validators=(colander_body_validator,))
def group_signup_post(request):
return {'data': request.validated}

def validate_bar(node, value):
if value != 'open':
raise Invalid(node, "The bar is not open.")
Expand Down

0 comments on commit 31bbdb9

Please sign in to comment.