Skip to content

Commit 644ff4c

Browse files
committed
Merge branch 'p4l1ly-custom_template'
2 parents 4833738 + 7e3b001 commit 644ff4c

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

aiohttp_swagger/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def setup_swagger(app: web.Application,
4848
swagger_home_decor: FunctionType = None,
4949
swagger_def_decor: FunctionType = None,
5050
swagger_info: dict = None,
51+
swagger_template_path: str = None,
5152
definitions: dict = None,
5253
security_definitions: dict = None):
5354
_swagger_url = ("/{}".format(swagger_url)
@@ -64,8 +65,9 @@ def setup_swagger(app: web.Application,
6465
swagger_info = generate_doc_from_each_end_point(
6566
app, api_base_url=api_base_url, description=description,
6667
api_version=api_version, title=title, contact=contact,
68+
template_path=swagger_template_path,
6769
definitions=definitions,
68-
security_definitions=security_definitions,
70+
security_definitions=security_definitions
6971
)
7072
else:
7173
swagger_info = json.dumps(swagger_info)

aiohttp_swagger/helpers/builders.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def generate_doc_from_each_end_point(
7070
api_version: str = "1.0.0",
7171
title: str = "Swagger API",
7272
contact: str = "",
73+
template_path: str = None,
7374
definitions: dict = None,
7475
security_definitions: dict = None):
7576
# Clean description
@@ -95,7 +96,10 @@ def nesteddict2yaml(d, indent=10, result=""):
9596
jinja2_env = Environment(loader=BaseLoader())
9697
jinja2_env.filters['nesteddict2yaml'] = nesteddict2yaml
9798

98-
with open(join(SWAGGER_TEMPLATE, "swagger.yaml"), "r") as f:
99+
if template_path is None:
100+
template_path = join(SWAGGER_TEMPLATE, "swagger.yaml")
101+
102+
with open(template_path, "r") as f:
99103
swagger_base = (
100104
jinja2_env.from_string(f.read()).render(
101105
description=cleaned_description,

doc/source/customizing.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ You can change this valued for Swagger doc:
88
3. API **Version**: Version of your API
99
4. **Title**: Title for your API
1010
5. **Contact**: Contact info.
11+
6. **Template Path**:Path to custom swagger template file.
1112

1213
.. code-block:: python
1314
@@ -44,14 +45,44 @@ You can change this valued for Swagger doc:
4445
description=long_description,
4546
title="My Custom Title",
4647
api_version="1.0.3",
47-
contact="[email protected]")
48+
49+
template_path="my/custom/path/to/swagger.yaml")
4850
4951
web.run_app(app, host="127.0.0.1")
5052
5153
It produces:
5254

5355
.. image:: _static/swagger_custom_params.jpg
5456

57+
Follows default swagger Jinja2 template which is used in the library:
58+
59+
.. code-block:: yaml
60+
61+
swagger: "2.0"
62+
info:
63+
description: |
64+
{{ description }}
65+
version: "{{ version }}"
66+
title: {{ title }}
67+
{% if contact %}
68+
contact:
69+
name: {{ contact }}
70+
{% endif %}
71+
basePath: {{ base_path }}
72+
schemes:
73+
- http
74+
- https
75+
{% if definitions %}
76+
definitions:
77+
{{ definitions|nesteddict2yaml }}
78+
{% endif %}
79+
{% if security_definitions %}
80+
securityDefinitions:
81+
{{ security_definitions|nesteddict2yaml }}
82+
{% endif %}
83+
paths:
84+
85+
5586
Adding Swagger from external file
5687
---------------------------------
5788

0 commit comments

Comments
 (0)