Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 4, 2025

Why make this change?

Closes #2698

SQL Server's XML data type (supported since SQL Server 2005) was not recognized by DAB, preventing use of tables with XML columns.

What is this change?

Treat XML as a string type, consistent with how SQL Server emits it via FOR JSON:

  • SqlTypeConstants.cs: Mark xml as supported (true)
  • TypeHelper.cs: Map SqlDbType.Xmltypeof(string)
  • DatabaseSchema-MsSql.sql: Add xml_types column to type_table with test data for integration testing

DAB passes XML as string in both directions; SQL Server handles validation.

How was this tested?

  • Integration Tests
  • Unit Tests

Existing CLRtoJsonValueTypeUnitTests now validates (xml,True) test case passes. Added XML column to type_table in MsSql test database schema with various XML test data to enable integration testing of XML data type operations.

Sample Request(s)

POST /documents
Content-Type: application/json

{
  "id": 1,
  "metadata": "<author>admin</author>"
}

Response:

{
  "value": [
    {
      "id": 1,
      "metadata": "<author>admin</author>"
    }
  ]
}
Original prompt

This section details on the original issue you should resolve

<issue_title>[Enhancement]: Support XML data type for MSSQL</issue_title>
<issue_description># XML data type

In SQL Server and Azure SQL Database, the XML data type stores structured XML content. Unlike the JSON data type, XML has been supported since SQL Server 2005 and includes native indexing, query, and validation capabilities.

CREATE TABLE documents (
    id INT PRIMARY KEY,
    metadata XML  -- a native XML document
)

FOR JSON support

When used with FOR JSON, SQL Server will emit the XML content as a string, not a parsed structure. This is because XML is not directly translatable to JSON without transformation.

SELECT id, metadata FROM documents FOR JSON AUTO;

Returns:

[
  {
    "id": 1,
    "metadata": "<doc><type>report</type><author>admin</author></doc>"
  }
]

Inserting XML

INSERT INTO documents (id, metadata)
VALUES (
    1,
    '<doc><type>report</type><author>admin</author></doc>'
);
  • The XML must be passed as a valid string literal.
  • SQL Server will parse and validate the XML at runtime.
  • You can define an XML schema collection for strict validation if needed.

Data API builder behavior

Data API builder (DAB) should treat the XML column as a string in both directions.

Query operations

DAB will expose XML values as string content in REST responses:

{
  "value": [
    {
      "id": 1,
      "metadata": "<doc><type>report</type><author>admin</author></doc>"
    }
  ]
}

Mutation operations

Whether creating or updating, input must be a string containing valid XML:

POST /documents
Content-Type: application/json

{
  "id": 2,
  "metadata": "<doc><type>memo</type><author>guest</author></doc>"
}

XML Considerations

  1. SQL Server will validate the string as well-formed XML before storing.
  2. DAB does not parse or format XML—it treats the field as a text payload.
  3. XML schema collections can be applied to restrict content if required.</issue_description>

Comments on the Issue (you are @copilot in this section)

@JerryNixon Basically, treat it as string.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add support for XML data type in MSSQL Support XML data type for MSSQL Dec 4, 2025
Copilot AI requested a review from JerryNixon December 4, 2025 03:23
@JerryNixon JerryNixon moved this from Todo to Review In Progress in Data API builder Dec 4, 2025
@JerryNixon JerryNixon added this to the Dec 2025 milestone Dec 4, 2025
@JerryNixon JerryNixon marked this pull request as ready for review December 4, 2025 05:08
Copilot AI review requested due to automatic review settings December 4, 2025 05:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for SQL Server's XML data type by treating it as a string type, consistent with how SQL Server emits XML content via FOR JSON.

  • Marks XML as a supported SQL type in SqlTypeConstants.cs
  • Maps SqlDbType.Xml to typeof(string) in TypeHelper.cs

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Core/Models/SqlTypeConstants.cs Marks xml type as supported (changed from false to true)
src/Core/Services/TypeHelper.cs Adds mapping from SqlDbType.Xml to typeof(string) in the type conversion dictionary

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

[SqlDbType.VarBinary] = typeof(byte[]),
[SqlDbType.VarChar] = typeof(string)
[SqlDbType.VarChar] = typeof(string),
[SqlDbType.Xml] = typeof(string)
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an XML column to the type_table in the test database schema (DatabaseSchema-MsSql.sql) to enable integration testing of XML data type operations. While the unit tests in CLRtoJsonValueTypeUnitTests will validate the type mapping, integration tests would verify actual CRUD operations with XML data.

For example, add to the type_table schema:

xml_types xml

And include test data:

INSERT INTO type_table(..., xml_types) VALUES (..., '<doc><type>report</type></doc>')

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 3ce04b9. The type_table now includes an xml_types xml column with test data:

  • <doc><type>report</type></doc>
  • <author>admin</author>
  • <metadata><key>value</key></metadata>
  • <config><setting enabled="true"/></config>
  • NULL (for row 5)

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

Labels

None yet

Projects

Status: Review In Progress

Development

Successfully merging this pull request may close these issues.

[Enhancement]: Support XML data type for MSSQL Host DAB in AKS

2 participants