Skip to content
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

Declaring a validation schema other than MappingSchema may throw an uncaught exception #250

Closed
ltvolks opened this issue Sep 18, 2014 · 2 comments

Comments

@ltvolks
Copy link
Contributor

ltvolks commented Sep 18, 2014

The colander.Mapping type is the only type that has the unknown attribute. During schema validation, this attribute is accessed.

Therefore any otherwise valid body will thrown an exception on validation if the declared schema type is not a colander.Mapping. Essentially this means anything other than a colander.MappingSchema.

Example:

import colander
from cornice.service import Service
from pyramid.config import Configurator
from webtest import TestApp


class ValidationSchema(colander.MappingSchema):
    key = colander.SchemaNode(colander.String())

class ListSchema(colander.SequenceSchema):
    key = colander.SchemaNode(colander.Int())

svc_validation = Service(name="test-validation", path="/test-validation")


@svc_validation.post(schema=ListSchema)
def post(request):
    return {"test": "succeeded"}


config = Configurator()
config.include("cornice")
config.scan()
app = config.make_wsgi_app()

client = TestApp(app)

# Throws an exception
# AttributeError: 'Sequence' object has no attribute 'unknown'
client.post_json('/test-validation', {'key': [1, 2, 3]})

Traceback:

  File "/venvs/cornice-test/src/cornice/cornice/service.py", line 495, in wrapper
    validate_colander_schema(args['schema'], request)
  File "/venvs/cornice-test/src/cornice/cornice/schemas.py", line 151, in validate_colander_schema
    if schema.colander_schema.typ.unknown == 'raise':
AttributeError: 'Sequence' object has no attribute 'unknown'

Somewhat related: #249

@tisdall
Copy link
Contributor

tisdall commented Jun 15, 2015

Essentially this means anything other than a colander.MappingSchema.

Not exactly true...

>>> import colander
>>> x = colander.MappingSchema(colander.Sequence())
>>> x.typ
<colander.Sequence object at 0x7fb908fc6208>
>>> x.typ.unknown
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Sequence' object has no attribute 'unknown'
>>> 

#309 fixes this issue

@leplatrem
Copy link
Contributor

Schema typing was reworked in Cornice 2.0 (current master), and this may not apply anymore. Please reopen if your usecase is not covered by the new API. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants