openassistant: report malformed schema entries instead of throwing
- Commit
c5a72cc0f1f54430bdef0602ef530fce59d409ca- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:20 UTC (28 days ago)
- Parents
596748f
JsonSchemaValidator.validateType called .jsonPrimitive on the schema's 'type' value and checkRequiredFields called it on every 'required' entry, throwing IllegalArgumentException when a caller-supplied schema carries a non-primitive there (e.g. 'type': ['object'] or 'required': ['a', {}]).
Why this matters: the schema arrives with the starting Intent from another app, so it is untrusted input. InferenceService.runIntentInference treats a throw from validateJsonAgainstSchema as a failed inference (RESULT_ERROR_CODE), aborting the whole extraction instead of continuing to stream. The observable outcome is unchanged (the inference still fails), but it now fails through the designed validation-error channel with a message naming the malformed entry.
Non-string 'type' and 'required' entries now return error strings ('Invalid ... in schema: must be a string'). Primitive behavior is byte-identical. This completes the validator totality started in the const/enum fix: validateJsonAgainstSchema returns String? and never throws on valid-JSON input from either side.
Verified with :openassistant:testDevUnitTest (full suite green), including the new malformedSchemaEntriesAreErrorsNotThrows regression test, which fails with IllegalArgumentException from validateType 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 bf3f11f767..94397236ea 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/util/JsonSchemaValidator.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/util/JsonSchemaValidator.kt@@ -6,7 +6,6 @@ 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 { fun validateJsonAgainstSchema(jsonString: String, schemaString: String): String? {@@ -92,7 +91,12 @@ object JsonSchemaValidator { private fun validateType(data: JsonElement, schema: JsonObject, path: String): String? { val pathPrefix = if (path.isEmpty()) "" else "at $path: "- val expectedType = schema["type"]?.jsonPrimitive?.content ?: return null+ val typeElement = schema["type"] ?: return null+ // The schema arrives with the starting Intent and may be malformed: a non-string+ // 'type' must be a validation error, never a throw (callers treat a throw as a+ // failed inference instead of continuing to stream).+ val expectedType = (typeElement as? JsonPrimitive)?.content+ ?: return "${pathPrefix}Invalid 'type' in schema: must be a string" if (expectedType == "object" && data !is JsonObject) { return "${pathPrefix}Expected an object but got ${data::class.simpleName}" }@@ -120,8 +124,11 @@ object JsonSchemaValidator { } private fun checkRequiredFields(data: JsonObject, schema: JsonObject, path: String): String? {+ val pathPrefix = if (path.isEmpty()) "" else "at $path: " (schema["required"] as? JsonArray)?.forEach { req ->- val fieldName = req.jsonPrimitive.content+ // A non-string entry is a malformed schema: report it, never throw.+ val fieldName = (req as? JsonPrimitive)?.content+ ?: return "${pathPrefix}Invalid 'required' entry in schema: must be a string" if (!data.containsKey(fieldName)) { val fullPath = if (path.isEmpty()) fieldName else "$path.$fieldName" return "Missing required field: '$fullPath'"
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 39de54535a..c33c2446ff 100644--- a/openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonValidationTest.kt+++ b/openassistant/src/test/java/com/vayunmathur/openassistant/util/JsonValidationTest.kt@@ -47,6 +47,19 @@ class JsonValidationTest { assertTrue(validate("""{"a":1}""", """{"type":"object"}""")!!.contains("Unexpected field")) } + @Test fun malformedSchemaEntriesAreErrorsNotThrows() {+ // Non-primitive "type" and "required" entries come from the caller's schema:+ // they must surface as validation errors, never throw (a throw aborts the+ // whole inference instead of letting it continue streaming).+ val badType = validate("""{"a":1}""", """{"type":["object"],"properties":{"a":{}}}""")+ assertNotNull(badType)+ val badRequired = validate(+ """{"a":1}""",+ """{"properties":{"a":{}},"required":["a",{}]}""",+ )+ assertTrue(badRequired!!.contains("required"), badRequired)+ }+ @Test fun constAndEnumConstraints() { val schema = """{"properties":{"kind":{"const":"note"},"level":{"enum":["low","high"]}}}""" assertTrue(validate("""{"kind":"task","level":"low"}""", schema)!!.contains("must be 'note'"))