quart-schema icon indicating copy to clipboard operation
quart-schema copied to clipboard

Make schema extensible

Open soon opened this issue 1 year ago • 1 comments

Right now it is impossible to customise schema without subclassing QuartSchema and using it everywhere. E.g. I needed to add operationId to my schema, and the only way is to manually change generated schema in the overridden QuartSchema.openapi method.

My proposal is to provide extension points so users can change schema during generation time. This may look like the following:

def method_schema_generator():
    # some magic decorator provided by library which does the following:
    # 1. Registers user-defined extension in the method
    # 2. Wraps extension in a way to pass schema during generation time, so user can "bind" all other arguments
    # 
    # Then during schema generation step QuartSchema calls registered extension 
    # passing schema to it along with all arguments bound by user 
    ...

# user-defined extension
@method_schema_generator
def operation_id(value, schema):
    schema['operationId'] = value

# usage
@app.post("/")
@validate_request(Todo)
@validate_response(Todo, 201)
@operation_id("create_todo")
async def create_todo(data: Todo) -> tuple[Todo, int]:
    ...

soon avatar May 22 '23 19:05 soon