Skip to content

Commit

Permalink
Add note about ordered Schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Sep 16, 2019
1 parent a4a31c6 commit 12c9f90
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
25 changes: 25 additions & 0 deletions docs/openapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,31 @@ The :meth:`Api.register_converter` allows to register a converter in the
@blp.route('/pets/{objectid:pet_id}')
...
Enforce Field Order in Documentation
------------------------------------

In the OpenAPI specification file, the fields of a ``Schema`` are documented as
schema `properties`. Although objects are not ordered in JSON, OpenAPI
graphical interfaces tend to respect the order in which the `properties` are
defined in the ``properties`` object in the specification file.

When using an ordererd ``Schema``, the fields definition order is preserved
when generating the specification file and the `properties` are displayed in
that order.

This is typically done in a base class:

.. code-block:: python
:emphasize-lines: 2,3
class MyBaseSchema(ma.Schema):
class Meta:
ordered = True
class User(MyBaseSchema):
name = ma.fields.String()
surname = ma.fields.String()
Serve the OpenAPI Documentation
-------------------------------

Expand Down
8 changes: 0 additions & 8 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ Define a marshmallow :class:`Schema <marshmallow.Schema>` to expose the model.
.. code-block:: python
class PetSchema(ma.Schema):
class Meta:

This comment has been minimized.

Copy link
@sloria

sloria Sep 16, 2019

Member

+1.

My guess is that ordered = True is completely unnecessary on Python>=3.6, since dictionary order is guaranteed to be in insertion order.

This comment has been minimized.

Copy link
@lafrech

lafrech Sep 16, 2019

Author Member

Yeah. Well, Python 3.7, technically, but yes.

https://stackoverflow.com/a/39980744

When we drop 3.5 and 3.6 in marshmallow, there is not much interest in keeping "ordered".

This comment has been minimized.

Copy link
@lafrech

lafrech Sep 16, 2019

Author Member

I added a note about Python >=3.7 or CPython 3.6.

ordered = True
id = ma.fields.Int(dump_only=True)
name = ma.fields.String()
Expand All @@ -62,10 +58,6 @@ query arguments.
.. code-block:: python
class PetQueryArgsSchema(ma.Schema):
class Meta:
ordered = True
name = ma.fields.String()
Expand Down

0 comments on commit 12c9f90

Please sign in to comment.