Fix [Issue-13], problems with multiple injected params for @JsonCreator ctor
diff --git a/release-notes/CREDITS b/release-notes/CREDITS
index 93ba7ac..4d75247 100644
--- a/release-notes/CREDITS
+++ b/release-notes/CREDITS
@@ -903,3 +903,9 @@
Jan Jan:
* Reported [JACKSON-832] (partial) Fix numeric range check for Longs (was not working)
[1.9.7]
+
+Stuart Dootson:
+ * Reported [Issue-13] Runtime error passing multiple injected values to a constructor
+ [1.9.7]
+
+
\ No newline at end of file
diff --git a/release-notes/VERSION b/release-notes/VERSION
index fcf0dea..da0ee8c 100644
--- a/release-notes/VERSION
+++ b/release-notes/VERSION
@@ -13,6 +13,8 @@
(reported by Steven S)
* [Issue-11] JsonParser.getValueAsLong() returning int, not long
(reported by Daniel L)
+ * [Issue-13] Runtime error passing multiple injected values to a constructor
+ (reported by Stuart D)
* [Issue-14]: Annotations were not included from parent classes of
mix-in classes
(reported by @guillaup)
diff --git a/src/mapper/java/org/codehaus/jackson/map/deser/impl/CreatorCollector.java b/src/mapper/java/org/codehaus/jackson/map/deser/impl/CreatorCollector.java
index eb0629c..8252d79 100644
--- a/src/mapper/java/org/codehaus/jackson/map/deser/impl/CreatorCollector.java
+++ b/src/mapper/java/org/codehaus/jackson/map/deser/impl/CreatorCollector.java
@@ -109,6 +109,12 @@
HashMap<String,Integer> names = new HashMap<String,Integer>();
for (int i = 0, len = properties.length; i < len; ++i) {
String name = properties[i].getName();
+ /* [Issue-13]: Need to consider Injectables, which may not have
+ * a name at all, and need to be skipped
+ */
+ if (name.length() == 0 && properties[i].getInjectableValueId() != null) {
+ continue;
+ }
Integer old = names.put(name, Integer.valueOf(i));
if (old != null) {
throw new IllegalArgumentException("Duplicate creator property \""+name+"\" (index "+old+" vs "+i+")");
diff --git a/src/mapper/java/org/codehaus/jackson/map/deser/impl/PropertyBasedCreator.java b/src/mapper/java/org/codehaus/jackson/map/deser/impl/PropertyBasedCreator.java
index fbf59cb..63708e7 100644
--- a/src/mapper/java/org/codehaus/jackson/map/deser/impl/PropertyBasedCreator.java
+++ b/src/mapper/java/org/codehaus/jackson/map/deser/impl/PropertyBasedCreator.java
@@ -31,6 +31,12 @@
protected final HashMap<String, SettableBeanProperty> _properties;
/**
+ * Number of properties: usually same as size of {@link #_properties},
+ * but not necessarily, when we have unnamed injectable properties.
+ */
+ protected final int _propertyCount;
+
+ /**
* If some property values must always have a non-null value (like
* primitive types do), this array contains such default values.
*/
@@ -52,7 +58,9 @@
Object[] defValues = null;
SettableBeanProperty[] creatorProps = valueInstantiator.getFromObjectArguments();
SettableBeanProperty[] propertiesWithInjectables = null;
- for (int i = 0, len = creatorProps.length; i < len; ++i) {
+ final int len = creatorProps.length;
+ _propertyCount = len;
+ for (int i = 0; i < len; ++i) {
SettableBeanProperty prop = creatorProps[i];
_properties.put(prop.getName(), prop);
if (prop.getType().isPrimitive()) {
@@ -98,7 +106,7 @@
*/
public PropertyValueBuffer startBuilding(JsonParser jp, DeserializationContext ctxt)
{
- PropertyValueBuffer buffer = new PropertyValueBuffer(jp, ctxt, _properties.size());
+ PropertyValueBuffer buffer = new PropertyValueBuffer(jp, ctxt, _propertyCount);
if (_propertiesWithInjectables != null) {
buffer.inject(_propertiesWithInjectables);
}
diff --git a/src/test/org/codehaus/jackson/map/deser/TestInjectables.java b/src/test/org/codehaus/jackson/map/deser/TestInjectables.java
index 58ce99a..76963cd 100644
--- a/src/test/org/codehaus/jackson/map/deser/TestInjectables.java
+++ b/src/test/org/codehaus/jackson/map/deser/TestInjectables.java
@@ -45,7 +45,18 @@
age = a;
}
}
-
+
+ static class CtorBean2 {
+ protected String name;
+ protected Integer age;
+
+ public CtorBean2(@JacksonInject String n, @JacksonInject("number") Integer a)
+ {
+ name = n;
+ age = a;
+ }
+ }
+
/*
/**********************************************************
/* Unit tests
@@ -77,6 +88,19 @@
assertEquals(55, bean.age);
assertEquals("Bubba", bean.name);
}
+
+ // [Issue-13]
+ public void testTwoInjectablesViaCreator() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.setInjectableValues(new InjectableValues.Std()
+ .addValue(String.class, "Bob")
+ .addValue("number", Integer.valueOf(13))
+ );
+ CtorBean2 bean = mapper.readValue("{ }", CtorBean2.class);
+ assertEquals(Integer.valueOf(13), bean.age);
+ assertEquals("Bob", bean.name);
+ }
public void testInvalidDup() throws Exception
{