1
1
# -*- coding: utf-8 -*-
2
- """Utilities for generating OpenAPI spec (fka Swagger) entities from
2
+ """Utilities for generating OpenAPI Specification (fka Swagger) entities from
3
3
marshmallow :class:`Schemas <marshmallow.Schema>` and :class:`Fields <marshmallow.fields.Field>`.
4
-
5
- OpenAPI 2.0 spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
6
4
"""
7
5
from __future__ import absolute_import , unicode_literals
8
6
import operator
61
59
62
60
# Properties that may be defined in a field's metadata that will be added to the output
63
61
# of field2property
64
- # https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0 .md#schemaObject
62
+ # https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2 .md#schemaObject
65
63
_VALID_PROPERTIES = {
66
64
'format' ,
67
65
'title' ,
@@ -334,11 +332,11 @@ def metadata2properties(self, field):
334
332
335
333
Will include field metadata that are valid properties of `OpenAPI schema
336
334
objects
337
- <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0 .md#schemaObject>`_
335
+ <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2 .md#schemaObject>`_
338
336
(e.g. “description”, “enum”, “example”).
339
337
340
338
In addition, `specification extensions
341
- <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2 .0.md#patterned-objects-9 >`_
339
+ <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3 .0.2. md#specification-extensions >`_
342
340
are supported. Prefix `x_` to the desired extension when passing the
343
341
keyword argument to the field constructor. apispec will convert `x_` to
344
342
`x-` to comply with OpenAPI.
@@ -366,7 +364,7 @@ def field2property(self, field, use_refs=True, name=None):
366
364
Will include field metadata that are valid properties of OpenAPI schema objects
367
365
(e.g. "description", "enum", "example").
368
366
369
- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0 .md#schemaObject
367
+ https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2 .md#schemaObject
370
368
371
369
:param Field field: A marshmallow field.
372
370
:param bool use_refs: Use JSONSchema ``refs``.
@@ -477,7 +475,7 @@ def schema2parameters(
477
475
of a single parameter; else return an array of a parameter for each included field in
478
476
the :class:`Schema <marshmallow.Schema>`.
479
477
480
- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0 .md#parameterObject
478
+ https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2 .md#parameterObject
481
479
"""
482
480
openapi_default_in = __location_map__ .get (default_in , default_in )
483
481
if self .openapi_version .major < 3 and openapi_default_in == 'body' :
@@ -508,15 +506,13 @@ def fields2parameters(self, fields, use_refs=True, default_in='body'):
508
506
of a single parameter; else return an array of a parameter for each included field in
509
507
the :class:`Schema <marshmallow.Schema>`.
510
508
511
- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject
512
-
513
509
In OpenAPI3, only "query", "header", "path" or "cookie" are allowed for the location
514
510
of parameters. In OpenAPI 3, "requestBody" is used when fields are in the body.
515
511
516
512
This function always returns a list, with a parameter
517
513
for each included field in the :class:`Schema <marshmallow.Schema>`.
518
514
519
- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1 .md#parameterObject
515
+ https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2 .md#parameterObject
520
516
"""
521
517
parameters = []
522
518
body_param = None
@@ -544,7 +540,7 @@ def field2parameter(self, field, name='body', use_refs=True, default_in='body'):
544
540
"""Return an OpenAPI parameter as a `dict`, given a marshmallow
545
541
:class:`Field <marshmallow.Field>`.
546
542
547
- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0 .md#parameterObject
543
+ https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2 .md#parameterObject
548
544
"""
549
545
location = field .metadata .get ('location' , None )
550
546
prop = self .field2property (field , use_refs = use_refs )
@@ -563,7 +559,7 @@ def property2parameter(
563
559
):
564
560
"""Return the Parameter Object definition for a JSON Schema property.
565
561
566
- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0 .md#parameterObject
562
+ https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2 .md#parameterObject
567
563
568
564
:param dict prop: JSON Schema property
569
565
:param str name: Field name
@@ -610,7 +606,7 @@ def schema2jsonschema(self, schema, **kwargs):
610
606
:class:`Schema <marshmallow.Schema>`. Schema may optionally provide the ``title`` and
611
607
``description`` class Meta options.
612
608
613
- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0 .md#schemaObject
609
+ https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2 .md#schemaObject
614
610
615
611
Example: ::
616
612
@@ -623,23 +619,16 @@ class Meta:
623
619
title = 'User'
624
620
description = 'A registered user'
625
621
626
- OpenAPI.schema2jsonschema(UserSchema)
627
- # {
628
- # 'title': 'User', 'description': 'A registered user',
629
- # 'properties': {
630
- # 'name': {'required': False,
631
- # 'description': '',
632
- # 'type': 'string'},
633
- # '_id': {'format': 'int32',
634
- # 'required': False,
635
- # 'description': '',
636
- # 'type': 'integer'},
637
- # 'email': {'format': 'email',
638
- # 'required': False,
639
- # 'description': 'email address of the user',
640
- # 'type': 'string'}
641
- # }
642
- # }
622
+ oaic = OpenAPIConverter(openapi_version='3.0.2', schema_name_resolver=resolver, spec=spec)
623
+ pprint(oaic.schema2jsonschema(UserSchema))
624
+ # {'description': 'A registered user',
625
+ # 'properties': {'_id': {'format': 'int32', 'type': 'integer'},
626
+ # 'email': {'description': 'email address of the user',
627
+ # 'format': 'email',
628
+ # 'type': 'string'},
629
+ # 'name': {'type': 'string'}},
630
+ # 'title': 'User',
631
+ # 'type': 'object'}
643
632
644
633
:param Schema schema: A marshmallow Schema instance or a class object
645
634
:rtype: dict, a JSON Schema Object
0 commit comments