#Converting xml back from user defined Record with `toXml` fails with `InherentTypeViolation`

1 messages · Page 1 of 1 (latest)

upbeat orchid
#

Hey Ballerina team, I'm trying to convert some of my fields back to XML.

@test:Config {}
function testSmallGdc() returns error? {
    xml readXml = check io:fileReadXml("./tests/gdc_small.xml");
    gdc_xml:GdCXml data = check xmldata:parseAsType(readXml);
    var foo = data.'payloadAny;
    if foo == () {
        return;
    }
    foreach var item in foo {
        io:println("item: " + item.toString());
        json jsonItem = check jsondata:toJson(item);
        io:println("jsonItem: " + jsonItem.toString());
        xml xmlItem = check xmldata:toXml(item);
        io:println("xmlItem: " + xmlItem);
    }
}

output:

Running Tests
        gdc
item: {"version":"token","type":"token","partnerGetRequest":{"partnerGetRequest":{"content":{"partner":5201282968,"listOption":2}}}}
jsonItem: {"version":"token","type":"token","partnerGetRequest":{"partnerGetRequest":{"content":{"partner":5201282968,"listOption":2}}}}
   error("{ballerina/lang.array}InherentTypeViolation",message="incompatible types: expected 'json', found 'map<anydata>'")
                                callableName: entries moduleName: ballerina.lang.map.0 fileName: map.bal lineNumber: 97
                                callableName: getNamespacesMap moduleName: ballerina.data.xmldata.0 fileName: xml_api.bal lineNumber: 403
                                callableName: fromJson moduleName: ballerina.data.xmldata.0 fileName: xml_api.bal lineNumber: 186
                                callableName: toXml moduleName: ballerina.data.xmldata.0 fileName: xml_api.bal lineNumber: 138
                                callableName: testSmallGdc moduleName: dev.gdc$test.0.tests.lib_test fileName: tests/lib_test.bal lineNumber: 38
                                callableName: testSmallGdc$lambda2$ moduleName: dev.gdc$test.0.tests.test_execute-generated_1 fileName: tests/test_execute-generated_1.bal lineNumber: 6

I don't see how there should be an issue converting this back to XML.
Thanks for the help

small tree
#

@upbeat orchid based on the error message, I created a following sample to reproduce the error. Where individual item within the for loop belongs to type Item. It worked as expected for me.

import ballerina/data.xmldata;
import ballerina/io;

type Item record {|
    string version;
    string 'type;
    record {|
        PartnerGetRequest1 partnerGetRequest;
    |} partnerGetRequest;
|};

type PartnerGetRequest1 record {|
    record {|
        int partner;
        int listOption;
    |} content;
|};

public function main() returns error? {
    Item item = {
        "version": "token",
        "type": "token",
        "partnerGetRequest": {
            "partnerGetRequest": {
                "content": {
                    "partner": 5201282968,
                    "listOption": 2
                }
            }
        }
    };

    xml val = check xmldata:toXml(item);
    io:println(val);
}

Can you share a the expected type and sample here?

upbeat orchid
#

yes here a better example ```bal
import ballerina/io;
import ballerina/data.xmldata;

public type PayloadAnyType record {
@xmldata:Attribute
string 'version?;
string 'type?;
};

public function main() returns error? {
io:println("Hello, World!");

xml readXml = check io:fileReadXml("./data.xml");
io:println(readXml);

PayloadAnyType data = check xmldata:parseAsType(readXml);

xml xmlItem = check xmldata:toXml(data);
io:println("xmlItem: " + xmlItem);

}```

#
 <payloadAny version="token" type="token">
    <partnerGetRequest>
        <partnerGetRequest>
            <content>
                <partner>5201282968</partner>
                <listOption>2</listOption>
            </content>
        </partnerGetRequest>
    </partnerGetRequest>
</payloadAny>```
small tree
upbeat orchid
#

Sure 👍