-
Notifications
You must be signed in to change notification settings - Fork 58
-
Notifications
You must be signed in to change notification settings - Fork 58
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
Proposal: Introduce API to convert the Ballerina record to XML #2819
Proposal: Introduce API to convert the Ballerina record to XML #2819
Comments
@kalaiyarasiganeshalingam How are we handling optional values in the Ballerina record. are we skipping those fields. And also shall we mention, how XML namespaces are handled. other special scenarios in the proposal |
No, we have to convert to an empty XML element, but if the record has optional fields, we can skip those. I have added these to the proposal now.
Updated the proposal |
@sameerajayasoma would you please have a look and give your feedback. |
This should work consistently with OpenAPI https://swagger.io/docs/specification/data-models/representing-xml/ If I have a value of type json (including records) that belongs to some Ballerina type T that we map to some OpenAPI spec S, and there is a value V that belongs to T, then the mapping of V to X being done in this issue should be the valid XML according to S. |
The default attribute prefix in In Ballerina Can we introduce annotations to change the default behavior of this record to XML mapping and vice versa? OpenAPI spec allow adding fields to change the behaviour. |
The proposal does not cover records values having fields of type |
But, the Ballerina connector team is building a WSDL tool that builds client connectors to connect to the SOAP backends. This feature is useful for them to convert Ballerina records to XML payload easily. Currently, they are building the XML payload from the record in each connector manually. |
@jclark @sameerajayasoma Could you please give your feedback on the updated changes. |
I guess I don't really understand what problem you are trying to solve: fromRecord doesn't make much sense to me. There are lots of different ways of converting between Ballerina and XML: there's no one right way; it depends what problem you are trying to solve. I would see OpenAPI-style XML as an alternative way of serializing the json and anydata types. So I would expect a
|
The problem is, We are building a connector to connect with the SOAP backend where the request payload is XML type. The connector APIs accept Ballerina record values, and inside the connector, we need to convert the Ballerina record value to XML value and construct a request payload. We are planning to give an API from the As we already have +1 for suggested APIs. They can use to convert both JSON and anydata types—the only doubt is whether the function names are aligned with our convention. |
Please find the following APIs definitions according to the latest suggestion. # Converts an XML to its `Map` or `Record` representation.
# The namespaces and attributes will not be considered a special case.
#
# + xmlValue - The XML source to be converted to a given target type
# + returnType - The `typedesc` of the `map<anydata>` that should be returned as a result
# + return - The given target type representation of the given XML on success,
# else returns an `xmldata:Error`
public isolated function fromXml(xml xmlValue, typedesc<(map<anydata>|json)> returnType = <>)
returns returnType|Error; # Converts a `Map` or `Record` representation to its XML representation.
# The record has annotations to configure namespaces and attributes, but others don't have these.
#
# + mapValue - The `Map` or `Record` representation source to be converted to XML
# + return - XML representation of the given source if the source is
# successfully converted or else an `xmldata:Error`
public isolated function toXml(map<anydata> mapValue) returns xml|Error; |
Summary
The Ballerina Xmldata module doesn't have any API to convert the Ballerina record to XML directly. So, this proposal introduces a new
fromRecord
API to convert the Ballerina record to XML.Goals
Provide a way to directly convert the Ballerina record to XML.
Motivation
When we are writing a connector for the SOAP backend service(e.g Netsuite Connector), we need to convert the Ballerina record value to XML payload. As mentioned in the summary, We don't have a way to convert the Ballerina record to XML. At the moment, users have to write their own custom implementation to convert Ballerina records to XML and it would be easy for them If we provide an API to convert.
Note: This feature is also required by the connector team and they are planning to remove their custom implementation and use
xmldata
module, once we have this feature.Description
The API definition of this:
Rules for Record to XML Conversion
The following rules are used during the conversion process:
_
, those will be handled as attributes or namespaces in the XML.The following table shows a mapping between the different forms of XML, to a corresponding matching Ballerina record representation by considering the above rules.
key-value
{name:"Asha"}
<name>Asha</name>
key-value and value is ""
{name:""}
<name/>
{name: ()}
<name/>
{}
single key-value
{
"store": {
"name": "Anne",
"address": {
"street": "Main",
"city": "94"
}
}
}
<store>
<name>Anne</name>
<address>
<street>Main</street>
<city>94</city>
</address>
</store>
multiple key-value pairs
{
"key1":"value1",
"key2":"value2"
}
root
tag<root>
<key1>value1</key1>
<key2>value2</key2>
</root>
as "\#content"
{"\#content":"value1"}
value1
prefix as ‘_’
{
"foo": {
"_key": "value",
"_xmlns\:ns0":"<http://sample.com>"
}
}
<foo key="value"
xmlns:ns0="<http://sample.com>"/>
The text was updated successfully, but these errors were encountered: