msgspec
msgspec copied to clipboard
Discriminating without Tags
Description
Continuing from https://github.com/jcrist/msgspec/issues/474#issue-1792097063 OpenAPI continues to have issues with parsing.
When attempting to parse fields such as
from msgspec import Meta, Struct, field
from typing import Annotated, Dict, List, Literal
class Parameter(Struct):
name: str
in_: Literal['query', 'header', 'path', 'cookie'] = field(name='in')
required: bool = False
schema: 'Schema' | None = None
content: Annotated[Dict[str, 'MediaType'], Meta(max_length=1)] | None = None
# ...
class Reference(Struct):
_ref: str
class Path(Struct):
# ...
parameters: List[Parameter | Reference] | None
we note that it's currently impossible to discriminate between Parameters and References, because we can't add a tag field. In this case however, there is an obvious difference in the set of keys, which we can use to differentiate between the two. I'm not sure how to generally solve the problem for sets of keys that overlap heavily (e.g. TypeScript's discriminated union)
I get around this by simply using the jsonref library to parse and replace the refs with the actual values, and then dumping the entire thing back into a string This is really unfriendly for performance though.
Thanks for opening this - this is something that I think we can and should support, but it will require some internal refactoring to make possible. Unfortunately I don't have a good workaround to suggest in the meantime.
Msgspec is an amazing library and I'd love to migrate, but this is a major blocker for me.
All the best.