Skip to content

Commit

Permalink
Merge branch '2.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 9, 2025
2 parents ae9615e + 0604fab commit ea201ff
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class BuilderInfiniteLoop1979Test
static class Builder
{
private SubBean temp;
private int id;
private Integer id;

Builder(@JsonProperty("beanId") int beanId) {
Builder(@JsonProperty("beanId") Integer beanId) {
this.id = beanId;
}

Expand All @@ -39,10 +39,10 @@ public Bean build()
@JsonDeserialize(builder = Builder.class)
static class Bean
{
int id;
Integer id;
SubBean thing;

public Bean(int id) {
public Bean(Integer id) {
this.id = id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tools.jackson.databind.testutil.DatabindTestUtil;

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

public class BuilderWithViewTest
extends DatabindTestUtil
Expand Down Expand Up @@ -52,9 +53,9 @@ public ValueClassXY build() {
@JsonDeserialize(builder=CreatorBuilderXY.class)
static class CreatorValueXY
{
final int _x, _y;
final Integer _x, _y;

protected CreatorValueXY(int x, int y) {
protected CreatorValueXY(Integer x, Integer y) {
_x = x;
_y = y;
}
Expand All @@ -63,11 +64,11 @@ protected CreatorValueXY(int x, int y) {
@JsonIgnoreProperties({ "bogus" })
static class CreatorBuilderXY
{
public int x, y;
public Integer x, y;

@JsonCreator
public CreatorBuilderXY(@JsonProperty("x") @JsonView(ViewX.class) int x,
@JsonProperty("y") @JsonView(ViewY.class) int y)
public CreatorBuilderXY(@JsonProperty("x") @JsonView(ViewX.class) Integer x,
@JsonProperty("y") @JsonView(ViewY.class) Integer y)
{
this.x = x;
this.y = y;
Expand Down Expand Up @@ -113,12 +114,12 @@ public void testCreatorViews() throws Exception
.withView(ViewX.class)
.readValue(json);
assertEquals(5, resultX._x);
assertEquals(0, resultX._y);
assertNull(resultX._y);

CreatorValueXY resultY = MAPPER.readerFor(CreatorValueXY.class)
.withView(ViewY.class)
.readValue(json);
assertEquals(0, resultY._x);
assertNull(resultY._x);
assertEquals(10, resultY._y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public Biggie(
}
}

private final ObjectReader BIGGIE_READER = sharedMapper().readerFor(Biggie.class);
private final ObjectReader BIGGIE_READER = sharedMapper()
.readerFor(Biggie.class)
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);

@Test
public void testBigPartial() throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ public Person(@JsonProperty(value="name") String name,
}
}

private final ObjectReader POINT_READER = sharedMapper().readerFor(Person.class);
private final ObjectReader PERSON_READER = sharedMapper().readerFor(Person.class);

@Test
public void testRequiredNonNullParam() throws Exception
{
Person p;
// First: fine if feature is not enabled
p = POINT_READER.readValue(a2q("{}"));
p = PERSON_READER.readValue(a2q("{}"));
assertEquals(null, p.name);
assertEquals(Integer.valueOf(0), p.age);

// Second: fine if feature is enabled but default value is not null
ObjectReader r = POINT_READER.with(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES);
ObjectReader r = PERSON_READER.with(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES);
p = r.readValue(a2q("{'name':'John', 'age': null}"));
assertEquals("John", p.name);
assertEquals(Integer.valueOf(0), p.age);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
import tools.jackson.databind.exc.MismatchedInputException;

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

import static tools.jackson.databind.testutil.DatabindTestUtil.*;

public class RequiredCreatorTest
{
static class FascistPoint {
int x, y;
Integer x, y;

@JsonCreator
public FascistPoint(@JsonProperty(value="x", required=true) int x,
@JsonProperty(value="y", required=false) int y)
public FascistPoint(@JsonProperty(value="x", required=true) Integer x,
@JsonProperty(value="y", required=false) Integer y)
{
this.x = x;
this.y = y;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void testRequiredAnnotatedParam() throws Exception
// also fine if 'y' is MIA
p = POINT_READER.readValue(a2q("{'x':3}"));
assertEquals(3, p.x);
assertEquals(0, p.y);
assertNull(p.y);

// but not so good if 'x' missing
try {
Expand All @@ -100,7 +101,7 @@ public void testRequiredGloballyParam() throws Exception
// as per above, ok to miss 'y' with default settings:
p = POINT_READER.readValue(a2q("{'x':2}"));
assertEquals(2, p.x);
assertEquals(0, p.y);
assertNull(p.y);

// but not if global checks desired
ObjectReader r = POINT_READER.with(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
import tools.jackson.databind.exc.InvalidDefinitionException;
import tools.jackson.databind.exc.MismatchedInputException;
import tools.jackson.databind.exc.ValueInstantiationException;
import tools.jackson.databind.testutil.DatabindTestUtil;

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

import static tools.jackson.databind.testutil.DatabindTestUtil.q;
import static tools.jackson.databind.testutil.DatabindTestUtil.verifyException;

public class TestCreators2
extends DatabindTestUtil
{
static class HashTest
{
Expand Down Expand Up @@ -264,7 +263,10 @@ public void testSimpleConstructor() throws Exception
@Test
public void testMissingPrimitives() throws Exception
{
Primitives p = MAPPER.readValue("{}", Primitives.class);
Primitives p = MAPPER
.readerFor(Primitives.class)
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.readValue("{}");
assertFalse(p.b);
assertEquals(0, p.x);
assertEquals(0.0, p.d);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,31 +270,36 @@ public void testNullsAsEmptyWithArrays() throws Exception
public void testNullsAsEmptyWithPrimitiveArrays() throws Exception
{
final String JSON = a2q("{'values':[null]}");
ObjectReader r = MAPPER.reader()
.without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);

// int[]
{
NullContentAsEmpty<int[]> result = MAPPER.readValue(JSON,
new TypeReference<NullContentAsEmpty<int[]>>() { });
NullContentAsEmpty<int[]> result = r.forType
(new TypeReference<NullContentAsEmpty<int[]>>() { })
.readValue(JSON);
assertEquals(1, result.values.length);
assertEquals(0, result.values[0]);
}

// long[]
{
NullContentAsEmpty<long[]> result = MAPPER.readValue(JSON,
new TypeReference<NullContentAsEmpty<long[]>>() { });
NullContentAsEmpty<long[]> result = r.forType
(new TypeReference<NullContentAsEmpty<long[]>>() { })
.readValue(JSON);
assertEquals(1, result.values.length);
assertEquals(0L, result.values[0]);
}

// boolean[]
{
NullContentAsEmpty<boolean[]> result = MAPPER.readValue(JSON,
new TypeReference<NullContentAsEmpty<boolean[]>>() { });
NullContentAsEmpty<boolean[]> result = r.forType
(new TypeReference<NullContentAsEmpty<boolean[]>>() { })
.readValue(JSON);
assertEquals(1, result.values.length);
assertFalse(result.values[0]);
}
}
}

@Test
public void testNullsAsEmptyWithMaps() throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,18 @@ public void testTextualNullAsNumber() throws Exception
assertNull(MAPPER.readValue(NULL_JSON, Float.class));
assertNull(MAPPER.readValue(NULL_JSON, Double.class));

assertEquals(Byte.valueOf((byte) 0), MAPPER.readValue(NULL_JSON, Byte.TYPE));
assertEquals(Short.valueOf((short) 0), MAPPER.readValue(NULL_JSON, Short.TYPE));
ObjectMapper nullOksMapper = jsonMapperBuilder()
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.build();

assertEquals(Byte.valueOf((byte) 0), nullOksMapper.readValue(NULL_JSON, Byte.TYPE));
assertEquals(Short.valueOf((short) 0), nullOksMapper.readValue(NULL_JSON, Short.TYPE));
// Character is bit special, can't do:
// assertEquals(Character.valueOf((char) 0), MAPPER.readValue(JSON, Character.TYPE));
assertEquals(Integer.valueOf(0), MAPPER.readValue(NULL_JSON, Integer.TYPE));
assertEquals(Long.valueOf(0L), MAPPER.readValue(NULL_JSON, Long.TYPE));
assertEquals(Float.valueOf(0f), MAPPER.readValue(NULL_JSON, Float.TYPE));
assertEquals(Double.valueOf(0d), MAPPER.readValue(NULL_JSON, Double.TYPE));
// assertEquals(Character.valueOf((char) 0), nullOksMapper.readValue(JSON, Character.TYPE));
assertEquals(Integer.valueOf(0), nullOksMapper.readValue(NULL_JSON, Integer.TYPE));
assertEquals(Long.valueOf(0L), nullOksMapper.readValue(NULL_JSON, Long.TYPE));
assertEquals(Float.valueOf(0f), nullOksMapper.readValue(NULL_JSON, Float.TYPE));
assertEquals(Double.valueOf(0d), nullOksMapper.readValue(NULL_JSON, Double.TYPE));

assertNull(MAPPER.readValue(NULL_JSON, BigInteger.class));
assertNull(MAPPER.readValue(NULL_JSON, BigDecimal.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*/
public class JDKScalarsDeserTest
{
private final ObjectMapper MAPPER = newJsonMapper();

private final static String NAN_STRING = "NaN";

final static class BooleanBean {
Expand Down Expand Up @@ -132,6 +130,11 @@ static class VoidBean {
public Void value;
}

// [databind#4858] Changes defaults for 3.0 so ensure configs work for 2.x and 3.x
private final ObjectMapper MAPPER = jsonMapperBuilder()
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.build();

/*
/**********************************************************
/* Scalar tests for boolean
Expand Down

0 comments on commit ea201ff

Please sign in to comment.