From 3483f71638f7205e6486ec0137d2862f3df9c42e Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Sun, 8 Mar 2026 16:01:13 -0400 Subject: [PATCH] handle runtime exception gracefully --- .../java/com/networknt/schema/Schema.java | 4 +++ .../com/networknt/schema/SchemaException.java | 5 +++ .../java/com/networknt/schema/SchemaTest.java | 32 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/main/java/com/networknt/schema/Schema.java b/src/main/java/com/networknt/schema/Schema.java index 87999f107..3b3181dcc 100644 --- a/src/main/java/com/networknt/schema/Schema.java +++ b/src/main/java/com/networknt/schema/Schema.java @@ -1192,6 +1192,8 @@ private JsonNode deserialize(String input, InputFormat inputFormat) { return this.getSchemaContext().getSchemaRegistry().readTree(input, inputFormat); } catch (IOException e) { throw new UncheckedIOException("Invalid input", e); + } catch (RuntimeException e) { + throw new SchemaException("Invalid " + inputFormat + " input: " + e.getMessage(), e); } } @@ -1213,6 +1215,8 @@ private JsonNode deserialize(AbsoluteIri input, InputFormat inputFormat) { } } catch (IOException e) { throw new UncheckedIOException("Invalid input", e); + } catch (RuntimeException e) { + throw new SchemaException("Invalid " + inputFormat + " input from " + input + ": " + e.getMessage(), e); } } diff --git a/src/main/java/com/networknt/schema/SchemaException.java b/src/main/java/com/networknt/schema/SchemaException.java index fe94a0ef3..3166461b0 100644 --- a/src/main/java/com/networknt/schema/SchemaException.java +++ b/src/main/java/com/networknt/schema/SchemaException.java @@ -40,6 +40,11 @@ public SchemaException(Throwable throwable) { this.error = null; } + public SchemaException(String message, Throwable throwable) { + super(message, throwable); + this.error = null; + } + @Override public String getMessage() { return this.error != null ? this.error.getMessage() : super.getMessage(); diff --git a/src/test/java/com/networknt/schema/SchemaTest.java b/src/test/java/com/networknt/schema/SchemaTest.java index ff1ac400b..f888c5372 100644 --- a/src/test/java/com/networknt/schema/SchemaTest.java +++ b/src/test/java/com/networknt/schema/SchemaTest.java @@ -98,4 +98,36 @@ public void run() { throw instance[0]; } } + + /** + * Issue 1231. + *

+ * When the YAML input length lands exactly on the snakeyaml-engine StreamReader + * 1024-char chunk boundary, an IndexOutOfBoundsException is thrown internally. + * This should be caught and wrapped in a SchemaException with a clear message + * rather than propagating as a raw IndexOutOfBoundsException. + */ + @Test + void yamlInputAtChunkBoundaryShouldThrowSchemaException() { + // Build a YAML key that is exactly 1024 chars so the total YAML content + // hits the snakeyaml-engine StreamReader boundary. + String longKey = "a".repeat(1024); + String yamlInput = longKey + ": value\n"; + + String schemaData = "{\"type\": \"object\"}"; + SchemaRegistry factory = SchemaRegistry.withDialect(com.networknt.schema.dialect.Dialects.getOpenApi31()); + Schema schema = factory.getSchema(schemaData); + + // Before the fix this threw IndexOutOfBoundsException directly. + // After the fix it should throw SchemaException with a clear message. + try { + schema.validate(yamlInput, InputFormat.YAML); + } catch (IndexOutOfBoundsException e) { + // Raw IOOBE must not escape - this is the bug we're fixing + throw new AssertionError("Expected SchemaException but got raw IndexOutOfBoundsException", e); + } catch (RuntimeException e) { + // Any other RuntimeException (including SchemaException) is acceptable + // as long as it's not the raw IndexOutOfBoundsException + } + } }