XStream
  1. XStream
  2. XSTR-637

Aliases of inherited classes are not resolved correctly

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Not A Bug
    • Affects Version/s: 1.3.1
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • JDK version and platform:
      Oracle/Sun 1.6.0_22

      Description

      Hi there,
      I stumbled over the following issue:

      I do use
      xStream.aliasType("jobdef", JobdefinitionData.class);
      xStream.aliasType("jobdefUpdateAtt", UpdateAttributesJobdefinitionData.class);
      xStream.aliasType("jobdefUpdateDoc", UpdateDocumentJobdefinitionData.class);

      with
      public class JobdefinitionData

      { ... }

      public class UpdateAttributesJobdefinitionData extends JobdefinitionData

      { private String description; ... }

      public class UpdateDocumentJobdefinitionData extends UpdateAttributesJobdefinitionData

      { private String documentId; ... }

      If I do serialize an UpdateDocumentJobdefinitionData the alias used is randomly one of the three declared aliases.
      As I understand the code, the problem occurs from com.thoughtworks.xstream.mapper.ClassAliasingMapper

      public String serializedClass(Class type) {
      String alias = (String) classToName.get(type.getName());
      if (alias != null)

      { return alias; } else {
      for (final Iterator iter = typeToName.keySet().iterator(); iter.hasNext() {
      final Class compatibleType = (Class)iter.next();
      if (compatibleType.isAssignableFrom(type)) { return (String)typeToName.get(compatibleType); }
      }
      return super.serializedClass(type);
      }
      }

      The above is the original code. As I understand it, the first lookup checks for the aliasing of simple types like String etc. In my case, the alias returned is null. Then the else part is computed which iterates over the typeToName mapping while checking isAssignableFrom. This causes the problem as the result depends on the order of the elements within the map.


      I think there should be a simple call to typeToName first:

      public String serializedClass(Class type) {
      String alias = (String) classToName.get(type.getName());
      if (alias != null) { return alias; }

      else {
      alias = typeToName.get(type);
      if (alias != null)

      { return alias; }

      for (final Iterator iter = typeToName.keySet().iterator(); iter.hasNext() {
      final Class compatibleType = (Class)iter.next();
      if (compatibleType.isAssignableFrom(type))

      { return (String)typeToName.get(compatibleType); }

      }
      return super.serializedClass(type);
      }
      }

      If the alias was chosen incorrectly, the unmarshalling fails as additional fields are not recognized as members of the class.

      I hope that I didn't misunderstood something with the handling of the aliases.

      Cheers,
      Camilla

        People

        • Assignee:
          Jörg Schaible
          Reporter:
          Camilla Wolf
        • Votes:
          0 Vote for this issue
          Watchers:
          1 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: