1. XStream
  2. XSTR-457

Don't write 'defined-in' attribute if not needed


    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.2.2
    • Fix Version/s: 1.4.5
    • Component/s: None
    • Labels:


      When trying to serialize pure XML conforming to a strict DTD, no extra attributes should be written.
      The issue below forces me to rename fields:

      public class A {
      private String foo="bar";

      public class B extends A {
      private Integer foo; // could be String aswell, doesn't matter

      Serializing an instance of B will create <a><foo defined-in="a">bar</foo></a>.

      Since B.foo is not conflicting with a.foo, as it's not written or read, the 'defined-in' is unnecessary.

      The fix would be in AbstractReflectionProvider around line 89; perhaps the simplestfix is to change
      the return sig for writeField on line 94 to boolean and return false if the mapper.shouldSerialize* returns false,
      then don't add the field to seenFields on line 98.
      Since the recursion seems to go up, when the field B.foo is not marked in the seenFields map,
      the writing of A.foo won't include that defined-in attribute.

      For now, the workaround I use is to rename B.foo, though since A.foo is private it's not obvious this is a problem
      until the XML is examined.

      (PS: sorry I didn't write a patch - I usually do; this is a simple fix but if you really want a patch I can go through the trouble of checking out the code etc.. )


        • Assignee:
          Jörg Schaible
          Kenney Westerhof
        • Votes:
          0 Vote for this issue
          1 Start watching this issue


          • Created: