-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
add test for a Java Record with an Optional param #5336
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
Open
pjfanning
wants to merge
5
commits into
FasterXML:3.x
Choose a base branch
from
pjfanning:record-test
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5bedc10
Create RecordWithOptionalParamTest.java
pjfanning 9e28a23
Update RecordWithOptionalParamTest.java
pjfanning 2a4477d
Update RecordWithOptionalParamTest.java
pjfanning d96278d
Update RecordWithOptionalParamTest.java
pjfanning c94c1ae
Simplify test a bit (remove serialization-side config)
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/test/java/tools/jackson/databind/records/RecordWithOptionalParamTest.java
This file contains hidden or 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,87 @@ | ||
package tools.jackson.databind.records; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import org.junit.jupiter.api.Test; | ||
import tools.jackson.databind.ObjectMapper; | ||
import tools.jackson.databind.json.JsonMapper; | ||
import tools.jackson.databind.testutil.DatabindTestUtil; | ||
import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
// [databind#5335] When deserializing records, java.util.Optional parameters should be not be | ||
// deserialized as null (Optional.empty() should be used instead) | ||
public class RecordWithOptionalParamTest | ||
extends DatabindTestUtil | ||
{ | ||
record RecordWithOptionalParam(String name, Optional<String> optional) { } | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
@Test | ||
void serialize() throws Exception | ||
{ | ||
RecordWithOptionalParam input = new RecordWithOptionalParam("v1", Optional.of("v2")); | ||
String json = MAPPER.writeValueAsString(input); | ||
String expected = a2q("{'name':'v1','optional':'v2'}"); | ||
assertEquals(expected, json); | ||
} | ||
|
||
@Test | ||
void serializeEmpty() throws Exception | ||
{ | ||
RecordWithOptionalParam input = new RecordWithOptionalParam("v1", Optional.empty()); | ||
String json = MAPPER.writeValueAsString(input); | ||
String expected = a2q("{'name':'v1','optional':null}"); | ||
assertEquals(expected, json); | ||
} | ||
|
||
@Test | ||
void deserializeNonEmpty() throws Exception | ||
{ | ||
String json = a2q("{'name':'v1','optional':'v2'}"); | ||
RecordWithOptionalParam output = MAPPER.readValue(json, RecordWithOptionalParam.class); | ||
assertEquals("v1", output.name()); | ||
assertEquals(Optional.of("v2"), output.optional()); | ||
} | ||
|
||
@Test | ||
void deserializeExplicitNull() throws Exception | ||
{ | ||
String json = a2q("{'name':'v1','optional':null}"); | ||
RecordWithOptionalParam output = MAPPER.readValue(json, RecordWithOptionalParam.class); | ||
assertEquals("v1", output.name()); | ||
assertNotNull(output.optional()); | ||
assertEquals(Optional.empty(), output.optional()); | ||
} | ||
|
||
@JacksonTestFailureExpected // [databind#5335] | ||
@Test | ||
void deserializeMissing() throws Exception | ||
{ | ||
String json = a2q("{'name':'v1'}"); | ||
RecordWithOptionalParam output = MAPPER.readValue(json, RecordWithOptionalParam.class); | ||
assertEquals("v1", output.name()); | ||
assertNotNull(output.optional()); | ||
assertEquals(Optional.empty(), output.optional()); | ||
} | ||
|
||
@JacksonTestFailureExpected // [databind#5335] | ||
@Test | ||
void deserializeIssue5335Config() throws Exception | ||
{ | ||
String json = a2q("{'name':'v1'}"); | ||
JsonMapper mapper = JsonMapper.builder() | ||
.changeDefaultPropertyInclusion( | ||
v -> JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.ALWAYS)) | ||
.build(); | ||
RecordWithOptionalParam output = mapper.readValue(json, RecordWithOptionalParam.class); | ||
assertEquals("v1", output.name()); | ||
assertNotNull(output.optional()); | ||
assertEquals(Optional.empty(), output.optional()); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.