Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StructuralSchemaRewriter fails for #[serde(untagged)] enums of unit enums #1622

Open
nightkr opened this issue Oct 31, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@nightkr
Copy link
Member

nightkr commented Oct 31, 2024

Current and expected behavior

Sometimes you want to write an enum that adds more specific options to an otherwise generic enum. For example:

#[derive(JsonSchema)]
enum BreakfastItem {
    Spam,
    Eggs,
}

#[derive(JsonSchema)]
enum Breakfast {
    FullBreakfast,
    #[serde(untagged)]
    Item(BreakfastItem),
}

However, schemars doesn't support variant-level #[serde(untagged)] (GREsau/schemars#222). One alternative is to break out the new options to a separate enum, like so:

#[derive(JsonSchema)]
enum BreakfastItem {
    Spam,
    Eggs,
}

// new, contains the variants that were otherwise exclusive to Breakfast
#[derive(JsonSchema)]
enum BreakfastMeal {
    FullBreakfast,
}

#[derive(JsonSchema)]
// moved from the Item variant
#[serde(untagged)]
enum Breakfast {
    Meal(BreakfastMeal),
    Item(BreakfastItem),
}

However, this generates the following schema:

anyOf:
  - type: string
    enum:
      - FullBreakfast
  - type: string
    enum:
      - Spam
      - Eggs

which isn't structural.

Possible solution

The structuralizer should recognize that we can safely merge these anyOf variants, into:

type: string
enum:
  - FullBreakfast
  - Spam
  - Eggs

Additional context

No response

Environment

Client Version: v1.30.2
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.31.0+k3s1

Configuration and features

schemars v0.8.21
├── kube-core v0.96.0 (*)

Affected crates

kube-core

Would you like to work on fixing this bug?

None

@nightkr nightkr added the bug Something isn't working label Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant