Skip to content
New issue

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

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

Already on GitHub? # to your account

Improve namespace handling for XML to record mappings #7547

Open
Nuvindu opened this issue Jan 31, 2025 · 0 comments
Open

Improve namespace handling for XML to record mappings #7547

Nuvindu opened this issue Jan 31, 2025 · 0 comments

Comments

@Nuvindu
Copy link
Contributor

Nuvindu commented Jan 31, 2025

Description

When trying to convert an XML value into a Ballerina record using the data.xmldata module, an incorrect behaviour occurs when the record has a namespace that differs from the namespace or namespace prefix included in the provided XML value.

1. XML with a namespace but no prefix in the Ballerina record

XML Value:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
    <s:Body/>
</s:Envelope>

Ballerina record:

@xmldata:Name {value: "Envelope"}
@xmldata:Namespace {uri: "http://www.w3.org/2003/05/soap-envelope"}
public type Response record {
    ResponseBody Body;
};

@xmldata:Namespace {uri: "http://www.w3.org/2003/05/soap-envelope"}
public type ResponseBody record {};

2. Ballerina record has a namespace with a prefix, but the prefix is not matched with the XML

XML Value:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
    <s:Body/>
</s:Envelope>

Ballerina record:

@xmldata:Name {value: "Envelope"}
@xmldata:Namespace {prefix: "soap", uri: "http://www.w3.org/2003/05/soap-envelope"}
public type Response record {
    ResponseBody Body;
};

@xmldata:Namespace {prefix: "soap", uri: "http://www.w3.org/2003/05/soap-envelope"}
public type ResponseBody record {};

Steps to Reproduce

  1. Run the following Ballerina code.
import ballerina/io;
import ballerina/data.xmldata;

public function main() returns error? {
         xml result = xml `<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body/></s:Envelope>`;
         Response response = check xmldata:parseAsType(result);
         io:println(response);
}

@xmldata:Name {value: "Envelope"}
@xmldata:Namespace {uri: "http://www.w3.org/2003/05/soap-envelope"}
public type Response record {
    ResponseBody Body;
};

@xmldata:Namespace {uri: "http://www.w3.org/2003/05/soap-envelope"}
public type ResponseBody record {};
  1. Observe the namespace mismatch error

Version

2201.10.0

Environment Details (with versions)

No response

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant