Open kaspersorensen opened 6 years ago
For reference, here's the type of exception that Jackson will report:
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 43] (through reference chain: example.model.ExampleResponse["codes"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1138)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1092)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:63)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:10)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:527)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
Related (or duplicate of): #5187
Description
When a definition in Swagger contains both regular
properties
(with complex value types) andadditionalProperties
with typestring
, then the generated Java model class cannot deserialized by e.g. Jackson since the class extendsHashMap<String,String>
and it's complex values are being "set" as a String.A better way of generating the class would probably be that the model class has a
Map<String,String>
field/member which additional properties are deserialized into.Swagger-codegen version
2.3.1
Swagger declaration file content or url
Example swagger file where the generated Java class extends
HashMap<String,String>
which then breaks deserialization of thecodes
object:Maven config used for generation
Suggest a fix/enhancement
I've worked around this issue in my project by having a customized mustache file without using the
parent
mustache property. I'm not sure what side-effects that may have for other projects though.