基于openapi 3的falcon设计优先方法
falcon-oas的Python项目详细描述
先决条件
- 已验证的OpenAPI 3文档
- Falcon OAS不在运行时验证OpenAPI 3文档本身。应该提前验证。
功能
- 请求验证和解组
- 访问控制
- falcon中路径项对象和资源类的关联
示例
classPetItem:defon_get(self,req,resp,pet_id):pet=get_pet_by_id(pet_id)resp.media=pet.to_dict()defon_delete(self,req,resp,pet_id):pet=delete_pet_by_id(pet_id)resp.status=falcon.HTTP_NO_CONTENTwithopen('/path/to/openapi.json')asf:spec_dict=json.load(f)api=falcon_oas.OAS(spec_dict).create_api()
以下是yaml中openapi 3文档的一部分:
paths:/api/v1/pets/{pet_id}:x-falcon-oas-implementation:path.to.PetItemget:responses:'200':description:A pet.delete:responses:'204':description:Successful deletion.security:-api_key:[]parameters:-name:pet_idin:pathrequired:trueschema:type:integercomponents:securitySchemes:api_key:x-falcon-oas-implementation:path.to.api_key_validatortype:apiKeyname:X-API-Keyin:header
pet_idpath参数在没有Field Converters的情况下被解组到int,因为它被定义为integer类型。
DELETE /api/v1/pets/{pet_id}请求受api_key安全方案保护。只有当相应的响应程序授予请求时,才会对其进行处理。否则自动发生403禁止错误。
x-falcon-oas-implementation将falcon中的path item对象和rest资源类关联起来,以便falcon oas自动用路径和资源实例调用falcon.API.add_route。
同时x-falcon-oas-implementation关联安全方案对象和访问控制功能,以便falcon oas在每个请求中自动处理安全需求对象。有关详细信息,请参见falcon_oas.extensions。
req.context['oas']
- req.context['oas'].user
- 授权用户。
- req.context['oas'].parameters
- 在dict中取消编组的请求参数。
- req.context['oas'].request_body
- 未编组的请求正文。
问题
媒体类型:application/problem+json仅限
解组错误
http状态码:400
- "type":"https://pypi.org/project/falcon-oas/0.3.0/#unmarshal-error"
- "title":"Unmarshal Error"
- "status":400
- "parameters":(可选)参数错误对象数组
- "request_body":(可选)请求正文错误对象数组
parameter error对象和request body error对象具有来自jsonschema.ValidationError的以下成员:
- "path":实例中有问题的元素的路径
- "validator":失败验证器的名称
- "message":解释错误的可读消息
示例:
{"type":"https://pypi.org/project/falcon-oas/0.3.0/#unmarshal-error","title":"Unmarshal Error","status":400,"parameters":[{"path":["path","pet_id"],"validator":"type","message":"'me' is not of type 'integer'"}],"request_body":[{"path":["name"],"validator":"type","message":"42 is not of type 'string'"}]}