Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 333166

Summary: MOXy: XmlJavaTypeAdapter not working in collection case when return type of marshal method is Object
Product: z_Archived Reporter: David McCann <david.mccann>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Proposed fix
none
Supporting tests none

Description David McCann CLA 2010-12-23 12:55:44 EST
This issue deals with the case where there is a List in the model class, and the adapter has Object as the return type of the marshal method.  Note that in this case the adapter is applied to each entry in the list as opposed to the list itself.

Given the following model class:

@XmlRootElement
public class Foo {
  @XmlElementRef(type=Bar.class)
  @XmlJavaTypeAdapter(value=MyAdapter.class)
  public List<String> items;
}

And the following adapter:

public final class MyAdapter extends XmlAdapter<Object, String> {
  public MyAdapter() {}
	
  public String unmarshal(Object arg0) throws Exception {
    String id = "";
    if (arg0 instanceof Bar) {
      id = ((Bar)arg0).id;
    }
    return id;
  }

  public Object marshal(String arg0) throws Exception {
    Bar bar = new Bar();
    if (arg0.equals("666")) {
      bar.id = arg0;
    } else if (arg0.equals("777")) {
      bar.id = arg0;
    } else if (arg0.equals("888")) {
      bar.id = arg0;
    }
    return bar;
  }
}

When we try and marshal a Foo we get the following exception:

Exception Description: The object [[666, 777, 888]], of class [class java.util.ArrayList], could not be converted to [class java.lang.Object]
Comment 1 David McCann CLA 2010-12-30 12:37:10 EST
Created attachment 185913 [details]
Proposed fix
Comment 2 David McCann CLA 2010-12-30 12:37:46 EST
Created attachment 185914 [details]
Supporting tests
Comment 3 David McCann CLA 2010-12-30 12:46:47 EST
Reviewed by: matt.macivor@oracle.com
Tests:  all unit tests pass as expected; jaxb/externalizedmetadata/xmladapter/list/XmlAdapterListTestCases; jaxb/xmladapter/list/XmlAdapterListMultipleBarTestCases; jaxb/xmladapter/list/XmlAdapterListSingleBarTestCases
Revision: 8761
Comment 4 David McCann CLA 2010-12-30 12:49:11 EST
Checked in tests cover two use cases for List type with adapter marshal method return type of Object:

1) Adapter applied to each entry in the list
2) Adapter applied to the entire list
Comment 5 Eclipse Webmaster CLA 2022-06-09 10:15:45 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 6 Eclipse Webmaster CLA 2022-06-09 10:29:30 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink