Package: chocs-middleware-openapi
Chocs-OpenApiOpenApi middleware for chocs library.Newest OpenAPI Specification (v.3.x) can be easily integrated into Chocs through application's middleware.
Validation is performed viaJsonSchema Draft-7.0 specificationand all commonly
used features are supported.FeaturesOpen api integration can be used to:validate request's bodyvalidate request's path parametersvalidate request's headersvalidate request's query parametersvalidate request's cookiesgenerate dtos from openapi fileInstallationWith pip,pipinstallchocs-middleware.openapior through poetrypoetryaddchocs-middleware.openapiUsageUsing your OpenAPI fileChocs can read json and yaml files, this example will cover yaml usage although the only difference is the file extension.importchocsfromchocs_middleware.openapiimportOpenApiMiddlewarefromosimportpath# absolute path to file containing open api documentation; yaml and json files are supportedopenapi_filename=path.join(path.dirname(__file__),"/openapi.yml")# instantiating application and passing open api middlewareapp=chocs.Application(OpenApiMiddleware(openapi_filename,validate_body=True,validate_query=True))# the registered route must correspond to open api route within `path` section.# if request body is invalid the registered controller will not be invoked@app.post("/pets")defcreate_pet(request:chocs.HttpRequest)->chocs.HttpResponse:...returnchocs.HttpResponse(status=200)Complete integration example can befound hereKeep in mind registered route has to match 1:1 the specified route insidepathssection inside your OpenApi documentationValidating request bodyBelow is very simple schema to validate request body of aPOST /petrequest. Request body is required, should be valid json request and contain the following properties:name (string)tags (array of string)id (optional string)openapi.ymlopenapi:"3.0.0"info:version:"1.0.0"title:"PetStore"paths:/pets:post:description:Creates a new PetrequestBody:description:Petrequired:truecontent:application/json:schema:$ref:"#/components/schemas/Pet"responses:200:description:"Success"components:schemas:Pet:type:objectrequired:-name-tagproperties:id:type:integername:type:stringtag:type:arrayitems:type:stringapp.pyimportchocsfromchocs_middleware.openapiimportOpenApiMiddlewarefromosimportpathopenapi_filename=path.join(path.dirname(__file__),"/openapi.yml")app=chocs.Application(OpenApiMiddleware(openapi_filename,validate_body=True))@app.post("/pets")defcreate_pet(request:chocs.HttpRequest)->chocs.HttpResponse:pet=request.parsed_body# here we will get valid petreturnchocs.HttpResponse(status=200)chocs.serve(app)create_petcontroller will be only invoked if request contains valid body. Pet's data can be accessed throughrequest.parsed_bodywhich is a dict-like object.Json schema supportChocs uses JSON Schema to validate your open api definitions with full draft-7 support and almost complete 2019-09 standard support.
This means you can use almost every feature described on theunderstanding json schemawebpage.
The webpage is a great resource full of examples and detailed descriptions around JSON Schema.There are some caveats aroundallOfvalidator:all object schemas insideallOfdefinition are automatically composed into a single object definitionwhen combining string validators make sure format validator is the last validator in the pipeline otherwise validation might fail due to string casting