BAEL-7277: Disable fail_on_empty_beans in Jackson (#15394)

This commit is contained in:
ACHRAF TAITAI 2023-12-15 08:45:42 +01:00 committed by GitHub
parent 51f2ed495b
commit d8bbd3d027
2 changed files with 24 additions and 3 deletions

View File

@ -0,0 +1,5 @@
package com.baeldung.exceptions;
public class EmptyObject {
}

View File

@ -2,13 +2,15 @@ package com.baeldung.mappingexception;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import com.baeldung.exceptions.EmptyObject;
import com.baeldung.exceptions.Person;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
@ -73,4 +75,18 @@ public class JacksonMappingExceptionUnitTest {
assertEquals(Arrays.asList("Amsterdam", "Tamassint"), country.getCities());
}
@Test
public void givenEmptyBean_whenSerializing_thenCorrect() throws JsonProcessingException {
// Create an ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
// Disable fail_on_empty_beans during serialization
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// Create an empty object
EmptyObject emptyObject = new EmptyObject();
// Serialize the empty object
String json = objectMapper.writeValueAsString(emptyObject);
// Verify that serialization is successful
assertEquals("{}", json);
}
}