Skip to content
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

Add Records+JsonAnySetter workarounds shared in #3439, as test cases. #4633

Closed
Closed
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 @@ -7,48 +7,80 @@
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import org.junit.jupiter.api.Test;

import java.util.LinkedHashMap;
import java.util.Map;

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

// [databind#562] Allow @JsonAnySetter on Creator constructors
public class RecordCreatorWithAnySetter562Test
extends DatabindTestUtil
public class RecordCreatorWithAnySetter562Test extends DatabindTestUtil
{
record RecordWithAnySetterCtor(int id,
Map<String, Integer> additionalProperties) {
record RecordWithAnySetterCtorParamCanonCtor(int id, Map<String, Integer> additionalProperties) {
@JsonCreator
public RecordWithAnySetterCtor(@JsonProperty("regular") int id,
@JsonAnySetter Map<String, Integer> additionalProperties
) {
public RecordWithAnySetterCtorParamCanonCtor(@JsonProperty("regular") int id,
@JsonAnySetter Map<String, Integer> additionalProperties) {
this.id = id;
this.additionalProperties = additionalProperties;
}
}

/*
/**********************************************************************
/* Test methods, alternate constructors
/**********************************************************************
*/
record RecordWithAnySetterComponentCanonCtor(int id, @JsonAnySetter Map<String, Object> any) {
}

// Workaround mentioned in [databind#3439]
record RecordWorkaroundWithAnySetterMethodWithAltCtor(int id, Map<String, Object> any) {
@JsonCreator
public RecordWorkaroundWithAnySetterMethodWithAltCtor(@JsonProperty("id") int id) {
this(id, new LinkedHashMap<>());
}

@JsonAnySetter
private void updateProperty(String name, Object value) {
any.put(name, value);
}
}

private final ObjectMapper MAPPER = newJsonMapper();

@Test
public void testRecordWithAnySetterCtor() throws Exception
{
// First, only regular property mapped
RecordWithAnySetterCtor result = MAPPER.readValue(a2q("{'regular':13}"),
RecordWithAnySetterCtor.class);
RecordWithAnySetterCtorParamCanonCtor result = MAPPER.readValue(a2q("{'regular':13}"),
RecordWithAnySetterCtorParamCanonCtor.class);
assertEquals(13, result.id);
assertEquals(0, result.additionalProperties.size());

// Then with unknown properties
result = MAPPER.readValue(a2q("{'regular':13, 'unknown':99, 'extra':-1}"),
RecordWithAnySetterCtor.class);
RecordWithAnySetterCtorParamCanonCtor.class);
assertEquals(13, result.id);
assertEquals(Integer.valueOf(99), result.additionalProperties.get("unknown"));
assertEquals(Integer.valueOf(-1), result.additionalProperties.get("extra"));
assertEquals(2, result.additionalProperties.size());
}

@Test
public void testDeserialize_WithAnySetterComponent_WithCanonicalConstructor() throws Exception
{
RecordWithAnySetterComponentCanonCtor value = MAPPER.readValue(
a2q("{'id':123,'any1':'Any 1','any2':'Any 2'}"), RecordWithAnySetterComponentCanonCtor.class);

Map<String, Object> expectedAny = new LinkedHashMap<>();
expectedAny.put("any1", "Any 1");
expectedAny.put("any2", "Any 2");
assertEquals(new RecordWithAnySetterComponentCanonCtor(123, expectedAny), value);
}

@Test
public void testDeserializeWorkaround_WithAnySetterMethod_WithAltConstructor() throws Exception
{
RecordWorkaroundWithAnySetterMethodWithAltCtor value = MAPPER.readValue(
a2q("{'id':123,'any1':'Any 1','any2':'Any 2'}"), RecordWorkaroundWithAnySetterMethodWithAltCtor.class);

Map<String, Object> expectedAny = new LinkedHashMap<>();
expectedAny.put("any1", "Any 1");
expectedAny.put("any2", "Any 2");
assertEquals(new RecordWorkaroundWithAnySetterMethodWithAltCtor(123, expectedAny), value);
}
}