msgspec icon indicating copy to clipboard operation
msgspec copied to clipboard

Discriminating without Tags

Open illeatmyhat opened this issue 2 years ago • 2 comments
trafficstars

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.

illeatmyhat avatar Jul 31 '23 16:07 illeatmyhat

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.

jcrist avatar Aug 03 '23 16:08 jcrist

Msgspec is an amazing library and I'd love to migrate, but this is a major blocker for me.

All the best.

rafalkrupinski avatar Jul 14 '24 09:07 rafalkrupinski