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

support response_model_exclude_unset #78

Closed Danferno closed 3 months ago

Danferno commented 4 months ago

Apologies if I missed it, but is there a way to exclude unset parameters from the generated response? Similar to FASTAPI's response_model_exclude_unset parameter? As far as I can tell, it sets the exclude_unset parameter in pydantic's model_dump.

For example, I have an API endpoint that returns content and an optional warning:

@dataclass
class ListResponse:
    content: list[str]
    warning: Optional[str]

I am currently getting errors when the warning is not set, as it is looking for the warning field. Especially in responses I do not find this intuitive: I specified it's an optional field, so why complain that it's missing? I could solve this by setting a default value, e.g.

@dataclass
class ListResponse:
    content: list[str]
    warning: Optional[str]=None

But then all responses will include an empty warning field, whereas I'd prefer the warning field to be absent entirely.

pgjones commented 3 months ago

Yep, supported via abf72c15523ca1dfb2e926a3f94c74f76eeaf1b2.

See also https://github.com/jcrist/msgspec/issues/633 for msgspec equivalent support.

Danferno commented 3 months ago

Is there a way to already use the version where this is patched? Unless I'm mistaken the pypi version doesn't include it yet