Skip to content

Commit

Permalink
Fix #2713 (wording of UnrecognizedPropertyException)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 7, 2020
1 parent f36a67e commit 69ff0d0
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 31 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ Versions: 3.x (for earlier see VERSION-2.x)
#2176: Add `JsonMapper.shared()` static method
#2539: Add `Deserializers.hasDeserializerFor()` (and something for `DeserializerFactory`)
to allow detection of explicitly supported types
#2713: Change wording of `UnrecognizedPropertyException` to refer to "property" not "field"
- Remove `MappingJsonFactory`
- Add context parameter for `TypeSerializer` contextualization (`forProperty()`)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public static UnrecognizedPropertyException from(JsonParser p,
} else {
ref = fromObjectOrClass.getClass();
}
String msg = String.format("Unrecognized field \"%s\" (class %s), not marked as ignorable",
// 06-May-2020, tatu: 2.x said "Unrecognized field" but we call them "properties"
// everywhere else so...
String msg = String.format("Unrecognized property \"%s\" (class %s), not marked as ignorable",
propertyName, ref.getName());
UnrecognizedPropertyException e = new UnrecognizedPropertyException(p, msg,
p.getCurrentLocation(), ref, propertyName, propertyIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public void testAnySetterDisable() throws Exception
MapImitatorDisabled.class);
fail("Should not pass");
} catch (JsonMappingException e) {
verifyException(e, "Unrecognized field \"value\"");
verifyException(e, "Unrecognized property \"value\"");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

/**
* Unit tests for verifying that feature requested
* via [JACKSON-88] ("setterless collections") work as
* Unit tests for verifying that ("setterless collections") work as
* expected, similar to how Collections and Maps work
* with JAXB.
*/
Expand Down Expand Up @@ -74,10 +74,10 @@ public void testSimpleSetterlessCollectionFailure() throws Exception
m.readValue
("{\"values\":[ \"abc\", \"def\" ]}", CollectionBean.class);
fail("Expected an exception");
} catch (JsonMappingException e) {
} catch (UnrecognizedPropertyException e) {
// Not a good exception, ideally could suggest a need for
// a setter...?
verifyException(e, "Unrecognized field");
verifyException(e, "Unrecognized property");
}
}

Expand All @@ -104,8 +104,8 @@ public void testSimpleSetterlessMapFailure() throws Exception
m.readValue
("{\"values\":{ \"a\":3 }}", MapBean.class);
fail("Expected an exception");
} catch (JsonMappingException e) {
verifyException(e, "Unrecognized field");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized property");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testUnknownProperty() throws Exception
MAPPER.readValue(json, ValueClassXY.class);
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "unrecognized field");
verifyException(e, "Unrecognized property ");
}
// but pass if ok to ignore
ValueClassXY result = MAPPER.readerFor(ValueClassXY.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void testExtraFields() throws Exception
MAPPER.readValue(json, ValueClassXY.class);
fail("Missing expected UnrecognizedPropertyException exception");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized field \"z\"");
verifyException(e, "Unrecognized property \"z\"");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void testSimpleWithIgnores() throws Exception
fail("Should not pass");
} catch (UnrecognizedPropertyException e) {
assertEquals("z", e.getPropertyName());
verifyException(e, "Unrecognized field \"z\"");
verifyException(e, "Unrecognized property \"z\"");
}

// but with config overrides should pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

/**
* Unit tests for checking handling of unknown properties
Expand Down Expand Up @@ -135,8 +136,8 @@ public void testUnknownHandlingDefault() throws Exception
{
try {
MAPPER.readValue(new StringReader(JSON_UNKNOWN_FIELD), TestBean.class);
} catch (JsonMappingException jex) {
verifyException(jex, "Unrecognized field \"foo\"");
} catch (UnrecognizedPropertyException jex) {
verifyException(jex, "Unrecognized property \"foo\"");
}
}

Expand Down Expand Up @@ -242,8 +243,8 @@ public void testClassWithUnknownAndIgnore() throws Exception
// but "d" is not defined, so should still error
try {
MAPPER.readValue("{\"a\":1,\"b\":2,\"c\":3,\"d\":4 }", ImplicitIgnores.class);
} catch (JsonMappingException e) {
verifyException(e, "Unrecognized field \"d\"");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized property \"d\"");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testVoidBeanDeserialization() throws Exception {
result = NO_VOID_MAPPER.readValue(DOC, VoidBean.class);
fail("Should not pass");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized field \"value\"");
verifyException(e, "Unrecognized property \"value\"");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testFinalFieldIgnoral() throws Exception
/*p =*/ mapper.readValue("{\"x\":2}", FixedPoint.class);
fail("Should not try to use final field");
} catch (JsonMappingException e) {
verifyException(e, "unrecognized field \"x\"");
verifyException(e, "Unrecognized property \"x\"");
}
}

Expand All @@ -58,7 +58,7 @@ public void testDeserializationInference() throws Exception
p = mapper.readValue(JSON, Point.class);
fail("Should not succeeed");
} catch (JsonMappingException e) {
verifyException(e, "unrecognized field \"x\"");
verifyException(e, "Unrecognized property \"x\"");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

public class CaseInsensitiveDeserTest extends BaseMapTest
{
Expand Down Expand Up @@ -116,8 +117,8 @@ public void testCaseInsensitiveDeserialization() throws Exception
mapper.readValue(JSON, Issue476Bean.class);

fail("Should not accept improper case properties by default");
} catch (JsonProcessingException e) {
verifyException(e, "Unrecognized field");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized property");
assertValidLocation(e.getLocation());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.module.SimpleSerializers;
Expand Down Expand Up @@ -170,7 +171,7 @@ public void setupModule(SetupContext context) {
* Basic test to ensure we do not have functioning default
* serializers for custom types used in tests.
*/
public void testWithoutModule()
public void testWithoutModule() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
// first: serialization failure:
Expand All @@ -185,9 +186,9 @@ public void testWithoutModule()
try {
mapper.readValue("{\"str\":\"ab\",\"num\":2}", CustomBean.class);
fail("Should have caused an exception");
} catch (IOException e) {
} catch (UnrecognizedPropertyException e) {
// 20-Sep-2017, tatu: Jackson 2.x had different exception; 3.x finds implicits too
verifyException(e, "Unrecognized field \"str\"");
verifyException(e, "Unrecognized property \"str\"");

/*
verifyException(e, "Cannot construct");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.seq;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

/**
* Tests to verify aspects of error recover for reading using
Expand Down Expand Up @@ -34,8 +35,8 @@ public void testRootBeans() throws Exception
try {
bean = it.nextValue();
fail("Should not have succeeded");
} catch (JsonMappingException e) {
verifyException(e, "Unrecognized field \"x\"");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized property \"x\"");
}
// 21-May-2015, tatu: With [databind#734], recovery, we now know there's no more data!
assertFalse(it.hasNextValue());
Expand All @@ -59,8 +60,8 @@ public void testSimpleRootRecovery() throws Exception
// second one problematic
try {
it.nextValue();
} catch (JsonMappingException e) {
verifyException(e, "Unrecognized field \"foo\"");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized property \"foo\"");
}

// but should recover nicely
Expand Down Expand Up @@ -88,8 +89,8 @@ public void testSimpleArrayRecovery() throws Exception
// second one problematic
try {
it.nextValue();
} catch (JsonMappingException e) {
verifyException(e, "Unrecognized field \"foo\"");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized property \"foo\"");
}

// but should recover nicely
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

public class TestUnwrappedWithUnknown650 extends BaseMapTest
{
Expand All @@ -24,8 +25,8 @@ public void testFailOnUnknownPropertyUnwrapped() throws Exception
try {
MAPPER.readValue(aposToQuotes(JSON), A.class);
fail("Exception was not thrown on unknown property");
} catch (JsonMappingException e) {
verifyException(e, "Unrecognized field");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized property");
}
}
}

0 comments on commit 69ff0d0

Please sign in to comment.