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

Empty request when using custom validators #71

Closed
tesarm opened this issue May 26, 2017 · 4 comments
Closed

Empty request when using custom validators #71

tesarm opened this issue May 26, 2017 · 4 comments

Comments

@tesarm
Copy link

tesarm commented May 26, 2017

Swagger doesn't generate request object for services with custom validators:

How to reproduce:

from cornice import Service
from cornice.validators import colander_body_validator 

def my_validator(request, schema=None, deserializer=None, **kwargs):
    return colander_body_validator(request, schema, deserializer, **kwargs)

class MySchema(colander.Schema):
    dummy = colander.SchemaNode(colander.String())

documented = Service(name='my_service', path='/my-url')
@documented.post(schema=MySchema(), validators=(colander_body_validator,))
def documented_post(self):
        return {}

undocumented = Service(name='another_service', path='/another-url')
@undocumented.post(schema=MySchema(), validators=(my_validator,))
def undocumented_post(self):
        return {}

The reason why I need to use custom validator is because I want to bind request to schema. My actual validator looks like the function below, but it doesn't make any difference if I call it with modifications

def my_validator(request, schema=None, deserializer=None, **kwargs):
    return colander_body_validator(request, schema.bind(request=request), deserializer, **kwargs)
@jomasti
Copy link
Contributor

jomasti commented May 27, 2017

I can't check right now, but I think you can create a body schema transfomer for the validator and append it to the list of transformers that are ran:

def body_schema_transformer(schema, args):
    validators = args.get('validators', [])
    if my_validator in validators:
        body_schema = schema
        schema = colander.MappingSchema()
        schema['body'] = body_schema
    return schema

@swagger.get()
def swagger_spec(request):
    ...
    generator = CorniceSwagger(get_services())
    generator.schema_transformers.append(body_schema_transformer)
    ...

@gabisurita
Copy link
Collaborator

gabisurita commented May 27, 2017

@jomasti answer should do the job. Currently that's not documented, but it definitely should be on this section.

@gabisurita
Copy link
Collaborator

Related to #51

@gabisurita
Copy link
Collaborator

@tesarm I've updated the documentation and assumed this to be solved. Feel free to reopen it if you have more problems. :)

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