Skip to content

Commit 927d36b

Browse files
committed
Merge branch '2.x' into 3.x
2 parents 7252207 + 0d54df6 commit 927d36b

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
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/tools/jackson/databind/DeserializationContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ public final Object findInjectableValue(Object valueId,
479479
|| (optional == null && !isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_INJECT_VALUE))) {
480480
return JacksonInject.Value.empty();
481481
}
482-
throw missingInjectValueException(String.format(
482+
throw missingInjectableValueException(String.format(
483483
"No 'injectableValues' configured, cannot inject value with id '%s'", valueId),
484484
valueId, forProperty, beanInstance);
485485
}
@@ -2100,11 +2100,11 @@ public DatabindException missingTypeIdException(JavaType baseType,
21002100
return InvalidTypeIdException.from(_parser, _colonConcat(msg, extraDesc), baseType, null);
21012101
}
21022102

2103-
public DatabindException missingInjectValueException(String msg,
2103+
public DatabindException missingInjectableValueException(String msg,
21042104
Object valueId,
21052105
BeanProperty forProperty, Object beanInstance) {
2106-
return InvalidDefinitionException.from(_parser, msg,
2107-
constructType(ClassUtil.classOf(beanInstance)));
2106+
return MissingInjectableValueExcepion.from(_parser, msg,
2107+
valueId, forProperty, beanInstance);
21082108
}
21092109

21102110
/*

src/main/java/tools/jackson/databind/InjectableValues.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,19 @@ public Object findInjectableValue(DeserializationContext ctxt,
8282
BeanProperty forProperty, Object beanInstance, Boolean optional)
8383
{
8484
if (!(valueId instanceof String)) {
85-
ctxt.reportBadDefinition(ClassUtil.classOf(valueId),
85+
throw ctxt.missingInjectableValueException(
8686
String.format(
87-
"Unrecognized inject value id type (%s), expecting String",
88-
ClassUtil.classNameOf(valueId)));
87+
"Unsupported injectable value id type (%s), expecting String",
88+
ClassUtil.classNameOf(valueId)),
89+
valueId, forProperty, beanInstance);
8990
}
9091
String key = (String) valueId;
9192
Object ob = _values.get(key);
9293
if (ob == null && !_values.containsKey(key)) {
9394
if (Boolean.FALSE.equals(optional)
9495
|| ((optional == null)
9596
&& ctxt.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_INJECT_VALUE))) {
96-
throw ctxt.missingInjectValueException(
97+
throw ctxt.missingInjectableValueException(
9798
String.format("No injectable value with id '%s' found (for property '%s')",
9899
key, forProperty.getName()),
99100
valueId, forProperty, beanInstance);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package tools.jackson.databind.exc;
2+
3+
import tools.jackson.core.JsonParser;
4+
5+
import tools.jackson.databind.BeanProperty;
6+
import tools.jackson.databind.DatabindException;
7+
8+
public class MissingInjectableValueExcepion
9+
extends DatabindException
10+
{
11+
private static final long serialVersionUID = 1L;
12+
13+
protected final Object _valueId;
14+
protected final BeanProperty _forProperty;
15+
protected final Object _beanInstance;
16+
17+
protected MissingInjectableValueExcepion(JsonParser p, String msg,
18+
Object valueId, BeanProperty forProperty, Object beanInstance)
19+
{
20+
super(p, msg);
21+
_valueId = valueId;
22+
_forProperty = forProperty;
23+
_beanInstance = beanInstance;
24+
}
25+
26+
public static MissingInjectableValueExcepion from(JsonParser p, String msg,
27+
Object valueId, BeanProperty forProperty, Object beanInstance)
28+
{
29+
return new MissingInjectableValueExcepion(p, msg, valueId, forProperty, beanInstance);
30+
}
31+
32+
public Object getValueId() { return _valueId; }
33+
public BeanProperty getForProperty() { return _forProperty; }
34+
public Object getBeanInstance() { return _beanInstance; }
35+
}

src/test/java/tools/jackson/databind/deser/inject/JacksonInject3072Test.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.fasterxml.jackson.annotation.OptBoolean;
77

88
import tools.jackson.databind.*;
9-
import tools.jackson.databind.exc.InvalidDefinitionException;
9+
import tools.jackson.databind.exc.MissingInjectableValueExcepion;
1010
import tools.jackson.databind.testutil.DatabindTestUtil;
1111

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

6767
@Test
6868
void testMandatoryFieldNotFound() {
69-
InvalidDefinitionException exception = assertThrows(
70-
InvalidDefinitionException.class, () -> READER.readValue("{}"));
69+
MissingInjectableValueExcepion exception = assertThrows(
70+
MissingInjectableValueExcepion.class, () -> READER.readValue("{}"));
7171

7272
assertThat(exception.getMessage())
7373
.startsWith("No 'injectableValues' configured, cannot inject value with id 'id'");
@@ -80,8 +80,8 @@ void testRequiredAnnotatedField() throws Exception {
8080
ObjectReader reader = READER.forType(DtoWithRequired.class)
8181
.without(DeserializationFeature.FAIL_ON_UNKNOWN_INJECT_VALUE);
8282

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

8686
assertThat(exception.getMessage())
8787
.startsWith("No 'injectableValues' configured, cannot inject value with id 'requiredValue'");
@@ -91,7 +91,7 @@ void testRequiredAnnotatedField() throws Exception {
9191
.addValue("id", "idValue"));
9292

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

9696
assertThat(exception.getMessage())
9797
.startsWith("No injectable value with id 'requiredValue' found (for property 'requiredField')");
@@ -108,8 +108,8 @@ void testMandatoryFieldNotFoundWithInjectableValues() {
108108
ObjectReader reader = READER
109109
.with(new InjectableValues.Std());
110110

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

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

0 commit comments

Comments
 (0)