Skip to content

Commit

Permalink
[#13768] URI should be deserialized from empty string to null.
Browse files Browse the repository at this point in the history
See FasterXML/jackson-databind#398, but we
don't need this edge case handling, client sends empty selection as
empty string, so this should be handled as null.
  • Loading branch information
attila-it4all committed Oct 7, 2023
1 parent 943a6a1 commit 2978b17
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -14,8 +15,9 @@
import org.slf4j.LoggerFactory;
import org.smartbit4all.api.binarydata.BinaryData;
import org.smartbit4all.api.binarydata.BinaryDataOutputStream;
import org.smartbit4all.core.object.zone.ZonedLocalDateDeserializer;
import org.smartbit4all.core.object.zone.ZonedLocalDateTimeDeserializer;
import org.smartbit4all.core.object.serialize.UriDeserializer;
import org.smartbit4all.core.object.serialize.ZonedLocalDateDeserializer;
import org.smartbit4all.core.object.serialize.ZonedLocalDateTimeDeserializer;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
Expand Down Expand Up @@ -51,6 +53,7 @@ public ObjectSerializerByObjectMapper() {
SimpleModule module = new SimpleModule();
module.addDeserializer(LocalDate.class, ZonedLocalDateDeserializer.INSTANCE);
module.addDeserializer(LocalDateTime.class, ZonedLocalDateTimeDeserializer.INSTANCE);
module.addDeserializer(URI.class, UriDeserializer.INSTANCE);
objectMapper.registerModule(module);
// Task 5641: Storage - OffsetDateTime formatting with default serializer
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.smartbit4all.core.object.serialize;

import java.io.IOException;
import java.net.URI;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

public class UriDeserializer extends JsonDeserializer<URI> {

public static final UriDeserializer INSTANCE =
new UriDeserializer();

private UriDeserializer() {}

@Override
public URI deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
String text = p.getValueAsString();
if (text != null) {
if (text.length() == 0 || (text = text.trim()).length() == 0) {
// we don't want to treat "" as a valid URI
return null;
}
return URI.create(text);
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.smartbit4all.core.object.zone;
package org.smartbit4all.core.object.serialize;

import java.io.IOException;
import java.time.DateTimeException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.smartbit4all.core.object.zone;
package org.smartbit4all.core.object.serialize;

import java.io.IOException;
import java.time.DateTimeException;
Expand Down

0 comments on commit 2978b17

Please sign in to comment.