microsoft / typespec

https://typespec.io/
MIT License
4.36k stars 203 forks source link

Xml support #1180

Closed timotheeguerin closed 7 months ago

timotheeguerin commented 1 year ago

Probably would want a @cadl-lang/xml library which would add a few more decorators.

Azure Notification Hubs has many APIs that are Atom+XML based which is not easy to describe using CADL directly. Instead, I would prefer to model an object directly such as a RegistrationDescription as the desired state, and transform into Atom+XML entry and feed format directly.

For example, I could describe a RegistrationDescription and AppleRegistrationDescription as the following:

@discriminator("platform")
@doc("Represents an Azure Notification Hubs registration description")
model RegistrationDescription {
  @doc("The registration ID")
  registrationId?: string;
  @doc("The registration tags comma separated list")
  tags?: string;
  @doc("The registration etag")
  etag?: string;
  @doc("The registration push variables property bag")
  pushVariables?: Record<string>;
  @doc("The registration expiration time")
  expirationTime?: plainDate;
}

@doc("Represents an Apple based Azure Notification Hubs registration description")
model AppleRegistrationDescription extends RegistrationDescription {
  @doc("The APNs device token")
  deviceToken: string;
  @doc("The platform of registration description")
  platform: "apple";
}

These models, should have the platform property removed, and serialized into a Atom+XML entry with Pascal case properties such as the following.

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
  <content type="application/xml">
    <AppleRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
      <Tags>myTag, myOtherTag</Tags>
      <DeviceToken>{DeviceToken}</DeviceToken>
    </AppleRegistrationDescription>
  </content>
</entry>

And in the case of list methods, should be wrapped in an Atom+XML feed such as the following:

<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="/{NotificationTopic}/channels/{channel hash}" />
  <id> https://{tenant}.windows.net/{NotificationTopic}/channels/{channel hash}</id>
  <updated>2012-08-17T17:32:00Z</updated>
  <entry xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:etag="1">
    <id>https://{tenant}.windows.net/{NotificationTopic}/registrations/{registrationId}</id>
    <title type="text"> /{NotificationTopic}/registrations/{registrationId}</title>
    <updated>2012-08-17T17:32:00Z</updated>
    <content type="application/xml">
      <AppleRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
        <Tags>myTag, myOtherTag</Tags>
        <DeviceToken>{DeviceToken}</DeviceToken>
      </AppleRegistrationDescription>
    </content>
  </entry>
  <entry>
    <!-- Other entries -->
  </entry>
</feed>

This should support CDATA, namespaces, attributes and other pieces such as the following possible annotations:

markcowl commented 1 year ago

@timotheeguerin please add an estimate

markcowl commented 1 year ago

Note: the DPG 1.0 Requirement is just for any breaking changes required for this support to be implemented

timotheeguerin commented 7 months ago

Closing in favor of https://github.com/microsoft/typespec/issues/2970