To: @core-devs (and specifically @open-api)
A word on sanic-openapi
, with pretty minimal effort, I got the following operational locally.
@dataclass
class Person:
name: str
age: int
@app.post("/person")
@openapi.body(
{"application/json": Person},
description="Body description",
required=True,
validate=True,
)
async def person(request, body):
print(f">>> {body=}")
return json({"Hello": "world"})
Bad data
ValidationError: Invalid request content-type: application/x-www-form-urlencoded
ValidationError: Invalid request body: User. Error: __init__() got an unexpected keyword argument 'foo'
ValidationError: Invalid request body: User. Error: __init__() missing 1 required positional argument: 'age'
Good data
>>> body=Person(name='alice', age=40)
This could be super easily extensible to support Pydantic, Marshmallow, etc. As it stands now, it does support Pydantic when used in dataclass mode.
We would need to decide if it is worth the effort to built in a fallback validator for regular dataclasses. Part of me feels like we should not, and just allow the user to add that if they want it. Doing content-type, and key validation being simple enough, perhaps we leave it like that. Another option might be to look for __validate__
or something similar.
With this in mind, I am wondering about making a divide. The sanic-openapi
library really is almost two. The OAS2 logic is very isolated from OAS3. I think perhaps this is okay.
What I am proposing is to branch the OAS3 logic off and create a new package called sanic-ext
(other names?) that includes this functionality. I would want it to be backwards compatible as much as possible with existing sanic-openapi
installations, but I would really like to just drop the OAS2 support and clean up the repository.
Thoughts?