|
24 | 24 | import static org.assertj.core.api.Assertions.assertThat; |
25 | 25 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
26 | 26 |
|
| 27 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 28 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 30 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 31 | + |
27 | 32 | /** |
28 | 33 | * @author Christian Tzolov |
29 | 34 | */ |
@@ -122,6 +127,55 @@ public void copyToTarget() { |
122 | 127 | assertThat(target.getSpecificField()).isNull(); |
123 | 128 | } |
124 | 129 |
|
| 130 | + @Test |
| 131 | + public void jsonToMap_emptyStringAsNullObject() { |
| 132 | + String json = "{\"name\":\"\", \"age\":30}"; |
| 133 | + // For Map: empty string remains "" |
| 134 | + Map<String, Object> map = ModelOptionsUtils.jsonToMap(json); |
| 135 | + assertThat(map.get("name")).isEqualTo(""); |
| 136 | + assertThat(map.get("age")).isEqualTo(30); |
| 137 | + |
| 138 | + // Custom ObjectMapper: still "" for Map |
| 139 | + ObjectMapper strictMapper = JsonMapper.builder() |
| 140 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 141 | + .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) |
| 142 | + .build() |
| 143 | + .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, false); |
| 144 | + Map<String, Object> mapStrict = ModelOptionsUtils.jsonToMap(json, strictMapper); |
| 145 | + assertThat(mapStrict.get("name")).isEqualTo(""); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void pojo_emptyStringAsNullObject() throws Exception { |
| 150 | + String json = "{\"name\":\"\", \"age\":30}"; |
| 151 | + |
| 152 | + // POJO with default OBJECT_MAPPER (feature enabled) |
| 153 | + Person person = ModelOptionsUtils.OBJECT_MAPPER.readValue(json, Person.class); |
| 154 | + assertThat(person.name).isEqualTo(""); // String remains "" |
| 155 | + assertThat(person.age).isEqualTo(30); // Integer is fine |
| 156 | + |
| 157 | + String jsonWithEmptyAge = "{\"name\":\"John\", \"age\":\"\"}"; |
| 158 | + Person person2 = ModelOptionsUtils.OBJECT_MAPPER.readValue(jsonWithEmptyAge, Person.class); |
| 159 | + assertThat(person2.name).isEqualTo("John"); |
| 160 | + assertThat(person2.age).isNull(); // Integer: "" → null |
| 161 | + |
| 162 | + // POJO with feature disabled: should fail for Integer field |
| 163 | + ObjectMapper strictMapper = JsonMapper.builder() |
| 164 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 165 | + .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) |
| 166 | + .build() |
| 167 | + .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, false); |
| 168 | + assertThatThrownBy(() -> strictMapper.readValue(jsonWithEmptyAge, Person.class)).isInstanceOf(Exception.class); |
| 169 | + } |
| 170 | + |
| 171 | + public static class Person { |
| 172 | + |
| 173 | + public String name; |
| 174 | + |
| 175 | + public Integer age; |
| 176 | + |
| 177 | + } |
| 178 | + |
125 | 179 | @Test |
126 | 180 | public void getJsonPropertyValues() { |
127 | 181 | record TestRecord(@JsonProperty("field1") String fieldA, @JsonProperty("field2") String fieldB) { |
|
0 commit comments