MethodProperty only checks for generic type in getters, not setters

Issue #322 resolved
Jason Sachs created an issue

https://bitbucket.org/asomov/snakeyaml/src/e18bb04c65e5a93f4f72b3c81142d0afb615549f/src/main/java/org/yaml/snakeyaml/introspector/MethodProperty.java?at=default&fileviewer=file-view-default

public MethodProperty(PropertyDescriptor property) {
    super(property.getName(), property.getPropertyType(),
            property.getReadMethod() == null ? null : property.getReadMethod()
                    .getGenericReturnType());
    this.property = property;
    this.readable = property.getReadMethod() != null;
    this.writable = property.getWriteMethod() != null;
}

The problem occurs in line 39:

property.getReadMethod() == null ? null : property.getReadMethod()
                    .getGenericReturnType()

The call to getGenericReturnType() is used if there's a read method. But if there's no read method, only a write method, we use the value null here, and lose the generic type information and incorrectly construct a List<> (maybe a Map<>, not sure; this happened to me) with the wrong type and not decode regular YAML mappingnodes as custom objects during YAML construction from a file.

It should be something like (not 100% sure of this)

property.getReadMethod() == null 
      ? getWriteMethodGenericType(property)
      : property.getReadMethod().getGenericReturnType()

with

Type getWriteMethodGenericType(PropertyDescriptor)
{ 
    return property.getWriteMethod().getGenericParameterTypes().get(0); // ?? not sure
}

Comments (6)

  1. Log in to comment