Skip to content

Commit

Permalink
Merge pull request #15150 from gytis/rest-data-panache-jsonb-null-bug
Browse files Browse the repository at this point in the history
Handle null values in REST Data Panache JSONB serializer
  • Loading branch information
gsmet authored Feb 18, 2021
2 parents 939374d + cf04b42 commit 720cf4a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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<String, HalLink> links = linksExtractor.getLinks(entity);
context.serialize("_links", links, generator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down

0 comments on commit 720cf4a

Please sign in to comment.