From cf04b4228ff1bcbd1ce92869a480bb715f497e26 Mon Sep 17 00:00:00 2001 From: Gytis Trikleris Date: Wed, 17 Feb 2021 17:58:57 +0100 Subject: [PATCH] Handle null values in REST Data Panache JSONB serializer --- .../hal/HalEntityWrapperJsonbSerializer.java | 10 +++++++++- .../runtime/hal/AbstractSerializersTest.java | 14 +++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/extensions/panache/rest-data-panache/runtime/src/main/java/io/quarkus/rest/data/panache/runtime/hal/HalEntityWrapperJsonbSerializer.java b/extensions/panache/rest-data-panache/runtime/src/main/java/io/quarkus/rest/data/panache/runtime/hal/HalEntityWrapperJsonbSerializer.java index 00c4d29380ba5..b860fbbcbe92e 100644 --- a/extensions/panache/rest-data-panache/runtime/src/main/java/io/quarkus/rest/data/panache/runtime/hal/HalEntityWrapperJsonbSerializer.java +++ b/extensions/panache/rest-data-panache/runtime/src/main/java/io/quarkus/rest/data/panache/runtime/hal/HalEntityWrapperJsonbSerializer.java @@ -37,7 +37,7 @@ public void serialize(HalEntityWrapper wrapper, JsonGenerator generator, Seriali for (PropertyModel property : classModel.getSortedProperties()) { if (property.isReadable()) { - context.serialize(property.getWriteName(), property.getValue(entity), generator); + writeValue(property.getWriteName(), property.getValue(entity), generator, context); } } @@ -48,6 +48,14 @@ public void serialize(HalEntityWrapper wrapper, JsonGenerator generator, Seriali } } + private void writeValue(String name, Object value, JsonGenerator generator, SerializationContext context) { + if (value == null) { + generator.writeNull(name); + } else { + context.serialize(name, value, generator); + } + } + private void writeLinks(Object entity, JsonGenerator generator, SerializationContext context) { Map links = linksExtractor.getLinks(entity); context.serialize("_links", links, generator); diff --git a/extensions/panache/rest-data-panache/runtime/src/test/java/io/quarkus/rest/data/panache/runtime/hal/AbstractSerializersTest.java b/extensions/panache/rest-data-panache/runtime/src/test/java/io/quarkus/rest/data/panache/runtime/hal/AbstractSerializersTest.java index 3e69d37def3e5..59ab3f35e74cd 100644 --- a/extensions/panache/rest-data-panache/runtime/src/test/java/io/quarkus/rest/data/panache/runtime/hal/AbstractSerializersTest.java +++ b/extensions/panache/rest-data-panache/runtime/src/test/java/io/quarkus/rest/data/panache/runtime/hal/AbstractSerializersTest.java @@ -23,6 +23,14 @@ void shouldSerializeOneBook() { assertBook(book, jsonReader.readObject()); } + @Test + void shouldSerializeOneBookWithNullName() { + Book book = new Book(1, null); + JsonReader jsonReader = Json.createReader(new StringReader(toJson(new HalEntityWrapper(book)))); + + assertBook(book, jsonReader.readObject()); + } + @Test void shouldSerializeCollectionOfBooks() { Book book = new Book(1, "Black Swan"); @@ -39,7 +47,11 @@ void shouldSerializeCollectionOfBooks() { private void assertBook(Book book, JsonObject bookJson) { assertThat(bookJson.getInt("id")).isEqualTo(book.id); - assertThat(bookJson.getString("book-name")).isEqualTo(book.getName()); + if (bookJson.isNull("book-name")) { + assertThat(book.getName()).isNull(); + } else { + assertThat(bookJson.getString("book-name")).isEqualTo(book.getName()); + } assertThat(bookJson.containsKey("ignored")).isFalse(); JsonObject bookLinksJson = bookJson.getJsonObject("_links");