register available modules with the object mapper

add tests
This commit is contained in:
Veerendra Vellanki
2024-07-15 13:10:38 -05:00
committed by Christian Tzolov
parent 92b06346fd
commit 60308eab77
2 changed files with 28 additions and 2 deletions

View File

@@ -25,6 +25,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
@@ -181,9 +182,8 @@ public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {
* @return Configured object mapper.
*/
protected ObjectMapper getObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = JsonMapper.builder().findAndAddModules().build();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new JavaTimeModule());
return mapper;
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.ai.converter;
import java.time.LocalDate;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -60,6 +61,13 @@ class BeanOutputConverterTest {
assertThat(testClass.getSomeString()).isEqualTo("some value");
}
@Test
public void convertClassWithDateType() {
var converter = new BeanOutputConverter<>(TestClassWithDateProperty.class);
var testClass = converter.convert("{ \"someString\": \"2020-01-01\" }");
assertThat(testClass.getSomeString()).isEqualTo(LocalDate.of(2020, 1, 1));
}
@Test
public void convertTypeReference() {
var converter = new BeanOutputConverter<>(new ParameterizedTypeReference<TestClass>() {
@@ -255,6 +263,24 @@ class BeanOutputConverterTest {
}
public static class TestClassWithDateProperty {
private LocalDate someString;
@SuppressWarnings("unused")
public TestClassWithDateProperty() {
}
public TestClassWithDateProperty(LocalDate someString) {
this.someString = someString;
}
public LocalDate getSomeString() {
return someString;
}
}
public static class TestClassWithJsonAnnotations {
@JsonProperty("string_property")