Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(structured): reject enum choice label collisions

dakjdakd3560057f8737
diff --git a/docs/structured.md b/docs/structured.md
index a2bd0c10..97a3d3c2 100644
--- a/docs/structured.md
+++ b/docs/structured.md
@@ -63,6 +63,7 @@ A schema that cannot be answered from a fixed option set raises `laya.structured
| `array` | `properties.name: arrays are not supported; ask one field per element` |
| nested `object` | `properties.name: nested objects are not supported; flatten the schema` |
| `$ref` / recursion | `properties.name: $ref/recursion is not supported; flatten the schema` |
+| enum values with the same choice label, such as `1` and `"1"` | `properties.name: enum values produce duplicate choice labels` |
| unbounded number | `properties.name: a numeric field needs integer 'minimum' and 'maximum' to become a score` |
| more than `MAX_PROPERTIES` / `MAX_OPTIONS` / `MAX_SCORE_LEVELS` | the limit is named in the message |
diff --git a/laya-ts/src/structured.ts b/laya-ts/src/structured.ts
index 7d1f4214..86d6eaef 100644
--- a/laya-ts/src/structured.ts
+++ b/laya-ts/src/structured.ts
@@ -114,6 +114,9 @@ function enumField(path: string, name: string, values: unknown[], description: u
if (values.length === 0) throw new SchemaError(`${path}: 'enum' must not be empty`);
if (values.every((v) => typeof v === "boolean")) return noulField(name, description);
const options: [string, unknown][] = values.map((v) => [v == null ? "null" : String(v), v]);
+ if (new Set(options.map(([label]) => label)).size !== options.length) {
+ throw new SchemaError(`${path}: enum values produce duplicate choice labels`);
+ }
return {
name,
kind: "choice",
diff --git a/laya-ts/tests/structured.test.ts b/laya-ts/tests/structured.test.ts
index 97485b1e..ff1191b8 100644
--- a/laya-ts/tests/structured.test.ts
+++ b/laya-ts/tests/structured.test.ts
@@ -101,6 +101,12 @@ describe("structured/projection", () => {
describe("structured/rejections", () => {
const bad = (schema: unknown) => () => questionsFromJsonSchema(schema);
+ it("rejects enum values with the same choice label", () => {
+ for (const values of [[1, "1"], [null, "null"], [true, "true"]]) {
+ expect(bad({ type: "object", properties: { x: { enum: values } } }))
+ .toThrowError(/properties\.x: enum values produce duplicate choice labels/);
+ }
+ });
it("rejects a free string", () => {
expect(bad({ type: "object", properties: { a: { type: "string" } } }))
.toThrowError(/properties\.a: a free string cannot be a fixed option set/);
diff --git a/laya/structured.py b/laya/structured.py
index 5bb226cf..523e02d8 100644
--- a/laya/structured.py
+++ b/laya/structured.py
@@ -80,6 +80,8 @@ def _enum_field(path: str, name: str, values: Sequence[Any], description: Option
if all(isinstance(v, bool) for v in values):
return _noul_field(path, name, description)
options = [(("null" if v is None else str(v)), v) for v in values]
+ if len({label for label, _ in options}) != len(options):
+ raise SchemaError("%s: enum values produce duplicate choice labels" % path)
criteria = {label: None for label, _ in options}
question = {
"type": "choice",
diff --git a/tests/test_structured.py b/tests/test_structured.py
index 5ab3ac8e..c288fdb7 100644
--- a/tests/test_structured.py
+++ b/tests/test_structured.py
@@ -115,6 +115,14 @@ check_raises("reject/too many properties", SchemaError,
_bad({"type": "object", "properties": {("p%d" % i): {"type": "boolean"} for i in range(33)}}))
check_raises("reject/too many options", SchemaError,
_bad({"type": "object", "properties": {"a": {"type": "string", "enum": ["v%d" % i for i in range(33)]}}}))
+for colliding in ([1, "1"], [None, "null"], [True, "True"]):
+ check_raises("reject/colliding enum %r" % colliding, SchemaError,
+ _bad({"type": "object", "properties": {"x": {"enum": colliding}}}))
+try:
+ questions_from_json_schema({"type": "object", "properties": {"x": {"enum": [1, "1"]}}})
+except SchemaError as exc:
+ check("reject/colliding enum names the field", str(exc),
+ "properties.x: enum values produce duplicate choice labels")
check_raises("reject/non-object root", SchemaError, _bad({"type": "array"}))
check_raises("reject/empty properties", SchemaError, _bad({"type": "object", "properties": {}}))