Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I have a union type in my api and I'm attempting to use a discriminator. It results in reasonable openapi schema

                    "action": {
                        "oneOf": [
                            {
                                "$ref": "#/components/schemas/PassAction"
                            },
                            {
                                "$ref": "#/components/schemas/BlockAction"
                            }
                        ],
                        "title": "Action",
                        "discriminator": {
                            "propertyName": "action_type",
                            "mapping": {
                                "block": "#/components/schemas/BlockAction",
                                "pass": "#/components/schemas/PassAction"
                            }
                        }

The generated client code doesn't seem to respect this. The generated code below:

        def _parse_action(data: object) -> Union["BlockAction", "PassAction"]:
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                action_type_0 = PassAction.from_dict(data)

                return action_type_0
            except:  # noqa: E722
                pass
            if not isinstance(data, dict):
                raise TypeError()
            action_type_1 = BlockAction.from_dict(data)

            return action_type_1

In order to get this somewhat working, I needed to add a unique field to both BlockAction and PassAction so that these auto generated from_dict methods end up throwing. Does this library support proper discriminated unions or is this a gap atm?

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.