Skip to content

Commit 3ec2a76

Browse files
committed
fix(dashboard): keep int-or-string quantities as strings for fractional values
Review caught that `["number", "string"]` made the form emit a JSON float for a typed 0.5, and the chart values.schema.json (anyOf integer|string, enforced by helm-controller) rejects a float, failing the HelmRelease — worse than the original bug. Switch to `["string", "integer"]`: StringField keeps typed values as strings, so "0.5" deploys, while the integer member still accepts a stored or default whole number like 1. Guard it with a form-level test that a typed 0.5 stays the string "0.5". Assisted-by: LLM Signed-off-by: IvanHunters <[email protected]>
1 parent 4504f2f commit 3ec2a76

3 files changed

Lines changed: 61 additions & 25 deletions

File tree

‎packages/system/dashboard/images/console/apps/console/src/components/SchemaForm.test.tsx‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRef } from "react"
22
import { describe, it, expect } from "vitest"
3-
import { render, screen, act } from "@testing-library/react"
3+
import { render, screen, act, fireEvent } from "@testing-library/react"
44
import { SchemaForm, type SchemaFormHandle } from "./SchemaForm.tsx"
55
import { IMMUTABLE_HELP_TEXT } from "../lib/immutable-paths.ts"
66

@@ -38,6 +38,38 @@ const tenantQuotaSchema = JSON.stringify({
3838
},
3939
})
4040

41+
describe("SchemaForm int-or-string quantity", () => {
42+
const quantitySchema = JSON.stringify({
43+
type: "object",
44+
properties: {
45+
cpu: {
46+
"x-kubernetes-int-or-string": true,
47+
pattern: "^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))([KMGTPE]i|[numkMGTPE])?$",
48+
anyOf: [{ type: "integer" }, { type: "string" }],
49+
},
50+
},
51+
})
52+
53+
it("keeps a typed fractional quantity as a string, not a float", () => {
54+
// A float like 0.5 is rejected by the chart's values.schema.json and would
55+
// fail the HelmRelease, so the field must stay a text input that emits
56+
// "0.5" rather than a number widget that emits the float 0.5.
57+
let last: { cpu?: unknown } = {}
58+
const { container } = render(
59+
<SchemaForm
60+
openAPISchema={quantitySchema}
61+
formData={{ cpu: 1 }}
62+
onChange={(d) => (last = d as { cpu?: unknown })}
63+
/>,
64+
)
65+
const input = container.querySelector('input[type="text"]') as HTMLInputElement
66+
expect(input).not.toBeNull()
67+
fireEvent.change(input, { target: { value: "0.5" } })
68+
expect(last.cpu).toBe("0.5")
69+
expect(typeof last.cpu).toBe("string")
70+
})
71+
})
72+
4173
describe("SchemaForm resourceQuotas editor", () => {
4274
it("uses the structured quota editor for int-or-string resourceQuotas", () => {
4375
render(<SchemaForm openAPISchema={tenantQuotaSchema} formData={{}} onChange={noop} />)

‎packages/system/dashboard/images/console/apps/console/src/lib/keys-order.test.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,25 @@ describe("sanitizeSchema", () => {
5555
expect(sanitizeSchema(input)).toEqual(input)
5656
})
5757

58-
it("coerces x-kubernetes-int-or-string to [number, string] and drops anyOf", () => {
58+
it("coerces x-kubernetes-int-or-string to [string, integer] and drops anyOf", () => {
5959
const out = sanitizeSchema({
6060
"x-kubernetes-int-or-string": true,
6161
pattern: QUANTITY_PATTERN,
6262
anyOf: [{ type: "integer" }, { type: "string" }],
6363
})
64-
expect(out.type).toEqual(["number", "string"])
64+
// string first so RJSF picks StringField and keeps typed values as strings;
65+
// integer still accepted for a stored/default whole number.
66+
expect(out.type).toEqual(["string", "integer"])
6567
expect(out.anyOf).toBeUndefined()
6668
// The pattern survives so junk strings are still rejected.
6769
expect(out.pattern).toBe(QUANTITY_PATTERN)
6870
})
6971

70-
it("int-or-string field validates whole and fractional numbers and quantity strings, rejects junk", () => {
71-
// Oracle is ajv via @rjsf/validator-ajv8, not hand-rolled assertions: the
72-
// sanitised schema is what the form actually validates against at submit.
73-
// `number` (not `integer`) is required because the text widget coerces a
74-
// typed `0.5` to the JS number 0.5, which an integer type would reject.
72+
it("int-or-string field accepts an integer and quantity strings, rejects a float and junk", () => {
73+
// Oracle is ajv via @rjsf/validator-ajv8, matching the chart's own
74+
// values.schema.json (anyOf integer|string): a JSON float like 0.5 is
75+
// invalid there and fails the HelmRelease, so the form must not accept it;
76+
// the fractional value belongs as the string "0.5".
7577
const schema = sanitizeSchema({
7678
description: "CPU limit.",
7779
default: 1,
@@ -81,13 +83,14 @@ describe("sanitizeSchema", () => {
8183
})
8284
const errors = (v: unknown) =>
8385
validator.validateFormData(v, schema as never).errors.map((e) => e.message)
84-
// Bare integer (the field's own default) must be accepted — the original bug.
86+
// Stored/default whole number must be accepted — the original bug.
8587
expect(errors(1)).toEqual([])
86-
// Fractional cores, coerced to a JS number by the widget, must be accepted.
87-
expect(errors(0.5)).toEqual([])
88-
// Suffixed quantity strings stay valid.
88+
// Fractional and suffixed quantities are valid as strings.
89+
expect(errors("0.5")).toEqual([])
8990
expect(errors("500m")).toEqual([])
9091
expect(errors("1")).toEqual([])
92+
// A JSON float must be rejected (the chart schema rejects it too).
93+
expect(errors(0.5).length).toBeGreaterThan(0)
9194
// Nonsense is still rejected by the pattern.
9295
expect(errors("bad!!").length).toBeGreaterThan(0)
9396
})

‎packages/system/dashboard/images/console/apps/console/src/lib/keys-order.ts‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ export function keysOrderToUiSchema(
5959
*
6060
* - `x-kubernetes-int-or-string` together with `anyOf: [{integer},{string}]`
6161
* — the field accepts either. We drop the anyOf (RJSF would render it as a
62-
* variant selector) and set `type: ["number", "string"]`, which keeps a
63-
* single text input while letting RJSF coerce a typed number (`cpu: 1` or
64-
* `cpu: 0.5`) to a JS number and leave a suffixed value (`cpu: 500m`) a
65-
* string — the same way the YAML editor and kubectl parse them. `number`,
66-
* not `integer`: the widget coerces a fractional `0.5` to the number 0.5,
67-
* which an integer type would reject.
62+
* variant selector) and set `type: ["string", "integer"]`. String first so
63+
* RJSF picks StringField: typed values stay strings, so a fractional `0.5`
64+
* is emitted as "0.5" rather than the JSON float 0.5 — which the chart's own
65+
* values.schema.json (anyOf integer|string, enforced by helm-controller)
66+
* would reject, failing the HelmRelease. The integer member still accepts a
67+
* stored or default whole number such as `cpu: 1`, which was the field's
68+
* original "must be string" bug.
6869
* - `x-kubernetes-preserve-unknown-fields: true` on an object without
6970
* properties — arbitrary nested values allowed. Left as a "raw" object
7071
* (handled via additionalProperties: true).
@@ -90,13 +91,13 @@ export function sanitizeSchema(schema: any): any {
9091
}
9192

9293
if (schema["x-kubernetes-int-or-string"]) {
93-
// Two-type array, not a bare "string": collapsing to "string" rejected the
94-
// numeric form of a quantity — including the schema's own numeric default
95-
// (`default: 1`) — so the form refused values the YAML editor and the API
96-
// both accept. ["number", "string"] validates whole and fractional numbers
97-
// and quantity strings, and still renders one text input (see the header
98-
// comment).
99-
out.type = ["number", "string"]
94+
// ["string", "integer"], not a bare "string": collapsing to "string"
95+
// rejected the numeric form of a quantity — including the schema's own
96+
// numeric default (`default: 1`) — with "must be string". String first
97+
// keeps StringField so typed values stay strings (a fractional "0.5" that
98+
// the chart schema accepts, not the float 0.5 it rejects); integer still
99+
// accepts a stored/default whole number. See the header comment.
100+
out.type = ["string", "integer"]
100101
}
101102
if (schema["x-kubernetes-preserve-unknown-fields"] && !schema.properties) {
102103
out.type = "object"

0 commit comments

Comments
 (0)