generated from pagopa/template-payments-java-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ErrorHandling and LocalDateTime json serialization (#46)
- Loading branch information
1 parent
654ff4c
commit 60beed8
Showing
11 changed files
with
360 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/it/gov/pagopa/pu/debtpositions/config/json/JsonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package it.gov.pagopa.pu.debtpositions.config.json; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.TimeZone; | ||
|
||
@Configuration | ||
public class JsonConfig { | ||
|
||
@Bean | ||
public ObjectMapper objectMapper() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.registerModule(configureDateTimeModule()); | ||
mapper.registerModule(new Jdk8Module()); | ||
mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.DEFAULT)); | ||
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | ||
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); | ||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); | ||
mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.PUBLIC_ONLY); | ||
mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.PUBLIC_ONLY); | ||
mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.PUBLIC_ONLY); | ||
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); | ||
mapper.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY); | ||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
mapper.setTimeZone(TimeZone.getDefault()); | ||
return mapper; | ||
} | ||
|
||
/** openApi is documenting LocalDateTime as date-time, which is interpreted as an OffsetDateTime by openApiGenerator */ | ||
private static SimpleModule configureDateTimeModule() { | ||
return new JavaTimeModule() | ||
.addSerializer(LocalDateTime.class, new LocalDateTimeToOffsetDateTimeSerializer()) | ||
.addDeserializer(LocalDateTime.class, new OffsetDateTimeToLocalDateTimeDeserializer()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...it/gov/pagopa/pu/debtpositions/config/json/OffsetDateTimeToLocalDateTimeDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package it.gov.pagopa.pu.debtpositions.config.json; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.io.IOException; | ||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
|
||
|
||
@Configuration | ||
public class OffsetDateTimeToLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { | ||
|
||
@Override | ||
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
|
||
String dateString = p.getValueAsString(); | ||
if(dateString.contains("+")){ | ||
return OffsetDateTime.parse(dateString).toLocalDateTime(); | ||
} else { | ||
return LocalDateTime.parse(dateString); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...teTimeToOffsetDateTimeSerializerTest.java → ...teTimeToOffsetDateTimeSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ov/pagopa/pu/debtpositions/config/json/OffsetDateTimeToLocalDateTimeDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package it.gov.pagopa.pu.debtpositions.config.json; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.IOException; | ||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
|
||
class OffsetDateTimeToLocalDateTimeDeserializerTest { | ||
|
||
private final OffsetDateTimeToLocalDateTimeDeserializer deserializer = new OffsetDateTimeToLocalDateTimeDeserializer(); | ||
|
||
@Test | ||
void givenOffsetDateTimeWhenThenOk() throws IOException { | ||
// Given | ||
OffsetDateTime offsetDateTime = OffsetDateTime.now(); | ||
JsonParser parser = Mockito.mock(JsonParser.class); | ||
Mockito.when(parser.getValueAsString()) | ||
.thenReturn(offsetDateTime.toString()); | ||
|
||
// When | ||
LocalDateTime result = deserializer.deserialize(parser, null); | ||
|
||
// Then | ||
Assertions.assertEquals(offsetDateTime.toLocalDateTime(), result); | ||
} | ||
|
||
@Test | ||
void givenLocalDateTimeWhenThenOk() throws IOException { | ||
// Given | ||
LocalDateTime localDateTime = LocalDateTime.now(); | ||
JsonParser parser = Mockito.mock(JsonParser.class); | ||
Mockito.when(parser.getValueAsString()) | ||
.thenReturn(localDateTime.toString()); | ||
|
||
// When | ||
LocalDateTime result = deserializer.deserialize(parser, null); | ||
|
||
// Then | ||
Assertions.assertEquals(localDateTime, result); | ||
} | ||
} |
Oops, something went wrong.