12

I am having issues adapting an Optional that contains a Set.

Initially we had a Set within an class that was to be marshalled to XML as such:

@XmlElementWrapper(name = "items")
@XmlElement(name = "item")
public Set<Item> getItems()
{
    return items;
}

We create a schema from our classes during the maven build. However the requirement has changed, and the Set now has to be wrapped in an Optional but we still need the output schema to remain the same.

@XmlElementWrapper(name = "items")
@XmlElement(name = "item")
@XmlJavaTypeAdapter(value = OptionalAdapter.class)
public Optional<Set<Item>> getItem()
{
    return items;
}

After wrapping the Set as such I have created the xml adapter:

public class OptionalAdapter extends XmlAdapter<Set<Item>,Optional<Set<Item>>>
{
    @Override
     public Optional<Set<Item>> unmarshal(final Set<Item> v) throws Exception
     {
        return Optional.ofNullable(v);
     }

     @Override
     public Set<Item> marshal(final Optional<Set<Item>> v) throws Exception
     {
         return v.orElse(null);
     }

 }

After changing the classes, schemagen fails (the maven output is just 'null', as far as I'm aware this is a known issue with the schemagen).

I have tried changing the adapter to use collection and to remove the XmlElementWrapper annotation from the optional but have had no luck. I was wondering if there was anything else that needs to be taken care of in order for this to generate the schema (and marshal/unmarshal) correctly?

Many Thanks

David Brossard
  • 13,584
  • 6
  • 55
  • 88
Gee2113
  • 258
  • 2
  • 11

0 Answers0