kube-rs / kube

Rust Kubernetes client and controller runtime
https://kube.rs
Apache License 2.0
3.05k stars 318 forks source link

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

Open nightkr opened 3 weeks ago

nightkr commented 3 weeks ago

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)] (https://github.com/GREsau/schemars/issues/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