-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.9.7 Extends the ability to define complex Schema objects.
- Loading branch information
1 parent
687e276
commit d005a18
Showing
8 changed files
with
220 additions
and
167 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 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,95 @@ | ||
import pathlib | ||
from typing import Dict, List, Optional, Union | ||
|
||
import sanic.request | ||
import sanic.response | ||
import sanic.router | ||
from sanic import Sanic | ||
|
||
# isort: off | ||
# These two lines are to ensure that the version of `sanic_openapi3e` your app uses is from this checkout. | ||
import sys | ||
|
||
sys.path.insert(0, str(pathlib.Path(__file__).absolute().parent.parent)) | ||
from sanic_openapi3e import doc, openapi_blueprint, swagger_blueprint | ||
|
||
# isort: on | ||
|
||
|
||
schemas: Optional[Dict[str, Union[doc.Schema, doc.Reference]]] = { | ||
"str.min4": doc.Schema(title="str.min4", _type="string", minimum=4, description="A string of len >= 4",), | ||
"int.min4": doc.Schema(title="int.min4", _type="integer", _format="int32", minimum=4, description="Minimum: 4",), | ||
"cra.properties": doc.Schema( | ||
title="cra.properties", | ||
_type="object", | ||
additional_properties=False, | ||
required=["name", "placeId", "cats", "chains"], | ||
external_docs=doc.ExternalDocumentation("https://tools.ietf.org/html/rfc7946#section-3.2"), | ||
properties={ | ||
"name": doc.Schema.String, | ||
"placeId": doc.Schema.String, | ||
"cats": doc.Schema(_type="array", items=doc.Schema.String, unique_items=True), | ||
"chains": doc.Schema(_type="array", items=doc.Schema.String, nullable=True, unique_items=True), | ||
}, | ||
), | ||
} | ||
components = doc.Components(schemas=schemas) | ||
security: List[doc.SecurityRequirement] = [doc.SecurityRequirement({"bearerAuth": []})] | ||
responses_200only = doc.Responses({"200": doc.Reference("#/components/responses/200")}, no_defaults=True) | ||
|
||
|
||
app = Sanic(name=__file__, strict_slashes=True) | ||
app.blueprint(openapi_blueprint) | ||
app.blueprint(swagger_blueprint) | ||
|
||
app.config.API_TITLE = __file__ | ||
app.config.API_DESCRIPTION = "This has an example with a non-trivial schema -see `cra.properties`" | ||
app.config.OPENAPI_COMPONENTS = components | ||
app.config.OPENAPI_SECURITY = security | ||
|
||
|
||
@app.route("/object/<an_id:int>", methods=frozenset(("GET", "HEAD", "OPTIONS"))) | ||
@doc.parameter( | ||
name="an_id", description="An ID", required=True, _in="path", schema=doc.Schema.Integer, | ||
) | ||
@doc.tag("Tag 1", description="A tag desc") | ||
@doc.responses( | ||
{ | ||
200: {"r": doc.Reference("#/components/responses/200")}, | ||
404: {"d": "Not there", "h": None, "c": None, "l": None}, | ||
401: None, | ||
403: None, | ||
405: None, | ||
410: None, | ||
500: None, | ||
} | ||
) | ||
def get_id(request, an_id: int): | ||
d = locals() | ||
del d["request"] # not JSON serializable | ||
return sanic.response.json(d) | ||
|
||
|
||
@app.get("/object2/<an_id:int>") | ||
@doc.parameter( | ||
name="an_id", description="An ID", required=True, _in="path", schema=doc.Schema.Integer, | ||
) | ||
@doc.tag("Tag 1", description="A tag desc") | ||
@doc.responses(responses_200only) | ||
def get_id2(request, an_id: int): | ||
d = locals() | ||
del d["request"] # not JSON serializable | ||
return sanic.response.json(d) | ||
|
||
|
||
example_port = 8002 | ||
|
||
|
||
@app.listener("after_server_start") | ||
async def notify_server_started(_, __): | ||
print("\n\n************* sanic-openapi3e ********************************") | ||
print(f"* See your openapi swagger on http://127.0.0.1:{example_port}/swagger/ *") | ||
print("************* sanic-openapi3e ********************************\n\n") | ||
|
||
|
||
app.go_fast(port=example_port) |
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 |
---|---|---|
|
@@ -17,5 +17,5 @@ dependencies: | |
- pyyaml >= 5.3.1 | ||
# - requests-async >=0.5.0 | ||
- pip: | ||
- sanic >=19.6.0 | ||
- sanic >= 20.12.0 | ||
|
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
Oops, something went wrong.