Skip to content

Commit 7f5e1a9

Browse files
1 parent 9d8a8e0 commit 7f5e1a9

6 files changed

Lines changed: 46 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [1.0.1] - 2022-??-??
8+
## [1.0.1] - 2022-05-05
99
- Adds a new output style, to provide an overview of the API endpoints with
1010
PlantUML
11+
- Fixes two bugs caused by improper handling of OpenAPI Documentation without
12+
`components` https://github.com/Neoteroi/mkdocs-plugins/issues/9
1113

1214
## [1.0.0] - 2022-04-20 :sparkles:
1315
- Adds features and a CLI to generate artifacts from OpenAPI Documentation

openapidocs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "1.0.0"
1+
VERSION = "1.0.1"

openapidocs/mk/v3/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,21 @@ def __init__(
7474
writer: Optional[DocumentsWriter] = None,
7575
style: Union[int, str] = 1,
7676
) -> None:
77-
self.doc = copy.deepcopy(doc)
77+
self.doc = self.normalize_data(copy.deepcopy(doc))
7878
self.texts = texts or EnglishTexts()
7979
self._writer = writer or Jinja2DocumentsWriter(
8080
__name__, views_style=style_from_value(style)
8181
)
8282

83+
def normalize_data(self, data):
84+
"""
85+
Applies corrections to the OpenAPI specification, to simplify its handling.
86+
"""
87+
if "components" not in data:
88+
data["components"] = {}
89+
90+
return data
91+
8392
def get_operations(self):
8493
"""
8594
Gets a dictionary of operations grouped by tag.

openapidocs/mk/v3/views_markdown/layout.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
{%- include "partial/path-items.html" %}
1515

1616
---
17+
{%- if components %}
1718
{%- if components.schemas %}
1819
{% include "partial/components-schemas.html" %}
1920
{% endif -%}
@@ -29,3 +30,4 @@
2930
{%- if components.securitySchemes %}
3031
{% include "partial/components-security-schemes.html" %}
3132
{% endif -%}
33+
{% endif -%}

openapidocs/mk/v3/views_mkdocs/layout.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
{% include "partial/path-items.html" %}
1515

1616
---
17+
{%- if components %}
1718
{%- if components.schemas %}
1819
{% include "partial/components-schemas.html" %}
1920
{% endif -%}
@@ -29,3 +30,4 @@
2930
{%- if components.securitySchemes %}
3031
{% include "partial/components-security-schemes.html" %}
3132
{% endif -%}
33+
{% endif -%}

tests/test_mk_v3.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,31 @@ def test_iter_bindings_arrays():
222222
def test_style_from_value_raises_value_error():
223223
with pytest.raises(ValueError):
224224
style_from_value("WRONG")
225+
226+
227+
def test_v3_markdown_gen_handles_missing_components():
228+
data = {
229+
"openapi": "3.0.0",
230+
"info": {
231+
"version": "1.0.0",
232+
"title": "Example",
233+
},
234+
"paths": {
235+
"/foo": {
236+
"get": {
237+
"responses": {
238+
"200": {
239+
"description": "Foo",
240+
"content": {"text/plain": {"schema": {"type": "string"}}},
241+
},
242+
},
243+
},
244+
},
245+
},
246+
}
247+
handler = OpenAPIV3DocumentationHandler(data)
248+
249+
html = handler.write(data)
250+
with open("___a.html", encoding="utf8", mode="wt") as f:
251+
f.write(html)
252+
assert html is not None

0 commit comments

Comments
 (0)