From 52da80f8326831b55f634bf14e3568dff51cc126 Mon Sep 17 00:00:00 2001 From: Markus Jung Date: Sun, 4 Aug 2024 11:21:10 +0200 Subject: [PATCH] [JOHNZON-407] fix NPE in JsonArrayBuilderImpl (cherry picked from commit 06caf92fc57e1c4a5f974079bb399e1839e70b11) --- .../apache/johnzon/core/JsonArrayBuilderImpl.java | 2 +- .../johnzon/core/JsonArrayBuilderImplTest.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayBuilderImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayBuilderImpl.java index 87c9a161..77698ba6 100644 --- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayBuilderImpl.java +++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonArrayBuilderImpl.java @@ -57,13 +57,13 @@ public JsonArrayBuilderImpl(final Collection initialData, final BufferStrateg final RejectDuplicateKeysMode rejectDuplicateKeysMode, final JsonProviderImpl jsonProvider) { this.bufferProvider = provider; this.rejectDuplicateKeysMode = rejectDuplicateKeysMode; + this.jsonProvider = jsonProvider; this.tmpList = new ArrayList<>(); if (!initialData.isEmpty()) { for (Object initialValue : initialData) { add(initialValue); } } - this.jsonProvider = jsonProvider; } @Override diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonArrayBuilderImplTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonArrayBuilderImplTest.java index 85d14eb6..7e54da04 100644 --- a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonArrayBuilderImplTest.java +++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonArrayBuilderImplTest.java @@ -202,4 +202,17 @@ public void testCreateArrayBuilderWithCollectionInitialData() { JsonArray jsonArray2 = otherBuilder.build(); Assert.assertEquals("[\"a\",\"b\",\"c\",\"d\"]", jsonArray2.toString()); } + + /** + * Testcase for JOHNZON-407 + */ + @Test + public void issue407() { + Collection initialData = new ArrayList<>(); + initialData.add(BigDecimal.ONE); + initialData.add(BigInteger.ONE); + + // This must not throw an exception + Json.createArrayBuilder(initialData).build(); + } }