Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support XML element name based on runtime type #679

Open
SimonCockx opened this issue Oct 28, 2024 · 1 comment
Open

Support XML element name based on runtime type #679

SimonCockx opened this issue Oct 28, 2024 · 1 comment

Comments

@SimonCockx
Copy link
Contributor

XSD schema's support a way to name an element based on its runtime type through substitution groups. I'm trying to deserialise such a substitution group. Is this supported in some way?

Example. Suppose there is two types of Animals: Cows and Goats. Based on their type, I want their element to be named either cow or goat. This would be modelled as follows:

XSD:

<!-- XML root element -->
<xs:element name="document">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="animal"/> <!-- Note that the name of this element is determined by the actual type of animal -->
    </xs:sequence>
  </xs:complexType>
</xs:element>

<!-- Element names: `Cow`s should be serialised as an element named `cow`, `Goat`s should be serialised as an element named `goat`. -->
<xs:element name="animal" type="Animal" abstract="true" />
<xs:element name="cow" type="Cow" substitutionGroup="animal" />
<xs:element name="goat" type="Goat" substitutionGroup="animal" />

<!-- Types: `Cow` and `Goat` extend from `Animal` -->
<xs:complexType name="Animal">
  <xs:attribute name="name" type="xs:string" />
</xs:complexType>
<xs:complexType name="Cow">
  <xs:complexContent>
    <xs:extension base="Animal" />
  </xs:complexContent>
</xs:complexType>
<xs:complexType name="Goat">
  <xs:complexContent>
    <xs:extension base="Animal" />
  </xs:complexContent>
</xs:complexType>

XML example instances:

<document>
    <cow name="Cow1" />
</document>
<document>
    <goat name="Goat1" />
</document>

Java model:

@XmlRootElement(name="document")
public class Document {
  private Animal animal;

  public Animal getAnimal() {
    return animal;
  }
  public void setAnimal(Animal animal) {
    this.animal = animal;
  }
}

public class Animal {
  @JacksonXmlProperty(isAttribute = true)
  private String name;

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

public class Cow extends Animal {}

public class Goat extends Animal {}

Is there a way of annotating the classes to get the desired behaviour?

@cowtowncoder
Copy link
Member

This is a known "most wanted feature", not specific to XML Schema (as in, supported by JAXB and other xml tools). Unfortunately such "flattening" (of nested elements) is not really supported by Jackson XML module.
It would be great to support it, but problem is that requires kind of structural transformation that is currently difficult to achieve due to the way Type[De]Serializers and regular Json[De]Serializers interact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants