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

Add upgrading docs for major versions (fixes #385) #387

Merged
merged 1 commit into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Documentation content
api
internals
faq
upgrading


Contribution & Feedback
Expand Down
125 changes: 125 additions & 0 deletions docs/source/upgrading.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
Upgrading
#########

1.X to 2.X
==========

Validators
----------

Validators now receive the kwargs of the related service definition.

Before:

.. code-block:: python

def has_payed(request):
if 'paid' not in request.GET:
request.errors.add('body', 'paid', 'You must pay!')

After:

.. code-block:: python

def has_payed(request, **kwargs):
free_access = kwargs.get('free_access')
if not free_access and 'paid' not in request.GET:
request.errors.add('body', 'paid', 'You must pay!')


Colander validation
-------------------

Colander schema validation now requires an explicit validator, and an additional
level of mapping for ``body``, ``querystring`` or ``headers`` instead of the former
``location`` attribute. The ``request.validated`` hences reflects this additional level.

Before:

.. code-block:: python

class SignupSchema(colander.MappingSchema):
username = colander.SchemaNode(colander.String(), location='body')
referrer = colander.SchemaNode(colander.String(), location='querystring',
missing=colander.drop)

@signup.get(schema=SignupSchema)
def signup_get(request):
username = request.validated['username']
referrer = request.validated['referrer']
return {'success': True}

After:

.. code-block:: python

from cornice.validators import colander_validator

class Querystring(colander.MappingSchema):
referrer = colander.SchemaNode(colander.String(), missing=colander.drop)

class Payload(colander.MappingSchema):
username = colander.SchemaNode(colander.String())

class SignupSchema(colander.MappingSchema):
body = Payload()
querystring = Querystring()

signup = cornice.Service()

@signup.get(schema=SignupSchema, validators=(colander_validator,))
def signup_get(request):
username = request.validated['body']['username']
referrer = request.validated['querystring']['referrer']
return {'success': True}


Error handler
-------------

* The ``error_handler`` callback of services now receives a ``request`` object instead of ``errors``.

Before:

.. code-block:: python

def xml_error(errors):
request = errors.request
...

After:

.. code-block:: python

def xml_error(request):
errors = request.errors
...


Deserializers
-------------

The support of ``config.add_deserializer()`` and ``config.registry.cornice_deserializers``
was dropped.


Services schemas introspection
------------------------------

The ``schema`` argument of services is now treated as service kwarg.
The ``service.schemas_for()`` method was dropped as well as the ``service.schemas``
property.

Before:

.. code-block:: python

schema = service.schemas_for(method="POST")

After:

.. code-block:: python

schema = [kwargs['schema'] for method, view, kwargs in service.definitions
if method == "POST"][0]