Skip to content

Adding the Optional support to the JacksonFactory #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,55 @@

package com.amazonaws.services.lambda.runtime.serialization.factories;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class JacksonFactoryTest {

private JacksonFactory jacksonFactory = JacksonFactory.getInstance();
private ObjectMapper mapper = jacksonFactory.getMapper();

@Test
public void deserializeVoidAsNonNull() throws Exception {
JacksonFactory instance = JacksonFactory.getInstance();
Void actual = instance.getMapper().readValue("{}", Void.class);
Void actual = mapper.readValue("{}", Void.class);
assertNotNull(actual);
}

@Test
public void testOptionalAsEmpty() throws JsonProcessingException {
Contact emptyEmail = new Contact("Example Co.", Optional.empty());
String emptyEmailJson = mapper.writeValueAsString(emptyEmail);
assertEquals("{\"name\":\"Example Co.\",\"email\":null}", emptyEmailJson);
}

@Test
public void testOptionalWithValue() throws JsonProcessingException {
Contact withEmail = new Contact("Example Co.", Optional.of("[email protected]"));
String withEmailJson = mapper.writeValueAsString(withEmail);
assertEquals("{\"name\":\"Example Co.\",\"email\":\"[email protected]\"}", withEmailJson);
}

static class Contact {
private final String name;
private final Optional<String> email;

public Contact(String name, Optional<String> email) {
this.name = name;
this.email = email;
}

public String getName() {
return name;
}

public Optional<String> getEmail() {
return email;
}
}
}
9 changes: 8 additions & 1 deletion aws-lambda-java-serialization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,22 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<relocation.prefix>com.amazonaws.lambda.thirdparty</relocation.prefix>
<jackson.version>2.10.0</jackson.version>
</properties>

<!-- If you're adding a new dependency, make sure it and its dependencies are relocated in the shade stage below -->
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -112,6 +113,7 @@ private static ObjectMapper createObjectMapper() {
SimpleModule module = new SimpleModule();
module.addDeserializer(Void.class, new VoidDeserializer());
mapper.registerModule(module);
mapper.registerModule(new Jdk8Module());

return mapper;
}
Expand Down