cornice-apispec 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cornice_apispec-0.1.0/LICENSE +28 -0
- cornice_apispec-0.1.0/PKG-INFO +63 -0
- cornice_apispec-0.1.0/README.md +32 -0
- cornice_apispec-0.1.0/pyproject.toml +44 -0
- cornice_apispec-0.1.0/src/cornice_apispec/__init__.py +125 -0
- cornice_apispec-0.1.0/src/cornice_apispec/generator.py +220 -0
- cornice_apispec-0.1.0/src/cornice_apispec/marshmallow.py +72 -0
- cornice_apispec-0.1.0/src/cornice_apispec/predicates.py +62 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, Robbin
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cornice-apispec
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: OpenAPI spec generation for Cornice/Pyramid with Marshmallow and OneOfSchema support
|
|
5
|
+
License: BSD-3-Clause
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: pyramid,openapi,apispec,marshmallow,cornice,swagger
|
|
8
|
+
Author: Robbin
|
|
9
|
+
Requires-Python: >=3.11,<4.0
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Framework :: Pyramid
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
20
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Requires-Dist: apispec (>=6.0.0)
|
|
23
|
+
Requires-Dist: cornice (>=6.0.0)
|
|
24
|
+
Requires-Dist: marshmallow (>=3.13.0,<4.0.0)
|
|
25
|
+
Requires-Dist: marshmallow-oneofschema (>=3.0.1,<4.0.0)
|
|
26
|
+
Requires-Dist: pyramid-apispec (>=0.4)
|
|
27
|
+
Project-URL: Homepage, https://github.com/cartaorobbin/cornice-apispec
|
|
28
|
+
Project-URL: Repository, https://github.com/cartaorobbin/cornice-apispec
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# cornice-apispec
|
|
32
|
+
|
|
33
|
+
OpenAPI spec generation for Cornice/Pyramid with Marshmallow and OneOfSchema support.
|
|
34
|
+
|
|
35
|
+
Forked from [pycornmarsh](https://github.com/niccolofanton/pycornmarsh) with a fix for `OneOfSchema` component name collisions when multiple API versions define schemas with the same class name.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install cornice-apispec
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## The Problem
|
|
44
|
+
|
|
45
|
+
When using `marshmallow-oneofschema` with multiple API versions (e.g. `v1.PartSchema` and `v3.PartSchema`), the OpenAPI spec generator would raise `DuplicateComponentNameError` because both schemas resolve to the same component name `PartSchema`.
|
|
46
|
+
|
|
47
|
+
## The Fix
|
|
48
|
+
|
|
49
|
+
`OneofOpenAPIConverter` now detects name collisions and automatically qualifies the component name with a version prefix extracted from the schema's module path (e.g. `V3PartSchema`).
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from cornice_apispec import get_spec
|
|
55
|
+
|
|
56
|
+
# In your Pyramid app
|
|
57
|
+
config.include("cornice_apispec")
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
BSD-3-Clause
|
|
63
|
+
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# cornice-apispec
|
|
2
|
+
|
|
3
|
+
OpenAPI spec generation for Cornice/Pyramid with Marshmallow and OneOfSchema support.
|
|
4
|
+
|
|
5
|
+
Forked from [pycornmarsh](https://github.com/niccolofanton/pycornmarsh) with a fix for `OneOfSchema` component name collisions when multiple API versions define schemas with the same class name.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install cornice-apispec
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## The Problem
|
|
14
|
+
|
|
15
|
+
When using `marshmallow-oneofschema` with multiple API versions (e.g. `v1.PartSchema` and `v3.PartSchema`), the OpenAPI spec generator would raise `DuplicateComponentNameError` because both schemas resolve to the same component name `PartSchema`.
|
|
16
|
+
|
|
17
|
+
## The Fix
|
|
18
|
+
|
|
19
|
+
`OneofOpenAPIConverter` now detects name collisions and automatically qualifies the component name with a version prefix extracted from the schema's module path (e.g. `V3PartSchema`).
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from cornice_apispec import get_spec
|
|
25
|
+
|
|
26
|
+
# In your Pyramid app
|
|
27
|
+
config.include("cornice_apispec")
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
BSD-3-Clause
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "cornice-apispec"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "OpenAPI spec generation for Cornice/Pyramid with Marshmallow and OneOfSchema support"
|
|
5
|
+
authors = ["Robbin"]
|
|
6
|
+
license = "BSD-3-Clause"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
packages = [{include = "cornice_apispec", from = "src"}]
|
|
9
|
+
homepage = "https://github.com/cartaorobbin/cornice-apispec"
|
|
10
|
+
repository = "https://github.com/cartaorobbin/cornice-apispec"
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 4 - Beta",
|
|
13
|
+
"Framework :: Pyramid",
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"License :: OSI Approved :: BSD License",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
20
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
21
|
+
]
|
|
22
|
+
keywords = ["pyramid", "openapi", "apispec", "marshmallow", "cornice", "swagger"]
|
|
23
|
+
|
|
24
|
+
[tool.poetry.dependencies]
|
|
25
|
+
python = "^3.11"
|
|
26
|
+
pyramid-apispec = ">=0.4"
|
|
27
|
+
cornice = ">=6.0.0"
|
|
28
|
+
marshmallow = ">=3.13.0,<4.0.0"
|
|
29
|
+
marshmallow-oneofschema = ">=3.0.1,<4.0.0"
|
|
30
|
+
apispec = ">=6.0.0"
|
|
31
|
+
|
|
32
|
+
[tool.poetry.group.dev.dependencies]
|
|
33
|
+
pytest = ">=7.0"
|
|
34
|
+
ruff = ">=0.1.0"
|
|
35
|
+
|
|
36
|
+
[build-system]
|
|
37
|
+
requires = ["poetry-core"]
|
|
38
|
+
build-backend = "poetry.core.masonry.api"
|
|
39
|
+
|
|
40
|
+
[tool.ruff]
|
|
41
|
+
line-length = 100
|
|
42
|
+
|
|
43
|
+
[tool.pytest.ini_options]
|
|
44
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import importlib
|
|
3
|
+
|
|
4
|
+
from apispec import APISpec
|
|
5
|
+
from apispec.exceptions import DuplicateComponentNameError
|
|
6
|
+
|
|
7
|
+
from .generator import get_schema_name, AutoDoc
|
|
8
|
+
|
|
9
|
+
from .marshmallow import MarshmallowPlugin
|
|
10
|
+
|
|
11
|
+
from .predicates import register as predicates_register
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def includeme(config):
|
|
15
|
+
config.include("pyramid_apispec.views")
|
|
16
|
+
|
|
17
|
+
def spec_view(request):
|
|
18
|
+
version = request.matchdict.get("version")
|
|
19
|
+
if not version:
|
|
20
|
+
return {}
|
|
21
|
+
try:
|
|
22
|
+
_path = f"{config.root_package.__name__}.views.api.{version}.apispecview"
|
|
23
|
+
m = importlib.import_module(_path)
|
|
24
|
+
except ImportError:
|
|
25
|
+
request.response.status = 404
|
|
26
|
+
return {}
|
|
27
|
+
return m.api_spec(request)
|
|
28
|
+
|
|
29
|
+
config.add_route("openapi_spec", "/{version}/openapi.json")
|
|
30
|
+
config.add_view(spec_view, route_name="openapi_spec", renderer="json")
|
|
31
|
+
|
|
32
|
+
config.pyramid_apispec_add_explorer(
|
|
33
|
+
spec_route_name="openapi_spec",
|
|
34
|
+
explorer_route_path="/{version}/api-explorer",
|
|
35
|
+
)
|
|
36
|
+
predicates_register(config)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class InstropectedView:
|
|
40
|
+
def __init__(self, view):
|
|
41
|
+
self.view = view
|
|
42
|
+
self.introspectable = view["introspectable"]
|
|
43
|
+
|
|
44
|
+
def __getattr__(self, attr):
|
|
45
|
+
return self.introspectable.get(attr)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def get_uri_placeholders(uri_pattern):
|
|
49
|
+
"""pattern: {any}"""
|
|
50
|
+
return re.findall("{(.*?)}", uri_pattern)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def get_spec(request, title, version, description=None, security_scheme=None):
|
|
54
|
+
spec = APISpec(
|
|
55
|
+
title=title,
|
|
56
|
+
version=version,
|
|
57
|
+
openapi_version="3.0.2",
|
|
58
|
+
plugins=[MarshmallowPlugin()],
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
introspector = request.registry.introspector
|
|
62
|
+
|
|
63
|
+
for view in introspector.get_category("views"):
|
|
64
|
+
intro = InstropectedView(view)
|
|
65
|
+
show_apispec = intro.pcm_show or ""
|
|
66
|
+
|
|
67
|
+
if not all(
|
|
68
|
+
(show_apispec == request.matchdict.get("version"), intro.request_methods)
|
|
69
|
+
):
|
|
70
|
+
continue
|
|
71
|
+
|
|
72
|
+
# Check option before skip
|
|
73
|
+
if intro.request_methods.lower() in ["head", "option"]:
|
|
74
|
+
continue
|
|
75
|
+
|
|
76
|
+
route = introspector.get("routes", intro.route_name)
|
|
77
|
+
original_pattern = route["pattern"]
|
|
78
|
+
cornice_service = request.registry.cornice_services.get(original_pattern)
|
|
79
|
+
|
|
80
|
+
autodoc = AutoDoc(
|
|
81
|
+
method=intro.request_methods,
|
|
82
|
+
introspectable_view=intro.introspectable,
|
|
83
|
+
cornice_service=cornice_service,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
if intro.pcm_request:
|
|
87
|
+
for _, v in intro.pcm_request.items():
|
|
88
|
+
try:
|
|
89
|
+
name_ = get_schema_name(v)
|
|
90
|
+
if name_ not in spec.components.schemas.keys():
|
|
91
|
+
spec.components.schema(name_, schema=v)
|
|
92
|
+
except DuplicateComponentNameError:
|
|
93
|
+
pass
|
|
94
|
+
|
|
95
|
+
if intro.pcm_responses:
|
|
96
|
+
for _, v in intro.pcm_responses.items():
|
|
97
|
+
try:
|
|
98
|
+
name_ = get_schema_name(v)
|
|
99
|
+
if name_ not in spec.components.schemas.keys():
|
|
100
|
+
spec.components.schema(name_, schema=v)
|
|
101
|
+
except DuplicateComponentNameError:
|
|
102
|
+
pass
|
|
103
|
+
|
|
104
|
+
path_parameters = get_uri_placeholders(original_pattern)
|
|
105
|
+
if path_parameters:
|
|
106
|
+
autodoc.add_path_parameter(path_parameters)
|
|
107
|
+
|
|
108
|
+
_op = autodoc.to_dict()
|
|
109
|
+
|
|
110
|
+
spec.path(path="/" + original_pattern, operations=_op)
|
|
111
|
+
|
|
112
|
+
if security_scheme:
|
|
113
|
+
for k, v in security_scheme.items():
|
|
114
|
+
spec.components.security_scheme(k, v)
|
|
115
|
+
|
|
116
|
+
openapi_spec = spec.to_dict()
|
|
117
|
+
|
|
118
|
+
if description:
|
|
119
|
+
openapi_spec["info"].update({"description": description})
|
|
120
|
+
|
|
121
|
+
scheme = request.scheme # Or force to https
|
|
122
|
+
main_server_url = "{}://{}".format(scheme, request.host)
|
|
123
|
+
openapi_spec.update({"servers": [{"url": main_server_url}]})
|
|
124
|
+
|
|
125
|
+
return openapi_spec
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
import hashlib
|
|
3
|
+
|
|
4
|
+
DEFAULT_CONTENT_TYPE = "application/json"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_schema_name(schema):
|
|
8
|
+
if inspect.isclass(schema):
|
|
9
|
+
return schema.__name__
|
|
10
|
+
|
|
11
|
+
if hasattr(schema, "__apispec__") and schema.__apispec__.get("model"):
|
|
12
|
+
return schema.__apispec__.get("model")
|
|
13
|
+
|
|
14
|
+
if "exclude" in schema.__dict__ and schema.exclude:
|
|
15
|
+
key = "{}".format(list(schema.__dict__.get("exclude"))).encode("utf-8")
|
|
16
|
+
|
|
17
|
+
return "{}-{}".format(
|
|
18
|
+
schema.__class__.__name__, hashlib.md5(key).hexdigest()[:5]
|
|
19
|
+
)
|
|
20
|
+
return schema.__class__.__name__
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
VALIDATOR_FOR_OPEN_API = {"querystring": "query", "headers": "header", "path": "path"}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class AutoDoc(object):
|
|
27
|
+
def __init__(self, method, introspectable_view, cornice_service):
|
|
28
|
+
self.method = method
|
|
29
|
+
self.view = introspectable_view
|
|
30
|
+
self.view_operations = {self.method.lower(): {}}
|
|
31
|
+
self.cornice_service = cornice_service
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def content_type(self):
|
|
35
|
+
content_type_info = self.cornice_service.get_contenttypes(self.method)
|
|
36
|
+
try:
|
|
37
|
+
return content_type_info[0] or DEFAULT_CONTENT_TYPE
|
|
38
|
+
except IndexError:
|
|
39
|
+
return DEFAULT_CONTENT_TYPE
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def request_schemas(self):
|
|
43
|
+
return self.view.get("pcm_request", {})
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def tags(self):
|
|
47
|
+
return self.view.get("pcm_tags", [])
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def summary(self):
|
|
51
|
+
return self.view.get("pcm_summary", "")
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def description(self):
|
|
55
|
+
return self.view.get("pcm_description", "")
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def security(self):
|
|
59
|
+
return self.view.get("pcm_security", None)
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def response_schemas(self):
|
|
63
|
+
return self.view.get("pcm_responses", [])
|
|
64
|
+
|
|
65
|
+
def add_path_parameter(self, path_parameters):
|
|
66
|
+
parameter_list = []
|
|
67
|
+
for parameter_name in path_parameters:
|
|
68
|
+
parameter_list += [
|
|
69
|
+
{
|
|
70
|
+
"name": parameter_name,
|
|
71
|
+
"in": "path",
|
|
72
|
+
"required": True,
|
|
73
|
+
"schema": self.get_type_from_field("String"),
|
|
74
|
+
"description": "{} parameter".format(parameter_name),
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
self._add_parameter(parameter_list)
|
|
78
|
+
|
|
79
|
+
def find_schema_for(self, location):
|
|
80
|
+
return self.request_schemas.get(location)
|
|
81
|
+
|
|
82
|
+
def _add_tags(self):
|
|
83
|
+
if self.tags:
|
|
84
|
+
self.view_operations[self.method.lower()].update({"tags": self.tags})
|
|
85
|
+
|
|
86
|
+
def _add_responses(self, responses_dict):
|
|
87
|
+
self.view_operations[self.method.lower()].update({"responses": responses_dict})
|
|
88
|
+
|
|
89
|
+
def _generate_responses(self):
|
|
90
|
+
if self.response_schemas:
|
|
91
|
+
responses_dict = {}
|
|
92
|
+
for status_code in self.response_schemas:
|
|
93
|
+
schema = self.response_schemas[status_code]
|
|
94
|
+
if isinstance(schema, str): # is only text description
|
|
95
|
+
status_code_dict = {
|
|
96
|
+
status_code: {"description": schema, "content": {}}
|
|
97
|
+
}
|
|
98
|
+
else:
|
|
99
|
+
schema_name = get_schema_name(schema)
|
|
100
|
+
if not inspect.isclass(schema) and schema.many:
|
|
101
|
+
schema_body = {
|
|
102
|
+
"type": "array",
|
|
103
|
+
"items": {
|
|
104
|
+
"$ref": "#/components/schemas/{}".format(schema_name)
|
|
105
|
+
},
|
|
106
|
+
}
|
|
107
|
+
else:
|
|
108
|
+
schema_body = {
|
|
109
|
+
"$ref": "#/components/schemas/{}".format(schema_name)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
status_code_dict = {
|
|
113
|
+
status_code: {
|
|
114
|
+
"description": schema.__doc__ or "",
|
|
115
|
+
"content": {self.content_type: {"schema": schema_body}},
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
responses_dict.update(status_code_dict)
|
|
119
|
+
# Update current method dict in Operation
|
|
120
|
+
self._add_responses(responses_dict)
|
|
121
|
+
|
|
122
|
+
def _add_parameter(self, parameter_list):
|
|
123
|
+
found_parameter_list = self.view_operations[self.method.lower()].get(
|
|
124
|
+
"parameters", []
|
|
125
|
+
)
|
|
126
|
+
for parameter in parameter_list:
|
|
127
|
+
found_parameter_list.append(parameter)
|
|
128
|
+
self.view_operations[self.method.lower()].update(
|
|
129
|
+
{"parameters": found_parameter_list}
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
def _generate_parameters(self):
|
|
133
|
+
def _observed_name(key):
|
|
134
|
+
field = schema._declared_fields[key]
|
|
135
|
+
return getattr(field, "data_key", key) or key
|
|
136
|
+
|
|
137
|
+
parameter_list = []
|
|
138
|
+
for parameter_in in ["querystring", "headers"]:
|
|
139
|
+
schema = self.find_schema_for(parameter_in)
|
|
140
|
+
if schema:
|
|
141
|
+
parameter_list += [
|
|
142
|
+
{
|
|
143
|
+
"name": _observed_name(key),
|
|
144
|
+
"in": VALIDATOR_FOR_OPEN_API.get(parameter_in, parameter_in),
|
|
145
|
+
"required": schema._declared_fields[key].required,
|
|
146
|
+
"schema": self.get_type_from_field(
|
|
147
|
+
schema._declared_fields[key]
|
|
148
|
+
),
|
|
149
|
+
"description": schema.__doc__ or "",
|
|
150
|
+
}
|
|
151
|
+
for key in schema._declared_fields
|
|
152
|
+
]
|
|
153
|
+
if parameter_list:
|
|
154
|
+
self._add_parameter(parameter_list)
|
|
155
|
+
|
|
156
|
+
def _generate_request_body(self):
|
|
157
|
+
body_schema = self.find_schema_for("body")
|
|
158
|
+
if body_schema:
|
|
159
|
+
request_body_dict = {
|
|
160
|
+
"content": {
|
|
161
|
+
self.content_type: {
|
|
162
|
+
"schema": {
|
|
163
|
+
"$ref": "#/components/schemas/{}".format(
|
|
164
|
+
get_schema_name(body_schema)
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
self._add_request_body(request_body_dict)
|
|
171
|
+
|
|
172
|
+
def _add_request_body(self, request_body_dict):
|
|
173
|
+
self.view_operations[self.method.lower()].update(
|
|
174
|
+
{"requestBody": request_body_dict}
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
def _add_summary(self):
|
|
178
|
+
if self.summary:
|
|
179
|
+
self.view_operations[self.method.lower()].update({"summary": self.summary})
|
|
180
|
+
|
|
181
|
+
def _add_description(self):
|
|
182
|
+
if self.description:
|
|
183
|
+
self.view_operations[self.method.lower()].update(
|
|
184
|
+
{"description": self.description}
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
def _add_security(self):
|
|
188
|
+
if self.security:
|
|
189
|
+
self.view_operations[self.method.lower()].update(
|
|
190
|
+
{"security": [{self.security: []}]}
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
def to_dict(self):
|
|
194
|
+
self._add_tags()
|
|
195
|
+
self._add_summary()
|
|
196
|
+
self._add_description()
|
|
197
|
+
self._add_security()
|
|
198
|
+
self._generate_request_body()
|
|
199
|
+
self._generate_parameters()
|
|
200
|
+
self._generate_responses()
|
|
201
|
+
return self.view_operations
|
|
202
|
+
|
|
203
|
+
@staticmethod
|
|
204
|
+
def get_type_from_field(field):
|
|
205
|
+
field_name = field.__class__.__name__
|
|
206
|
+
return {
|
|
207
|
+
"UUID": {"type": "string", "format": "uuid"},
|
|
208
|
+
"Number": {"type": "number"},
|
|
209
|
+
"Integer": {"type": "integer"},
|
|
210
|
+
"Decimal": {"type": "number"},
|
|
211
|
+
"Boolean": {"type": "boolean"},
|
|
212
|
+
"Float": {"type": "number", "format": "float"},
|
|
213
|
+
"DateTime": {"type": "string", "format": "date-time"},
|
|
214
|
+
"LocalDateTime": {"type": "string", "format": "date-time"},
|
|
215
|
+
"Time": {"type": "string", "format": "time"},
|
|
216
|
+
"Date": {"type": "string", "format": "date"},
|
|
217
|
+
"Email": {"type": "string", "format": "email"},
|
|
218
|
+
"Bool": {"type": "boolean"},
|
|
219
|
+
"Int": {"type": "integer"},
|
|
220
|
+
}.get(field_name, {"type": "string"})
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from apispec.ext import marshmallow
|
|
4
|
+
from apispec.ext.marshmallow import common
|
|
5
|
+
from marshmallow_oneofschema import OneOfSchema
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class OneofOpenAPIConverter(marshmallow.OpenAPIConverter):
|
|
9
|
+
|
|
10
|
+
@staticmethod
|
|
11
|
+
def _qualify_name(schema_cls, base_name):
|
|
12
|
+
"""Prefix with version segment from module path to avoid collision."""
|
|
13
|
+
module = schema_cls.__module__ or ""
|
|
14
|
+
match = re.search(r"\bv(\d+)\b", module)
|
|
15
|
+
if match:
|
|
16
|
+
return f"V{match.group(1)}{base_name}"
|
|
17
|
+
parts = module.rsplit(".", 2)
|
|
18
|
+
qualifier = parts[-2] if len(parts) >= 2 else "Alt"
|
|
19
|
+
return f"{qualifier.capitalize()}{base_name}"
|
|
20
|
+
|
|
21
|
+
def schema2jsonschema(self, schema):
|
|
22
|
+
if self.openapi_version.major < 3 or not is_oneof(schema):
|
|
23
|
+
return super().schema2jsonschema(schema)
|
|
24
|
+
|
|
25
|
+
mapping = {}
|
|
26
|
+
oneof = []
|
|
27
|
+
for name, type_schema in schema.type_schemas.items():
|
|
28
|
+
schema_instance = common.resolve_schema_instance(type_schema)
|
|
29
|
+
schema_key = common.make_schema_key(schema_instance)
|
|
30
|
+
|
|
31
|
+
if schema_key not in self.refs:
|
|
32
|
+
component_name = self.schema_name_resolver(type_schema) or name
|
|
33
|
+
|
|
34
|
+
# FIX: qualify name if it already exists (v3 vs v1 collision)
|
|
35
|
+
if component_name in self.spec.components.schemas:
|
|
36
|
+
component_name = self._qualify_name(
|
|
37
|
+
type(schema_instance), component_name
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
self.spec.components.schema(component_name, schema=type_schema)
|
|
41
|
+
|
|
42
|
+
ref_dict = self.get_ref_dict(schema_instance)
|
|
43
|
+
mapping[name] = ref_dict["$ref"]
|
|
44
|
+
oneof.append(ref_dict)
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
"oneOf": oneof,
|
|
48
|
+
"discriminator": {
|
|
49
|
+
"propertyName": schema.type_field,
|
|
50
|
+
"mapping": mapping,
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def is_oneof(schema):
|
|
56
|
+
return (isinstance(schema, type) and issubclass(schema, OneOfSchema)) or isinstance(
|
|
57
|
+
schema, OneOfSchema
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class MarshmallowPlugin(marshmallow.MarshmallowPlugin):
|
|
62
|
+
Converter = OneofOpenAPIConverter
|
|
63
|
+
|
|
64
|
+
def init_spec(self, spec):
|
|
65
|
+
super().init_spec(spec)
|
|
66
|
+
self.spec = spec
|
|
67
|
+
self.openapi_version = spec.openapi_version
|
|
68
|
+
self.openapi = OneofOpenAPIConverter(
|
|
69
|
+
openapi_version=spec.openapi_version,
|
|
70
|
+
schema_name_resolver=self.schema_name_resolver,
|
|
71
|
+
spec=spec,
|
|
72
|
+
)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
__all__ = [
|
|
4
|
+
"PCMResponseSchemasPredicate",
|
|
5
|
+
"PCMRequestSchemasPredicate",
|
|
6
|
+
"PCMTagsPredicate",
|
|
7
|
+
"PCMSummaryPredicate",
|
|
8
|
+
"PCMDescriptionPredicate",
|
|
9
|
+
"PCMSecurityPredicate",
|
|
10
|
+
"PCMShowInPredicate",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class _BasePCMSchemasPredicate:
|
|
15
|
+
PRED_ID = "pcm"
|
|
16
|
+
|
|
17
|
+
def __init__(self, val, _):
|
|
18
|
+
self.val = val
|
|
19
|
+
|
|
20
|
+
def text(self):
|
|
21
|
+
return "{} = {}".format(self.PRED_ID, self.val)
|
|
22
|
+
|
|
23
|
+
phash = text
|
|
24
|
+
|
|
25
|
+
def __call__(self, context, request):
|
|
26
|
+
return True
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class PCMResponseSchemasPredicate(_BasePCMSchemasPredicate):
|
|
30
|
+
PRED_ID = "pcm_responses"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class PCMRequestSchemasPredicate(_BasePCMSchemasPredicate):
|
|
34
|
+
PRED_ID = "pcm_request"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class PCMTagsPredicate(_BasePCMSchemasPredicate):
|
|
38
|
+
PRED_ID = "pcm_tags"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class PCMSummaryPredicate(_BasePCMSchemasPredicate):
|
|
42
|
+
PRED_ID = "pcm_summary"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class PCMDescriptionPredicate(_BasePCMSchemasPredicate):
|
|
46
|
+
PRED_ID = "pcm_description"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class PCMSecurityPredicate(_BasePCMSchemasPredicate):
|
|
50
|
+
PRED_ID = "pcm_security"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class PCMShowInPredicate(_BasePCMSchemasPredicate):
|
|
54
|
+
PRED_ID = "pcm_show"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def register(config):
|
|
58
|
+
this = sys.modules[__name__]
|
|
59
|
+
|
|
60
|
+
for c in __all__:
|
|
61
|
+
cls_ = getattr(this, c)
|
|
62
|
+
config.add_view_predicate(cls_.PRED_ID, cls_)
|