Skip to content

JAXB ObjectFactory Mapping with Dash (-) #3928

Open

Description

@Selfmade-RuLeZ

Expected behavior

I have to call some SOAP services through the corresponding WSDL files.
Unfortunately some files have some attributes with dashes. I would expect, that MapStruct can map those fields using the corresponding ObjectFactory class.

In this PR a comment even mentioned to use a regex to match underscores and dashes, but the latter never made it into this PR.

#713

Actual behavior

MapStruct cannot handle the dash inside of the element name. I get a Ambiguous mapping methods found for mapping property error as it cannot find the specific mapping method of the ObjectFactory
Example:
Element:

@XmlElementRef(name = "Foo-Bar", namespace = "mynamespace", type = JAXBElement.class, required = false)
 protected JAXBElement<Integer> fooBar;

ObjectFactory Method:

@XmlElementDecl(namespace = "mynamespace", name = "Foo-Bar", scope = MyClass.class)
    public JAXBElement<String> createMyClassFooBar(String value) {
        return new JAXBElement<>(_MyClassFooBar_QNAME, String.class, MyClass.class, value);
    }

Steps to reproduce the problem

Manipulate the Jaxb integration test to use a dash instead of the camel case parameter. It should generate the field in camel case but the mapping breaks afterwards.

https://github.com/mapstruct/mapstruct/tree/4e1720c2a899252de448c38eb10942f9fbbf5276/integrationtest/src/test/resources/jakartaJaxbTest

MapStruct Version

MapStruct 1.6.3

Activity

  1. Selfmade-RuLeZ commented on Sep 12, 2025

    @Selfmade-RuLeZ
    Author

    I guess I found the problem. Mapstruct is trying to resolve the setter methods but the problem is another one. It's not mainly the dash itself but the generation of those classes lead to this problem.

    Explained:

    <-- Part of the WSDL -->
    
    <element name="XY-Something" type="int" minOccurs="0" nillable="true"/>
       // Generated field from class MyClass.class using cxf:
    
        @XmlElementRef(name = "XY-Something", namespace = "http://mynamespace", type = JAXBElement.class, required = false)
        protected JAXBElement<Integer> xySomething;
    
        public void setXYSomething(JAXBElement<Integer> value) {
            this.xySomething = value;
        }

    I generate my DTO from the named WSDL. So Mapstruct tries to find the member XYSomething as this is also the get method for this property. The processor class XmlElementDeclSelector tries to find the field inside of MyClass otherwise it just takes the target property name which is "XYSomething". In the loop of getMatchingMethods inside of XmlElementDeclSelector the XML declaration name of each property is taken which result in "XY-Something" and thus nameIsSetAndMatches is false as it will be compared with "XYSomething".

    Maybe a solution would be to sanitize the dash in the JakartaXmlElementDeclSelector and JakartaXmlElementDeclSelector classes, but that's for you to decide.

    Edit:

    /*
     * Copyright MapStruct Authors.
     *
     * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
     */
    class JavaxXmlElementDeclSelector extends XmlElementDeclSelector {
    
        JavaxXmlElementDeclSelector(TypeUtils typeUtils) {
            super( typeUtils );
        }
    
        @Override
        XmlElementDeclInfo getXmlElementDeclInfo(Element element) {
            XmlElementDeclGem gem = XmlElementDeclGem.instanceOn( element );
    
            if (gem == null) {
                return null;
            }
    
            return new XmlElementDeclInfo( gem.name().get().replaceAll("-",""), gem.scope().get() );
        }
    
        @Override
        XmlElementRefInfo getXmlElementRefInfo(Element element) {
            XmlElementRefGem gem = XmlElementRefGem.instanceOn( element );
    
            if (gem == null) {
                return null;
            }
    
            return new XmlElementRefInfo( gem.name().get().replaceAll("-",""), gem.type().get() );
        }
    }

    Just a smell modification that solves the problem with passing all unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions