Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…bind into fix-4388
  • Loading branch information
JooHyukKim committed Mar 3, 2024
2 parents c243f73 + f2432b0 commit f1fbff3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ enum MixinOverloadedDefault {
/* Test methods
/**********************************************************
*/
private final ObjectMapper MAPPER = new ObjectMapper();

private final ObjectMapper MAPPER = newJsonMapper();

@Test
public void testWithoutCustomFeatures() throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ public void setCountryCode(String countryCode) {
public void setDescription(String description) {
this.description = description;
}


}

public static class EncryptedCreditCardDetails implements PaymentDetails {
Expand All @@ -100,7 +98,6 @@ public void setPaymentInstrumentID(UUID paymentInstrumentID) {
public void setName (String name) {
this.name = name;
}

}

public enum FormOfPayment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

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

// [databind#2588] / [databind#2610]
// [databind#2588] / [databind#2610] / [databind#4354]
public class ExternalTypeId2588Test extends DatabindTestUtil
{
// [databind#2588]
interface Animal { }

static class Cat implements Animal { }
static class Cat implements Animal {
public int lives = 9;
}

public static class Dog implements Animal { }

static class Wolf implements Animal {
public boolean alive;
}

@JsonIgnoreProperties(ignoreUnknown = true)
static class Pet {
final String type;
Expand Down Expand Up @@ -51,6 +58,8 @@ public String idFromValueAndType(Object value, Class<?> suggestedType) {
return "cat";
} else if (suggestedType.isAssignableFrom(Dog.class)) {
return "dog";
} else if (suggestedType.isAssignableFrom(Wolf.class)) {
return "wolf";
}
return null;
}
Expand All @@ -71,16 +80,17 @@ public JsonTypeInfo.Id getMechanism() {
}
}

private final ObjectMapper MAPPER = newJsonMapper();

// [databind#2588]
@Test
public void testExternalTypeId2588() throws Exception
public void testExternalTypeId2588Read() throws Exception
{
final ObjectMapper mapper = newJsonMapper();
Pet pet;

// works?

pet = mapper.readValue(a2q(
pet = MAPPER.readValue(a2q(
"{\n" +
" 'type': 'cat',\n" +
" 'animal': { },\n" +
Expand All @@ -92,7 +102,7 @@ public void testExternalTypeId2588() throws Exception
assertNotNull(pet);

// fails:
pet = mapper.readValue(a2q(
pet = MAPPER.readValue(a2q(
"{\n" +
" 'animal\": { },\n" +
" 'ignoredObject': {\n" +
Expand All @@ -103,4 +113,11 @@ public void testExternalTypeId2588() throws Exception
), Pet.class);
assertNotNull(pet);
}

@Test
public void testExternalTypeId2588Write() throws Exception
{
String json = MAPPER.writeValueAsString(new Pet("cat", new Wolf()));
assertEquals(a2q("{'animal':{'alive':false},'type':'wolf'}"), json);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.fasterxml.jackson.failing;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.newJsonMapper;
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.q;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class EnumDefaultRead4403Test
{
// [databind#4403]
enum Brand4403 {
@JsonProperty("005")
SEAT,
@JsonProperty("006")
HYUNDAI,
@JsonEnumDefaultValue
OTHER
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();

// [databind#4403]
@Test
public void readFromDefault4403() throws Exception
{
ObjectReader r = MAPPER.readerFor(Brand4403.class)
.with(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
assertEquals(Brand4403.SEAT, r.readValue(q("005")));
assertEquals(Brand4403.HYUNDAI, r.readValue(q("006")));
assertEquals(Brand4403.OTHER, r.readValue(q("x")));

// Problem here: "001" taken as "Stringified" index 1
assertEquals(Brand4403.OTHER, r.readValue(q("001")));
}

}

0 comments on commit f1fbff3

Please sign in to comment.