-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/using_both_produces_and_response
- Loading branch information
Showing
12 changed files
with
232 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Docstring Parsing | ||
|
||
sanic-openAPI will try to parse your function for documentation to add to the swagger interface, so for example: | ||
|
||
```python | ||
app = Sanic() | ||
app.blueprint(swagger_blueprint) | ||
|
||
|
||
@app.get("/test") | ||
async def test(request): | ||
''' | ||
This route is a test route | ||
In can do lots of cool things | ||
''' | ||
return json({"Hello": "World"}) | ||
``` | ||
|
||
Would add that docstring to the openAPI route 'summary' and 'description' fields. | ||
|
||
For advanced users, you can also edit the yaml yourself, by adding the line "openapi:" followed by a valid yaml string. | ||
|
||
Note: the line "openapi:" should contain no whitespace before or after it. | ||
|
||
Note: any decorators you use on the function must utilise functools.wraps or similar in order to preserve the docstring if you would like to utilising the docstring parsing capability. | ||
|
||
```python | ||
app = Sanic() | ||
app.blueprint(swagger_blueprint) | ||
|
||
|
||
@app.get("/test") | ||
async def test(request): | ||
''' | ||
This route is a test route | ||
In can do lots of cool things | ||
openapi: | ||
--- | ||
responses: | ||
'200': | ||
description: OK | ||
''' | ||
return json({"Hello": "World"}) | ||
``` | ||
|
||
If the yaml fails to parse for any reason, a warning will be printed, and the yaml will be ignored. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import inspect | ||
import warnings | ||
|
||
import yaml | ||
|
||
|
||
class OpenAPIDocstringParser: | ||
def __init__(self, docstring: str): | ||
""" | ||
Args: | ||
docstring (str): docstring of function to be parsed | ||
""" | ||
if docstring is None: | ||
docstring = "" | ||
self.docstring = inspect.cleandoc(docstring) | ||
|
||
def to_openAPI_2(self) -> dict: | ||
""" | ||
Returns: | ||
json style dict: dict to be read for the path by swagger 2.0 UI | ||
""" | ||
raise NotImplementedError() | ||
|
||
def to_openAPI_3(self) -> dict: | ||
""" | ||
Returns: | ||
json style dict: dict to be read for the path by swagger 3.0.0 UI | ||
""" | ||
raise NotImplementedError() | ||
|
||
|
||
class YamlStyleParametersParser(OpenAPIDocstringParser): | ||
def _parse_no_yaml(self, doc: str) -> dict: | ||
""" | ||
Args: | ||
doc (str): section of doc before yaml, or full section of doc | ||
Returns: | ||
json style dict: dict to be read for the path by swagger UI | ||
""" | ||
# clean again in case further indentation can be removed, | ||
# usually this do nothing... | ||
doc = inspect.cleandoc(doc) | ||
|
||
if len(doc) == 0: | ||
return {} | ||
|
||
lines = doc.split("\n") | ||
|
||
if len(lines) == 1: | ||
return {"summary": lines[0]} | ||
else: | ||
summary = lines.pop(0) | ||
|
||
# remove empty lines at the beginning of the description | ||
while len(lines) and lines[0].strip() == "": | ||
lines.pop(0) | ||
|
||
if len(lines) == 0: | ||
return {"summary": summary} | ||
else: | ||
# use html tag to preserve linebreaks | ||
return {"summary": summary, "description": "<br>".join(lines)} | ||
|
||
def _parse_yaml(self, doc: str) -> dict: | ||
""" | ||
Args: | ||
doc (str): section of doc detected as openapi yaml | ||
Returns: | ||
json style dict: dict to be read for the path by swagger UI | ||
Warns: | ||
UserWarning if the yaml couldn't be parsed | ||
""" | ||
try: | ||
return yaml.safe_load(doc) | ||
except Exception as e: | ||
warnings.warn("error parsing openAPI yaml, ignoring it. ({})".format(e)) | ||
return {} | ||
|
||
def _parse_all(self) -> dict: | ||
if "openapi:\n" not in self.docstring: | ||
return self._parse_no_yaml(self.docstring) | ||
|
||
predoc, yamldoc = self.docstring.split("openapi:\n", 1) | ||
|
||
conf = self._parse_no_yaml(predoc) | ||
conf.update(self._parse_yaml(yamldoc)) | ||
return conf | ||
|
||
def to_openAPI_2(self) -> dict: | ||
return self._parse_all() | ||
|
||
def to_openAPI_3(self) -> dict: | ||
return self._parse_all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from sanic_openapi import autodoc | ||
|
||
|
||
tests = [] | ||
|
||
_ = '' | ||
|
||
tests.append({'doc': _, 'expects': {}}) | ||
|
||
_ = 'one line docstring' | ||
|
||
tests.append({'doc': _, 'expects': {"summary": "one line docstring"}}) | ||
|
||
_ = ''' | ||
first line | ||
more lines | ||
''' | ||
|
||
tests.append({'doc': _, 'expects': { | ||
"summary": "first line", | ||
"description": "more lines"}}) | ||
|
||
|
||
_ = ''' | ||
first line | ||
more lines | ||
openapi: | ||
--- | ||
responses: | ||
'200': | ||
description: OK | ||
''' | ||
|
||
tests.append({'doc': _, 'expects': { | ||
"summary": "first line", | ||
"description": "more lines", | ||
"responses": {"200": {"description": "OK"}}}}) | ||
|
||
|
||
def test_autodoc(): | ||
for t in tests: | ||
parser = autodoc.YamlStyleParametersParser(t["doc"]) | ||
assert parser.to_openAPI_2() == t["expects"] | ||
assert parser.to_openAPI_3() == t["expects"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters