Module Causes "Simple Type Constructors" to Fail
If I have a simple type constructor:
public static final class SimpleConstructor {
private final String value;
public SimpleConstructor(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
And I add the module parameter names module, the class will no longer work as Jackson detects that the one parameter, "value" in the above example, has a name and thus it can no longer be constructor from just a string (and presumably any of the other simple types).
Here a JUnit test demonstrating the issue:
public class SimpleConstructorTest {
private static final TypeReference<List<SimpleConstructor>> SIMPLE_CONSTRUCTOR_REF
= new TypeReference<List<SimpleConstructor>>() {};
@Test
public void testSimpleConstructorWithParametersModule() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new ParameterNamesModule());
final List<SimpleConstructor> value = mapper.readValue("[\"value\"]", SIMPLE_CONSTRUCTOR_REF);
// Above line fails with JsonMappingException
assertEquals(1, value.size());
assertEquals("value", value.get(0).getValue());
}
@Test
public void testSimpleConstructorWithoutParametersModule() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
final List<SimpleConstructor> value = mapper.readValue("[\"value\"]",SIMPLE_CONSTRUCTOR_REF);
assertEquals(1, value.size());
assertEquals("value", value.get(0).getValue());
}
public static final class SimpleConstructor {
private final String value;
public SimpleConstructor(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}
Sadly, this issue was mentioned before here but still hasn't been resolved.
@cowtowncoder , how should this be fixed? Should we simply ignore creators with one parameter?
I think this can be resolved by requiring distinction of "explicit name" from annotations vs "implicit name" that comes from parameter names module. But the code that tries to handle this is quite untangled; and I suspect this is also related to that other bug where factory/constructor creator methods get mixed up.
Fix needs to go in jackson-databind I suspect. This module can and should ideally just provide underlying implicit names.
Of course if a work-around is needed, perhaps it would make sense to just ignore single-argument creators -- that should prevent the problem.
Does an issue related to this already exist in jackson-databind? Any rough ETA?
@lpandzic @alexwen I added test similar to one included above, but I can not reproduce the problem against Jackson 2.4.2. I will see if I can reproduce it against earlier versions, as that would suggest a fix went in 2.4.2 or 2.4.1.
Also does not fail against 2.4.0, so I wonder if I made some mistake in adapting the test...
@cowtowncoder are you certain you are testing with -parameters enabled? I downloaded latest trunk (with the failing test), removed the test exclusion through maven, and ran the tests through maven and the test failed. The test is successful if you run without -parameters enabled.
@alexwen Hmhm. I haven't made any changes to the project, so it depends on what pom.xml suggests, although I ran it through Eclipse. It does sound like this is what prevents failure tho.
Most likely, yes. I know when running through IntelliJ, even with maven integration, IntelliJ does not set the flag so I must modify the compiler config separately.
Quick note: now https://github.com/FasterXML/jackson-databind/issues/421 is fixed. It might help with this issue. I don't know what needs to be done to enable parameters, although pom.xml says this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<inherited>true</inherited>
<configuration>
<source>${javac.src.version}</source>
<target>${javac.target.version}</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<optimize>true</optimize>
<compilerArguments>
<Xmaxerrs>10000</Xmaxerrs>
<Xmaxwarns>10000</Xmaxwarns>
<Xlint />
<Werror />
<parameters />
</compilerArguments>
</configuration>
</plugin>
but what that <parameters /> there means I don't know (could be enabling, could be disabling).
Try this
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<testCompilerArgument>-parameters</testCompilerArgument>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
I believe I'm encountering this same problem using Resteasy 3.0.10.Final with the resteasy-jackson2-provider. I'm using Jackson 2.5.1.
Symptom
All tests run successfully in Eclipse. Running tests from Maven fails, whether done within Eclipse or from the command-line. (Line breaks added to output below, marked by backslash.)
testListAll(xxx.xxxxxx.xxx.xxxxxx.rest.PackagesTest) Time elapsed: 0.248 sec <<< FAILURE!
java.lang.AssertionError: Exception encountered: com.fasterxml.jackson.databind.JsonMappingException: \
Can not instantiate value of type java.util.HashMap from \
String value ('de0ede87-26d3-4559-a18e-84326fd20eb6'); \
no single-String constructor/factory method
at [Source: ["de0ede87-26d3-4559-a18e-84326fd20eb6"]; line: 1, column: 2]
at org.junit.Assert.fail(Assert.java:88)
at xxx.xxxxxx.xxx.xxxxxx.rest.RESTJsonTest.extractJsonStuff(RESTJsonTest.java:213)
at xxx.xxxxxx.xxx.xxxxxx.rest.PackagesTest.testListAll(PackagesTest.java:112)
The failing code is
TypeReference<List<HashMap<String,String>>> ref =
new TypeReference<List<HashMap<String,String>>>() {};
List<HashMap<String,String>> extractedResults = extractJsonStuff(response, ref);
(It also failed with the original Map<String,Object> that I used.)
The JSON being parsed is
[{"generatedTimestamp":"2015-02-20 06:54:21.000",
"uuid":"de0ede87-26d3-4559-a18e-84326fd20eb6",
"originalFileName":"RandomFilename.pkm",
"generatedBy":"999999",
"insertionTime":"2015-05-21 12:34:56.000",
"name":"ThingName",
"state":"QUEUED",
"type":"FooBar",
"version":"4.0.0-RELEASE",
"description":"Token package"}]
Diagnosis
Not sure: the code snippet executes just fine in Eclipse, but not in Maven.
Cure
Sure would like one.
@luskwater I am not sure you are facing the same problem here; symptom does not sound like one issue talks about. Instead this looks like a mismatch between JSON String value, and expected Java Map value.
I have the same problem as the original submitter, except that I also use Mode.PROPERTIES for the ParameterNamesModule (you can reproduce with the original test case submitted and including this mode).
My problem is that when I have a simple type that accepts a String (basically a "typed" wrapper for String) that I get an exception: com.fasterxml.jackson.databind.JsonMappingException:
Can not instantiate value of type [simple type, class SimpleConstructorTest$SimpleConstructor] from String value ('value'); no single-String constructor/factory method
There seems to be no way to annotate this type or constructor to indicate it is a simple value type.
I also don't really understand why it would be ambigious... a value looks like:
"<value>"
While an Object would look like:
{"constructorParameterName": "<value>"}
What is the name of the field in json and what is the name of the parameter name in constructor? Can you provide a complete code example which fails?
@hjohn as @lpandzic suggested, a unit test would be needed. But also: please file a new issue for this (with ref to this one); it may be related to the original issue (or might not be), but it is much easier to track release notes and progress when issue life-cycle is clear. That is, re-opening an old issue makes it difficult to reason about version in which various problem has been fixed.
It will be easier to explain behavior with a reproducible test case.