Skip to content

Commit 0d54df6

Browse files
authored
Fix #5151: add MissingInjectableValueExcepion (#5156)
1 parent 4191c27 commit 0d54df6

File tree

5 files changed

+58
-15
lines changed

5 files changed

+58
-15
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Project: jackson-databind
1414
from 2.20
1515
#5103: Use `writeStartObject(Object forValue, int size)` for `ObjectNode`
1616
serialization
17+
#5151: Add new exception type, `MissingInjectValueException`, to be used
18+
for failed `@JacksonInject`
1719
- Generate SBOMs [JSTEP-14]
1820
1921
2.19.1 (not yet released)

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
2323
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
2424
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
25+
import com.fasterxml.jackson.databind.exc.MissingInjectableValueExcepion;
2526
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
2627
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
2728
import com.fasterxml.jackson.databind.introspect.Annotated;
@@ -474,7 +475,7 @@ public final Object findInjectableValue(Object valueId,
474475
|| (optional == null && !isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_INJECT_VALUE))) {
475476
return JacksonInject.Value.empty();
476477
}
477-
throw missingInjectValueException(String.format(
478+
throw missingInjectableValueException(String.format(
478479
"No 'injectableValues' configured, cannot inject value with id '%s'", valueId),
479480
valueId, forProperty, beanInstance);
480481
}
@@ -2099,11 +2100,11 @@ public JsonMappingException missingTypeIdException(JavaType baseType,
20992100
/**
21002101
* @since 2.20
21012102
*/
2102-
public JsonMappingException missingInjectValueException(String msg,
2103+
public JsonMappingException missingInjectableValueException(String msg,
21032104
Object valueId,
21042105
BeanProperty forProperty, Object beanInstance) {
2105-
return InvalidDefinitionException.from(_parser, msg,
2106-
constructType(ClassUtil.classOf(beanInstance)));
2106+
return MissingInjectableValueExcepion.from(_parser, msg,
2107+
valueId, forProperty, beanInstance);
21072108
}
21082109

21092110
/*

src/main/java/com/fasterxml/jackson/databind/InjectableValues.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,19 @@ public Object findInjectableValue(DeserializationContext ctxt, Object valueId,
8787
throws JsonMappingException
8888
{
8989
if (!(valueId instanceof String)) {
90-
ctxt.reportBadDefinition(ClassUtil.classOf(valueId),
90+
throw ctxt.missingInjectableValueException(
9191
String.format(
92-
"Unrecognized inject value id type (%s), expecting String",
93-
ClassUtil.classNameOf(valueId)));
92+
"Unsupported injectable value id type (%s), expecting String",
93+
ClassUtil.classNameOf(valueId)),
94+
valueId, forProperty, beanInstance);
9495
}
9596
String key = (String) valueId;
9697
Object ob = _values.get(key);
9798
if (ob == null && !_values.containsKey(key)) {
9899
if (Boolean.FALSE.equals(optional)
99100
|| ((optional == null)
100101
&& ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_INJECT_VALUE))) {
101-
throw ctxt.missingInjectValueException(
102+
throw ctxt.missingInjectableValueException(
102103
String.format("No injectable value with id '%s' found (for property '%s')",
103104
key, forProperty.getName()),
104105
valueId, forProperty, beanInstance);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.databind.exc;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
5+
import com.fasterxml.jackson.databind.BeanProperty;
6+
import com.fasterxml.jackson.databind.JsonMappingException;
7+
8+
/**
9+
* @since 2.20
10+
*/
11+
public class MissingInjectableValueExcepion
12+
extends JsonMappingException
13+
{
14+
private static final long serialVersionUID = 1L;
15+
16+
protected final Object _valueId;
17+
protected final BeanProperty _forProperty;
18+
protected final Object _beanInstance;
19+
20+
protected MissingInjectableValueExcepion(JsonParser p, String msg,
21+
Object valueId, BeanProperty forProperty, Object beanInstance)
22+
{
23+
super(p, msg);
24+
_valueId = valueId;
25+
_forProperty = forProperty;
26+
_beanInstance = beanInstance;
27+
}
28+
29+
public static MissingInjectableValueExcepion from(JsonParser p, String msg,
30+
Object valueId, BeanProperty forProperty, Object beanInstance)
31+
{
32+
return new MissingInjectableValueExcepion(p, msg, valueId, forProperty, beanInstance);
33+
}
34+
35+
public Object getValueId() { return _valueId; }
36+
public BeanProperty getForProperty() { return _forProperty; }
37+
public Object getBeanInstance() { return _beanInstance; }
38+
}

src/test/java/com/fasterxml/jackson/databind/deser/inject/JacksonInject3072Test.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.fasterxml.jackson.databind.*;
99
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
10+
import com.fasterxml.jackson.databind.exc.MissingInjectableValueExcepion;
1011
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
1112

1213
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@@ -66,8 +67,8 @@ void testOptionalFieldNotFound() throws Exception {
6667

6768
@Test
6869
void testMandatoryFieldNotFound() {
69-
InvalidDefinitionException exception = assertThrows(
70-
InvalidDefinitionException.class, () -> READER.readValue("{}"));
70+
MissingInjectableValueExcepion exception = assertThrows(
71+
MissingInjectableValueExcepion.class, () -> READER.readValue("{}"));
7172

7273
assertThat(exception.getMessage())
7374
.startsWith("No 'injectableValues' configured, cannot inject value with id 'id'");
@@ -80,8 +81,8 @@ void testRequiredAnnotatedField() throws Exception {
8081
ObjectReader reader = READER.forType(DtoWithRequired.class)
8182
.without(DeserializationFeature.FAIL_ON_UNKNOWN_INJECT_VALUE);
8283

83-
InvalidDefinitionException exception = assertThrows(
84-
InvalidDefinitionException.class, () -> reader.readValue("{}"));
84+
MissingInjectableValueExcepion exception = assertThrows(
85+
MissingInjectableValueExcepion.class, () -> reader.readValue("{}"));
8586

8687
assertThat(exception.getMessage())
8788
.startsWith("No 'injectableValues' configured, cannot inject value with id 'requiredValue'");
@@ -91,7 +92,7 @@ void testRequiredAnnotatedField() throws Exception {
9192
.addValue("id", "idValue"));
9293

9394
exception = assertThrows(
94-
InvalidDefinitionException.class, () -> reader2.readValue("{}"));
95+
MissingInjectableValueExcepion.class, () -> reader2.readValue("{}"));
9596

9697
assertThat(exception.getMessage())
9798
.startsWith("No injectable value with id 'requiredValue' found (for property 'requiredField')");
@@ -108,8 +109,8 @@ void testMandatoryFieldNotFoundWithInjectableValues() {
108109
ObjectReader reader = READER
109110
.with(new InjectableValues.Std());
110111

111-
InvalidDefinitionException exception = assertThrows(
112-
InvalidDefinitionException.class, () -> reader.readValue("{}"));
112+
MissingInjectableValueExcepion exception = assertThrows(
113+
MissingInjectableValueExcepion.class, () -> reader.readValue("{}"));
113114

114115
assertThat(exception.getMessage())
115116
.startsWith("No injectable value with id 'id' found (for property 'id')");

0 commit comments

Comments
 (0)