openassistant: handle non-primitive values in const/enum validation
- Commit
596748f63788e52507bea438427e8e33ab4fc2e9- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:16 UTC (28 days ago)
- Parents
05b6fca
JsonSchemaValidator.validateConst and validateEnum called .jsonPrimitive on the validated value, which throws IllegalArgumentException when the model emits an object or array for a const/enum-constrained field (or when the schema itself carries a non-primitive const/enum entry).
Why this matters: InferenceService.runIntentInference calls validateJsonAgainstSchema on every streaming candidate and treats a throw as a failed inference (RESULT_ERROR_CODE), aborting the whole extraction. A single malformed candidate like {"kind": {"nested": 1}} against a const schema killed the request instead of letting inference continue streaming toward a valid candidate.
The fix compares content for primitives and falls back to toString() for anything else, so non-primitive values and schema entries always produce validation-error strings and the validator's contract (returns String?, never throws on valid JSON input) holds. Primitive-case messages are unchanged.
Verified with :openassistant:testDevUnitTest (full suite green), including the new constAndEnumWithNonPrimitiveValues regression test, which fails with IllegalArgumentException on the unfixed code.Diff
openassistant/src/main/java/com/vayunmathur/openassistant/util/JsonSchemaValidator.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/util/JsonSchemaValidator.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/util/JsonSchemaValidator.ktindex 9ec1b8a9c9..bf3f11f767 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/util/JsonSchemaValidator.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/util/JsonSchemaValidator.kt@@ -5,6 +5,7 @@ import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonObject+import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.jsonPrimitive object JsonSchemaValidator {@@ -144,18 +145,24 @@ object JsonSchemaValidator { } private fun validateConst(value: JsonElement, propSchema: JsonObject, fullPath: String): String? {- val constValue = propSchema["const"]?.jsonPrimitive?.content ?: return null- if (value.jsonPrimitive.content != constValue) {- return "Field '$fullPath' must be '$constValue' but got '${value.jsonPrimitive.content}'"+ val constElement = propSchema["const"] ?: return null+ // Values are model output and the schema arrives with the starting Intent: either side+ // may be a non-primitive, which must be a validation error, never a throw (callers treat+ // a throw as a failed inference instead of continuing to stream).+ val constContent = (constElement as? JsonPrimitive)?.content ?: constElement.toString()+ val valueContent = (value as? JsonPrimitive)?.content ?: value.toString()+ if (valueContent != constContent) {+ return "Field '$fullPath' must be '$constContent' but got '$valueContent'" } return null } private fun validateEnum(value: JsonElement, propSchema: JsonObject, fullPath: String): String? { val enumValues = propSchema["enum"] as? JsonArray ?: return null- val allowed = enumValues.map { it.jsonPrimitive.content }- if (value.jsonPrimitive.content !in allowed) {- return "Field '$fullPath' has invalid value '${value.jsonPrimitive.content}'. " ++ val allowed = enumValues.map { (it as? JsonPrimitive)?.content ?: it.toString() }+ val valueContent = (value as? JsonPrimitive)?.content ?: value.toString()+ if (valueContent !in allowed) {+ return "Field '$fullPath' has invalid value '$valueContent'. " + "Allowed values: $allowed" } return null
openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonValidationTest.kt
diff --git a/openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonValidationTest.kt b/openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonValidationTest.ktindex 989e1d3057..39de54535a 100644--- a/openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonValidationTest.kt+++ b/openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonValidationTest.kt@@ -56,6 +56,23 @@ class JsonValidationTest { assertNull(validate("""{"kind":"note","level":"high"}""", schema)) } + @Test fun constAndEnumWithNonPrimitiveValues() {+ val schema = """{"properties":{"kind":{"const":"note"},"level":{"enum":["low","high"]}}}"""+ // Null is a primitive: mismatch, not a throw.+ val nullErr = validate("""{"kind":null,"level":"low"}""", schema)+ assertTrue(nullErr!!.contains("must be 'note'"), nullErr)+ // Object / array values must yield validation errors, never throw.+ val objErr = validate("""{"kind":{"nested":1},"level":"low"}""", schema)+ assertTrue(objErr!!.contains("must be 'note'"), objErr)+ val arrErr = validate("""{"kind":"note","level":["low"]}""", schema)+ assertTrue(arrErr!!.contains("invalid value"), arrErr)+ // Non-primitive const/enum entries in the schema itself must not throw either.+ assertNotNull(validate(+ """{"kind":"note","level":"low"}""",+ """{"properties":{"kind":{"const":{"nested":1}},"level":{"enum":[["low"],"high"]}}}""",+ ))+ }+ @Test fun arrayItemsAreValidated() { val schema = """{"type":"array","items":{"type":"object","properties":{"n":{}},"required":["n"]}}""" assertNull(validate("""[{"n":1},{"n":2}]""", schema))