Skip to content

Conversation

sbernauer
Copy link
Member

@sbernauer sbernauer commented Oct 8, 2025

Fixes kube Schema conversion since schemars Schema's have changed.

  • Add tests for a variety of enum use-cases:
    • Tagged vs Untagged
    • Unit vs Tuple vs Structural variants
    • With and without doc-comments (descriptions)
  • Rewrite the hoisting logic
    • This is annotated with dev-comments to help understand intend and to ease future schemars changes.

This also fixes an issue where Untagged enum variant doc-comments were being applied to field descriptions.

@sbernauer sbernauer self-assigned this Oct 8, 2025
@sbernauer sbernauer moved this to Development: In Progress in Stackable Engineering Oct 8, 2025
@sbernauer sbernauer closed this Oct 9, 2025
@sbernauer sbernauer reopened this Oct 9, 2025
Note: As described on the function, it is overly documented to express intent where it can be a little unclear. Ideally where would be some clearer official Kubernetes documentation on the differences between regular OpenAPI 3 schemas, and what Kubernetes accepts.
… there are no oneOfs at the top level of the schema
Note: At this point, there is some strange interaction with the new hoisting functions and the existing code whereby "properties" disappear.
…l as enum

All tests in this PR are passing

Note: The function should be renamed, but it needs some work to not only do optional things.
@NickLarsenNZ

This comment was marked as outdated.

note: test expectaions changed as part of the implementation: Untagged variant doc-comments will not be preserved. Previously they were erroneously added as field descriptions.
Also update function docs, and expected panic messages
Comment on lines +271 to +272
// TODO (@NickLarsenNZ): At this point, we are seeing duplicate `title` when printing schema as json.
// This is because `title` is specified under both `extensions` and `other`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I should remove it, but worth sharing the knowledge before I do:

Suggested change
// TODO (@NickLarsenNZ): At this point, we are seeing duplicate `title` when printing schema as json.
// This is because `title` is specified under both `extensions` and `other`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we actually should leave it here (reworded/adjusted) to explicitly share the knowledge.

// This will panic if duplicate properties are encountered that do not have the same shape.
// That can happen when the untagged enum variants each refer to structs which contain the same field name but with different types or doc-comments.
// The developer needs to make them the same.
// TODO (@NickLarsenNZ): Add a case for a structural variant, and a tuple variant containing a structure where the same field name is used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add a test for:

// panics...wrong. two can't be u32 AND NormalEnum
/// An untagged enum with a nested enum inside
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[serde(untagged)]
enum InvalidUntaggedEnumX {
    /// Used in case the `one` field of tpye [`u32`] is present
    A { one: String, two: u32 },
    /// Used in case the `two` field of type [`NormalEnum`] is present
    B { two: NormalEnum },
    /// Used in case no fields are present
    C {},
}

// This is fine, except that it will fail until he descriptions match (or we fix the invalid descriptions)
/// An untagged enum with a nested enum inside
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[serde(untagged)]
enum UntaggedEnum3 {
    /// Used in case the `one` field of tpye [`u32`] is present
    A { one: String, two: NormalEnum },
    /// Used in case the `two` field of type [`NormalEnum`] is present
    B { two: NormalEnum },
    /// Used in case no fields are present
    C {},
}

Comment on lines +161 to +163
// Set the schema to nullable (as we know we matched the null variant earlier)
// TODO (@NickLarsenNZ): Do we need to insert `nullable` into `other` too?
kube_schema.extensions.insert("nullable".to_owned(), true.into());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyone know what to do here?
I don't know if it's good enough being in one place, or if it should be in both for compatibility with something.

schemars sends fields that are the same between extensions and other.

@NickLarsenNZ NickLarsenNZ moved this from Development: In Progress to Development: Waiting for Review in Stackable Engineering Oct 22, 2025
@NickLarsenNZ NickLarsenNZ marked this pull request as ready for review October 22, 2025 15:17
@NickLarsenNZ NickLarsenNZ changed the title Add some tests for CRD generation for enums fix: kube-core::Schema hoisting logic Oct 23, 2025
@NickLarsenNZ NickLarsenNZ self-assigned this Oct 23, 2025
Comment on lines +259 to +263
#[cfg(test)]
fn schemars_schema_to_kube_schema(incoming: schemars::Schema) -> Result<Schema, serde_json::Error> {
serde_json::from_value(incoming.to_value())
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unused. @sbernauer did you want to do something with is?

Else I will remove it:

Suggested change
#[cfg(test)]
fn schemars_schema_to_kube_schema(incoming: schemars::Schema) -> Result<Schema, serde_json::Error> {
serde_json::from_value(incoming.to_value())
}

@sbernauer sbernauer moved this from Development: Waiting for Review to Development: In Review in Stackable Engineering Oct 23, 2025

// Enum definitions

/// A very simple enum with empty variants
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to change this description. It needs changes in the expected test schemas too

Suggested change
/// A very simple enum with empty variants
/// A very simple enum with unit variants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Development: In Review

Development

Successfully merging this pull request may close these issues.

3 participants