oxidecomputer / oxidecomputer/typify

Discriminators and internally tagged enums

Open
#498 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
898
Forks
114
Avg merge
4h 18m
Merged PRs (30d)
14

Description

Hello! Thanks so much for typify.

I have a relatively simple issue to explain, with some code samples. I have some discriminated unions which simply need #[serde(tag = "my_type")] instead of #[serde(untagged)] but they keep coming up untagged. This leads to one item getting parsed as the other and vice versa, which isn't what I want. I can edit the generated code, but I'd really love to get typify to honor the discriminator.propertyName that pydantic produces in its jsonschema as the preferred tag.

Of course propertyName is some offshoot of jsonschema proper (OAI I suppose), but it solves a problem and I'd love to either support it directly or have a way to override the parameters of the serde field attribute programmatically?

In short:

      {
        "discriminator": {
          "mapping": {
            "Baz": "#/$defs/Baz",
            "FooBar": "#/$defs/FooBar"
          },
          "propertyName": "my_type"
        },
        "oneOf": [
          {
            "$ref": "#/$defs/FooBar"
          },
          {
            "$ref": "#/$defs/Baz"
          }
        ]
      }

produces an attribute of:
#[serde(untagged)]
when in my opinion (and experience because changing this works) it should produce:
#[serde(tag="my_type") based off of propertyName.

I put the runnable example code in a repo: https://github.com/rseymour/typify-tag-example/tree/main

I think I could fix this myself, but I haven't been able to correlate the code to the strings it outputs for annotations.

[addendum]
If there's a way to fix the jsonschema itself to produce internally tagged enums that's also an option.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the typify code that handles JSON Schema discriminators and emits serde enum attributes, using the linked typify-tag-example repository as the reproduction case. Trace how the shown discriminator with propertyName is currently turned into #[serde(untagged)]. Done means the example generates an internally tagged enum with the requested property, or a clearly defined programmatic override is supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.