pgjones / quart-schema

Quart-Schema is a Quart extension that provides schema validation and auto-generated API documentation.
MIT License
76 stars 24 forks source link

Make schema extensible #40

Open soon opened 1 year ago

soon commented 1 year ago

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]:
    ...
mst-ableton commented 1 year ago

I think a more extensible schema generation would be nice, but I wanted to mention that I've just submitted a PR to add operationID support here: https://github.com/pgjones/quart-schema/pull/41.