Debian Codegraph @

docs/upgrading.rst

raw
1 Upgrading to Newer Releases
2 ===========================
3
4 This section documents migration paths to new releases.
5
6 Upgrading to 5.0.0
7 ------------------
8
9 Upgrading to 4.0.0
10 ------------------
11
12 location is ignored in field metadata
13 *************************************
14
15 ``location`` parameter is ignored in ``Field`` metadata. A ``Schema`` can only
16 have a single location.
17
18 A ``Schema`` with fields from different locations must be split into multiple
19 ``Schema``s.
20
21 Upgrading to 3.0.0
22 ------------------
23
24 Upgrading to 2.0.0
25 ------------------
26
27 plugin helpers must accept extra `**kwargs`
28 *******************************************
29
30 Since custom plugins helpers may define extra kwargs and those kwargs are passed
31 to all plugin helpers by :meth:`APISpec.path <APISpec.path>`, all plugins should
32 accept unknown kwargs.
33
34 The example plugin below defines an additional `func` argument and accepts extra
35 `**kwargs`.
36
37 .. code-block:: python
38 :emphasize-lines: 2
39
40 class MyPlugin(BasePlugin):
41 def path_helper(self, path, func, **kwargs):
42 """Path helper that parses docstrings for operations. Adds a
43 ``func`` parameter to `apispec.APISpec.path`.
44 """
45 operations.update(load_operations_from_docstring(func.__doc__))
46
47 Components must be referenced by ID, not full path
48 **************************************************
49
50 While apispec 1.x would let the user reference components by path or ID,
51 apispec 2.x only accepts references by ID.
52
53 .. code-block:: python
54
55 # apispec<2.0.0
56 spec.path(
57 path="/gist/{gist_id}",
58 operations=dict(
59 get=dict(
60 responses={
61 "200": {
62 "content": {
63 "application/json": {"schema": {"$ref": "#/definitions/Gist"}}
64 }
65 }
66 }
67 )
68 ),
69 )
70
71 # apispec>=2.0.0
72 spec.path(
73 path="/gist/{gist_id}",
74 operations=dict(
75 get=dict(
76 responses={"200": {"content": {"application/json": {"schema": "Gist"}}}}
77 )
78 ),
79 )
80
81 References by ID are accepted by both apispec 1.x ad 2.x and are a better
82 choice because they delegate the creation of the full component path to apispec.
83 This allows more flexibility as apispec creates the component path according to
84 the OpenAPI version.
85
86 Upgrading to 1.0.0
87 ------------------
88
89 ``openapi_version`` Is Required
90 *******************************
91
92 ``openapi_version`` no longer defaults to ``"2.0"``. It is now a
93 required argument.
94
95 .. code-block:: python
96 :emphasize-lines: 4
97
98 spec = APISpec(
99 title="Swagger Petstore",
100 version="1.0.0",
101 openapi_version="2.0", # or "3.0.2"
102 plugins=[MarshmallowPlugin()],
103 )
104
105 Web Framework Plugins Packaged Separately
106 *****************************************
107
108 ``apispec.ext.flask``, ``apispec.ext.bottle``, and
109 ``apispec.ext.tornado`` have been moved to a a separate package,
110 `apispec-webframeworks <https://github.com/marshmallow-code/apispec-webframeworks>`_.
111
112 If you use these plugins, install ``apispec-webframeworks`` with
113 ``pip``:
114
115 ::
116
117 $ pip install apispec-webframeworks
118
119 Then, update your imports:
120
121 .. code-block:: python
122
123 # apispec<1.0.0
124 from apispec.ext.flask import FlaskPlugin
125
126 # apispec>=1.0.0
127 from apispec_webframeworks.flask import FlaskPlugin
128
129
130 YAML Support Is Optional
131 ************************
132
133 YAML functionality is now optional. To install with YAML support:
134
135 ::
136
137 $ pip install 'apispec[yaml]'
138
139 You will need to do this if you use ``apispec-webframeworks`` or call
140 `APISpec.to_yaml <apispec.APISpec.to_yaml>` in your code.
141
142
143 Registering Entities
144 ********************
145
146 Methods for registering OAS entities are changed to the noun form
147 for internal consistency and for consistency with OAS v3 terminology.
148
149 .. code-block:: python
150
151 # apispec<1.0.0
152 spec.add_tag({"name": "Pet", "description": "Operations on pets"})
153 spec.add_path("/pets/", operations={...})
154 spec.definition("Pet", properties={...})
155 spec.add_parameter("PetID", "path", {...})
156
157 # apispec>=1.0.0
158 spec.tag({"name": "Pet", "description": "Operations on pets"})
159 spec.path("/pets/", operations={...})
160 spec.components.schema("Pet", {"properties": {...}})
161 spec.components.parameter("PetID", "path", {...})
162
163 Adding Additional Fields to Schemas
164 ***********************************
165
166 The ``extra_fields`` parameter to ``schema`` is removed. It is no longer
167 necessary. Pass all fields in to the component ``dict``.
168
169 .. code-block:: python
170
171 # <1.0.0
172 spec.definition("Pet", schema=PetSchema, extra_fields={"discriminator": "name"})
173
174 # >=1.0.0
175 spec.components.schema("Pet", schema=PetSchema, component={"discriminator": "name"})
176
177
178 Nested Schemas Are Referenced
179 *****************************
180
181 When using the `MarshmallowPlugin
182 <apispec.ext.marshmallow.MarshmallowPlugin>`, nested `Schema
183 <marshmallow.Schema>` classes are referenced (with ``"$ref"``) in the output spec.
184 By default, the name in the spec will be the class name with the "Schema" suffix
185 removed, e.g. ``fields.Nested(PetSchema())`` -> ``"#components/schemas/Pet"``.
186
187 The `ref` argument to `fields.Nested <marshmallow.fields.Nested>`_ is no
188 longer respected.
189
190
191 .. code-block:: python
192
193 # apispec<1.0.0
194 class PetSchema(Schema):
195 owner = fields.Nested(
196 HumanSchema,
197 # `ref` has no effect in 1.0.0. Remove.
198 ref="#components/schemas/Human",
199 )
200
201
202 # apispec>=1.0.0
203 class PetSchema(Schema):
204 owner = fields.Nested(HumanSchema)
205
206
207 .. seealso::
208
209 This behavior is customizable. See :ref:`marshmallow_nested_schemas`.