Commit 644ff4ca authored by Goran Pavlovic's avatar Goran Pavlovic
Browse files

Merge branch 'p4l1ly-custom_template'

parents 48337381 7e3b001b
......@@ -48,6 +48,7 @@ def setup_swagger(app: web.Application,
swagger_home_decor: FunctionType = None,
swagger_def_decor: FunctionType = None,
swagger_info: dict = None,
swagger_template_path: str = None,
definitions: dict = None,
security_definitions: dict = None):
_swagger_url = ("/{}".format(swagger_url)
......@@ -64,8 +65,9 @@ def setup_swagger(app: web.Application,
swagger_info = generate_doc_from_each_end_point(
app, api_base_url=api_base_url, description=description,
api_version=api_version, title=title, contact=contact,
template_path=swagger_template_path,
definitions=definitions,
security_definitions=security_definitions,
security_definitions=security_definitions
)
else:
swagger_info = json.dumps(swagger_info)
......
......@@ -70,6 +70,7 @@ def generate_doc_from_each_end_point(
api_version: str = "1.0.0",
title: str = "Swagger API",
contact: str = "",
template_path: str = None,
definitions: dict = None,
security_definitions: dict = None):
# Clean description
......@@ -95,7 +96,10 @@ def generate_doc_from_each_end_point(
jinja2_env = Environment(loader=BaseLoader())
jinja2_env.filters['nesteddict2yaml'] = nesteddict2yaml
with open(join(SWAGGER_TEMPLATE, "swagger.yaml"), "r") as f:
if template_path is None:
template_path = join(SWAGGER_TEMPLATE, "swagger.yaml")
with open(template_path, "r") as f:
swagger_base = (
jinja2_env.from_string(f.read()).render(
description=cleaned_description,
......
......@@ -8,6 +8,7 @@ You can change this valued for Swagger doc:
3. API **Version**: Version of your API
4. **Title**: Title for your API
5. **Contact**: Contact info.
6. **Template Path**:Path to custom swagger template file.
.. code-block:: python
......@@ -44,7 +45,8 @@ You can change this valued for Swagger doc:
description=long_description,
title="My Custom Title",
api_version="1.0.3",
contact="my.custom.contact@example.com")
contact="my.custom.contact@example.com"
template_path="my/custom/path/to/swagger.yaml")
web.run_app(app, host="127.0.0.1")
......@@ -52,6 +54,35 @@ It produces:
.. image:: _static/swagger_custom_params.jpg
Follows default swagger Jinja2 template which is used in the library:
.. code-block:: yaml
swagger: "2.0"
info:
description: |
{{ description }}
version: "{{ version }}"
title: {{ title }}
{% if contact %}
contact:
name: {{ contact }}
{% endif %}
basePath: {{ base_path }}
schemes:
- http
- https
{% if definitions %}
definitions:
{{ definitions|nesteddict2yaml }}
{% endif %}
{% if security_definitions %}
securityDefinitions:
{{ security_definitions|nesteddict2yaml }}
{% endif %}
paths:
Adding Swagger from external file
---------------------------------
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment