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

Like List, initialize the HashMap as empty when class is generated using xsd #517

Closed
jobas2007 opened this issue Mar 5, 2024 · 5 comments · Fixed by #518
Closed

Like List, initialize the HashMap as empty when class is generated using xsd #517

jobas2007 opened this issue Mar 5, 2024 · 5 comments · Fixed by #518
Assignees
Labels
enhancement xjc-plugins Issue concerns basics plugins

Comments

@jobas2007
Copy link

The current xjc plugin initializes the list to empty when getter is called.

Firstly was trying to use HashMap in XSD and finally was able to achieve it, but the xjc plugin is yet to initialize it. More details are in stack overflow links https://stackoverflow.com/questions/78077708/using-hashmap-in-xsd.

Was advised by Laurent (contributor) to open a issue.

Appreciate quick resolution so that can use updated plugin before prod release. Thanks

@laurentschoelens laurentschoelens self-assigned this Mar 5, 2024
@laurentschoelens laurentschoelens added enhancement xjc-plugins Issue concerns basics plugins labels Mar 5, 2024
@laurentschoelens
Copy link
Collaborator

Hi @jobas2007
Thanks for creating the issue as suggested in SO
Could you tell me which version of JAXB is targeted (JAXB 2.3 / 3.x / 4.x) ?
I'll try having a working MRE with details from SO

@mattrpav
Copy link
Collaborator

mattrpav commented Mar 5, 2024

Maps can get really complicated to try to get correct marshal/unmarshal.

A couple key features:

  1. Java supports any object being the key (not all languages do, so interop is problematic)
  2. Map.Entry is a valid public scoped object to marshal/unmarshal
  3. Some map types in various languages support null key and value entries
  4. Differentiation between null entries and empty
  5. Falling back to xs:anyType is not great, since you lose ability to communicate strongly typed maps (which are generally more desirable these days vs Map<String, Object>

The scheme is more like this:

<element name="Map">
  <complexType>
    <sequence>
      <element name="entries" minOccurs="0" maxOccurs="1">
        <complexType>
          <sequence>
            <element name="entry" type="map:entry" minOccurs="0" maxOccurs="unbounded"/>
          </sequence>
        </complexType>
      </element>
    </sequence>
  </complexType>
</element>  

<complexType name="entry">
  <sequence> 
    <element name="key" minOccurs="0" type="anyType" minOccurs="0" maxOccurs="1"/>
    <element name="value" minOccurs="0" type="anyType" minOccurs="0" maxOccurs="1"/>
  </sequence>
</complexType>

However, the above approach makes doing Maps with simple primitive K,V overly complex.

Explosion looks like this

NOTE: To get this actually correct, the Map would need to convert the to be a of all the various combination of <K,V> Map.Entry types, since Maps shouldn't have mismatched typing across all the contained Map.Entry.

<complexType name="entry" abstract="true" />

<complexType name="stringStringEntry">
  <complexContent>
    <extension base="map:entry">
      <sequence> 
        <element name="key" minOccurs="0" type="string" minOccurs="1" maxOccurs="1"/>
        <element name="value" minOccurs="0" type="string" minOccurs="0" maxOccurs="1"/>
      </sequence>
    </extension>
  </complexContent>
</complexType>

<complexType name="stringIntegerEntry">
  <complexContent>
    <extension base="map:entry">
      <sequence> 
        <element name="key" minOccurs="0" type="string" minOccurs="1" maxOccurs="1"/>
        <element name="value" minOccurs="0" type="int" minOccurs="0" maxOccurs="1"/>
      </sequence>
    </extension>
  </complexContent>
</complexType>

<complexType name="stringLongEntry">
  <complexContent>
    <extension base="map:entry">
      <sequence> 
        <element name="key" minOccurs="0" type="string" minOccurs="1" maxOccurs="1"/>
        <element name="value" minOccurs="0" type="long" minOccurs="0" maxOccurs="1"/>
      </sequence>
    </extension>
  </complexContent>
</complexType>

...

<complexType name="objectObjectEntry">
  <complexContent>
    <extension base="map:entry">
      <sequence> 
        <element name="key" minOccurs="0" type="anyType" minOccurs="1" maxOccurs="1"/>
        <element name="value" minOccurs="0" type="anyType" minOccurs="0" maxOccurs="1"/>
      </sequence>
    </extension>
  </complexContent>
</complexType>

laurentschoelens added a commit to laurentschoelens/jaxb-tools that referenced this issue Mar 5, 2024
with possibility of defining impl class and ignored fields
@laurentschoelens
Copy link
Collaborator

@jobas2007 : provided PR #518 for this in v4 branch

laurentschoelens added a commit to laurentschoelens/jaxb-tools that referenced this issue Mar 6, 2024
@jobas2007
Copy link
Author

jobas2007 commented Mar 6, 2024

Hi @jobas2007 Thanks for creating the issue as suggested in SO Could you tell me which version of JAXB is targeted (JAXB 2.3 / 3.x / 4.x) ? I'll try having a working MRE with details from SO

Thanks for looking in this right away.
2.3

@laurentschoelens
Copy link
Collaborator

It'll need backport then to v2.x branch but will be done with only new namespaces.

mattrpav pushed a commit that referenced this issue Apr 9, 2024
with possibility of defining impl class and ignored fields
laurentschoelens added a commit that referenced this issue Apr 11, 2024
with possibility of defining impl class and ignored fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement xjc-plugins Issue concerns basics plugins
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants