From 2c1e0991e4dcfc0b7f73977016d6986248bf899f Mon Sep 17 00:00:00 2001 From: Piotr Rzysko Date: Wed, 22 May 2024 20:31:17 +0200 Subject: [PATCH] schema-based parsing: fix handling non-existent fields in schema --- .../org/simdjson/ConstructorArgumentsMap.java | 3 +++ .../ObjectSchemaBasedParsingTest.java | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/org/simdjson/ConstructorArgumentsMap.java b/src/main/java/org/simdjson/ConstructorArgumentsMap.java index 259c299..d53a544 100644 --- a/src/main/java/org/simdjson/ConstructorArgumentsMap.java +++ b/src/main/java/org/simdjson/ConstructorArgumentsMap.java @@ -51,6 +51,9 @@ ConstructorArgument get(byte[] buffer, int len) { int place = findPlace(buffer, len); for (int i = 0; i < capacity; i++) { byte[] key = keys[place]; + if (key == null) { + return null; + } if (Arrays.equals(key, 0, key.length, buffer, 0, len)) { return arguments[place]; } diff --git a/src/test/java/org/simdjson/ObjectSchemaBasedParsingTest.java b/src/test/java/org/simdjson/ObjectSchemaBasedParsingTest.java index dcb6545..c19265c 100644 --- a/src/test/java/org/simdjson/ObjectSchemaBasedParsingTest.java +++ b/src/test/java/org/simdjson/ObjectSchemaBasedParsingTest.java @@ -642,6 +642,25 @@ public void listsWithoutElementTypeAreNotSupported() { .hasMessage("Undefined list element type."); } + @Test + public void issue50() { + // given + SimdJsonParser parser = new SimdJsonParser(); + byte[] json = toUtf8("{\"name\": \"John\", \"age\": 30, \"aaa\": 1, \"bbb\": 2, \"ccc\": 3}"); + + // when + Issue50 object = parser.parse(json, json.length, Issue50.class); + + // then + assertThat(object.aaa()).isEqualTo(1); + assertThat(object.bbb()).isEqualTo(2); + assertThat(object.ccc()).isEqualTo(3); + } + + private record Issue50(long aaa, long bbb, long ccc) { + + } + private record RecordWithExplicitFieldNames(@JsonFieldName("ąćśńźż") long firstField, @JsonFieldName("\u20A9\u0E3F") long secondField, @JsonFieldName("αβγ") long thirdField,