public static UnrecognizedPropertyException from( JsonParser jsonParser, Object obj, String str, Collection<Object> collection) { if (obj == null) { throw new IllegalArgumentException(); } Class cls; if (obj instanceof Class) { cls = (Class) obj; } else { cls = obj.getClass(); } UnrecognizedPropertyException unrecognizedPropertyException = new UnrecognizedPropertyException( "Unrecognized field \"" + str + "\" (class " + cls.getName() + "), not marked as ignorable", jsonParser.getCurrentLocation(), cls, str, collection); unrecognizedPropertyException.prependPath(obj, str); return unrecognizedPropertyException; }
@Test public void testShouldFailForStrictObjectMapperOnAdditionalFields() throws IOException { DynamicClient client = strictDynamicClientWithApiKey(); RequestExecutor requestExecutor = (RequestExecutor) Whitebox.getInternalState(client, "requestExecutor"); ObjectMapper objectMapper = (ObjectMapper) Whitebox.getInternalState(requestExecutor, "objectMapper"); try { objectMapper.readValue("{\"id\":\"x\", \"name\":\"Foo\"}", DummyMethod.class); fail(); } catch (UnrecognizedPropertyException e) { assertThat(e.getPropertyName(), is("name")); } }
/** * Helper method for reporting a problem with unhandled unknown exception * * @param instanceOrClass Either value being populated (if one has been instantiated), or Class * that indicates type that would be (or have been) instantiated * @param deser Deserializer that had the problem, if called by deserializer (or on behalf of one) */ public void reportUnknownProperty( Object instanceOrClass, String fieldName, JsonDeserializer<?> deser) throws JsonMappingException { if (!isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) { return; } // Do we know properties that are expected instead? Collection<Object> propIds = (deser == null) ? null : deser.getKnownPropertyNames(); throw UnrecognizedPropertyException.from(_parser, instanceOrClass, fieldName, propIds); }