Repository navigation
Support XML data type for MSSQL #3006
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
Open
Copilot
wants to merge
16
commits into
main
Choose a base branch
from
copilot/support-xml-data-type-mssql
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
afb1798
Initial plan
Copilot 353b1ea
Add support for XML data type in MSSQL by treating it as string
Copilot 3ce04b9
Add XML column to type_table in MsSql test database schema
Copilot f4b9b5f
Merge branch 'main' into copilot/support-xml-data-type-mssql
RubenCerna2079 686ec1e
Merge branch 'main' into copilot/support-xml-data-type-mssql
JerryNixon 27c857f
Merge branch 'main' into copilot/support-xml-data-type-mssql
aaronburtle f0ecf98
Merge branch 'main' into copilot/support-xml-data-type-mssql
JerryNixon 24a7c81
Fix merge conflict
RubenCerna2079 c12190f
Remove XML from unavailable data types for autoentity generation
RubenCerna2079 f4151e0
Add tests for XML data type
RubenCerna2079 6e253e4
Add tests for XML data type
RubenCerna2079 73046cf
Merge remote-tracking branch 'refs/remotes/origin/copilot/support-xml…
RubenCerna2079 911ab54
Add MCP tests
RubenCerna2079 90ce517
Fix test
RubenCerna2079 2fe1613
:
RubenCerna2079 93cd147
Merge branch 'main' into copilot/support-xml-data-type-mssql
RubenCerna2079 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,7 +116,8 @@ public static class TypeHelper | |
| [SqlDbType.UniqueIdentifier] = typeof(Guid), | ||
| [SqlDbType.VarBinary] = typeof(byte[]), | ||
| [SqlDbType.VarChar] = typeof(string), | ||
| [SqlDbType.Vector] = typeof(float) | ||
| [SqlDbType.Vector] = typeof(float), | ||
| [SqlDbType.Xml] = typeof(string) | ||
| }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to handle XML explicitly in filtering and ordering during queries? Do we have supporting tests covering those? |
||
|
|
||
| private static readonly Dictionary<SqlDbType, DbType> _sqlDbDateTimeTypeToDbType = new() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| using System.Collections.Generic; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Azure.DataApiBuilder.Mcp.BuiltInTools; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using ModelContextProtocol.Protocol; | ||
| using static Azure.DataApiBuilder.Service.Tests.SqlTests.SqlTestHelper; | ||
|
|
||
| namespace Azure.DataApiBuilder.Service.Tests.Mcp | ||
| { | ||
| [TestClass, TestCategory(TestCategory.MSSQL)] | ||
| public class MsSqlMcpXmlTypesTests : McpToolTestBase | ||
| { | ||
| private const string ENTITY_NAME = "XmlProfile"; | ||
| private const string ORIGINAL_XML = "<order id=\"200\"><item>Pen</item></order>"; | ||
|
|
||
| [ClassInitialize] | ||
| public static async Task SetupAsync(TestContext context) | ||
| { | ||
| DatabaseEngine = TestCategory.MSSQL; | ||
| await InitializeTestFixture(); | ||
| } | ||
|
|
||
| /// <summary>Verifies MCP reads preserve XML attributes and SQL null values.</summary> | ||
| [DataTestMethod] | ||
| [DataRow(1, "<order id=\"100\"><item>Book</item><qty>2</qty></order>", DisplayName = "Read XML with a quoted attribute")] | ||
| [DataRow(2, "<config><setting enabled=\"true\" name=\"cache\"/></config>", DisplayName = "Read XML with multiple quoted attributes")] | ||
| [DataRow(4, null, DisplayName = "Read null XML content")] | ||
| public async Task ReadXmlById_ReturnsXmlOrNull(int id, string expectedXml) | ||
| { | ||
| JsonElement records = await ReadXmlAsync($"id eq {id}"); | ||
| Assert.AreEqual(1, records.GetArrayLength()); | ||
| AssertXmlContent(records[0], expectedXml); | ||
| } | ||
|
|
||
| /// <summary>Verifies an MCP collection returns valid XML strings.</summary> | ||
| [TestMethod] | ||
| public async Task ReadXmlList_ReturnsStringsAndNull() | ||
| { | ||
| JsonElement records = await ReadXmlAsync("id ge 1 and id le 4"); | ||
| Assert.AreEqual(4, records.GetArrayLength()); | ||
| foreach (JsonElement record in records.EnumerateArray()) | ||
| { | ||
| JsonElement content = record.GetProperty("content"); | ||
| if (record.GetProperty("id").GetInt32() == 4) | ||
| { | ||
| Assert.AreEqual(JsonValueKind.Null, content.ValueKind); | ||
| } | ||
| else | ||
| { | ||
| Assert.AreEqual(JsonValueKind.String, content.ValueKind); | ||
| AssertIsValidXml(content.GetString()!); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Verifies filtering an XML column excludes records with null content.</summary> | ||
| [TestMethod] | ||
| public async Task FilterXmlNotNull_ExcludesNullContent() | ||
| { | ||
| JsonElement records = await ReadXmlAsync("id ge 1 and id le 4 and content ne null"); | ||
| Assert.AreEqual(3, records.GetArrayLength()); | ||
| foreach (JsonElement record in records.EnumerateArray()) | ||
| { | ||
| Assert.AreEqual(JsonValueKind.String, record.GetProperty("content").ValueKind); | ||
| Assert.AreNotEqual(4, record.GetProperty("id").GetInt32()); | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Verifies MCP create/read round trips preserve XML or null, and deletion removes the record.</summary> | ||
| [DataTestMethod] | ||
| [DataRow(ORIGINAL_XML, DisplayName = "Create and delete XML with a quoted attribute")] | ||
| [DataRow("<config><setting enabled=\"true\" name=\"cache\"/></config>", DisplayName = "Create and delete XML with multiple quoted attributes")] | ||
| [DataRow(null, DisplayName = "Create and delete null XML content")] | ||
| public async Task CreateXml_RoundTripsAndDeletes(string xml) | ||
| { | ||
| CallToolResult result = await CreateXmlAsync(xml); | ||
| AssertSuccess(result, "Creating XML content should succeed."); | ||
| JsonElement created = GetRecordsArray(ParseResultRoot(result))[0]; | ||
| int id = created.GetProperty("id").GetInt32(); | ||
| try | ||
| { | ||
| AssertXmlContent(created, xml); | ||
| AssertXmlContent((await ReadXmlAsync($"id eq {id}"))[0], xml); | ||
| } | ||
| finally | ||
| { | ||
| await DeleteXmlAsync(id); | ||
| } | ||
|
|
||
| Assert.AreEqual(0, (await ReadXmlAsync($"id eq {id}")).GetArrayLength()); | ||
| } | ||
|
|
||
| /// <summary>Verifies MCP updates preserve XML attributes and escaped text, or clear content to null.</summary> | ||
| [DataTestMethod] | ||
| [DataRow("<order id=\"999\"><item>Updated & quoted</item></order>", DisplayName = "Update XML with a quoted attribute and escaped text")] | ||
| [DataRow("<config><setting enabled=\"false\" name=\"updated\"/></config>", DisplayName = "Update XML with multiple quoted attributes")] | ||
| [DataRow(null, DisplayName = "Update XML content to null")] | ||
| public async Task UpdateXml_RoundTrips(string xml) | ||
| { | ||
| CallToolResult created = await CreateXmlAsync(ORIGINAL_XML); | ||
| AssertSuccess(created, "Creating the update fixture should succeed."); | ||
| int id = GetRecordsArray(ParseResultRoot(created))[0].GetProperty("id").GetInt32(); | ||
| try | ||
| { | ||
| CallToolResult result = await UpdateXmlAsync(id, xml); | ||
| AssertSuccess(result, "Updating XML content should succeed."); | ||
| AssertXmlContent(ParseResultRoot(result).GetProperty("result"), xml); | ||
| AssertXmlContent((await ReadXmlAsync($"id eq {id}"))[0], xml); | ||
| } | ||
| finally | ||
| { | ||
| await DeleteXmlAsync(id); | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Verifies MCP creation rejects malformed XML at the database boundary.</summary> | ||
| [DataTestMethod] | ||
| [DataRow("<order><item>Book</order>", DisplayName = "Reject XML creation with a mismatched closing tag")] | ||
| [DataRow("<order id=unquoted></order>", DisplayName = "Reject XML creation with an unquoted attribute")] | ||
| public async Task CreateMalformedXml_ReturnsError(string xml) | ||
| { | ||
| AssertError(await CreateXmlAsync(xml)); | ||
| } | ||
|
|
||
| /// <summary>Verifies rejected malformed XML updates leave the stored content unchanged.</summary> | ||
| [DataTestMethod] | ||
| [DataRow("<order><item>Book</order>", DisplayName = "Reject XML update with a mismatched closing tag and preserve content")] | ||
| [DataRow("<order id=unquoted></order>", DisplayName = "Reject XML update with an unquoted attribute and preserve content")] | ||
| public async Task UpdateMalformedXml_ReturnsErrorAndPreservesContent(string xml) | ||
| { | ||
| CallToolResult created = await CreateXmlAsync(ORIGINAL_XML); | ||
| AssertSuccess(created, "Creating the update fixture should succeed."); | ||
| int id = GetRecordsArray(ParseResultRoot(created))[0].GetProperty("id").GetInt32(); | ||
| try | ||
| { | ||
| AssertError(await UpdateXmlAsync(id, xml)); | ||
| AssertXmlContent((await ReadXmlAsync($"id eq {id}"))[0], ORIGINAL_XML); | ||
| } | ||
| finally | ||
| { | ||
| await DeleteXmlAsync(id); | ||
| } | ||
| } | ||
|
|
||
| private static async Task<JsonElement> ReadXmlAsync(string filter) | ||
| { | ||
| CallToolResult result = await ExecuteToolAsync(new ReadRecordsTool(), BuildQueryServiceProvider(), | ||
| new Dictionary<string, object?> { { "entity", ENTITY_NAME }, { "filter", filter }, { "select", "id,content" } }); | ||
| AssertSuccess(result, "Reading XML content should succeed."); | ||
| return GetRecordsArray(ParseResultRoot(result)); | ||
| } | ||
|
|
||
| private static Task<CallToolResult> CreateXmlAsync(string xml) | ||
| { | ||
| return ExecuteToolAsync(new CreateRecordTool(), BuildMutationServiceProvider(), | ||
| new Dictionary<string, object?> | ||
| { | ||
| { "entity", ENTITY_NAME }, | ||
| { "data", new Dictionary<string, object?> { { "content", xml } } } | ||
| }); | ||
| } | ||
|
|
||
| private static Task<CallToolResult> UpdateXmlAsync(int id, string xml) | ||
| { | ||
| return ExecuteToolAsync(new UpdateRecordTool(), BuildMutationServiceProvider(), | ||
| new Dictionary<string, object?> | ||
| { | ||
| { "entity", ENTITY_NAME }, | ||
| { "keys", new Dictionary<string, object> { { "id", id } } }, | ||
| { "fields", new Dictionary<string, object?> { { "content", xml } } } | ||
| }); | ||
| } | ||
|
|
||
| private static async Task DeleteXmlAsync(int id) | ||
| { | ||
| CallToolResult result = await ExecuteToolAsync(new DeleteRecordTool(), BuildMutationServiceProvider(), | ||
| new Dictionary<string, object?> | ||
| { | ||
| { "entity", ENTITY_NAME }, | ||
| { "keys", new Dictionary<string, object> { { "id", id } } } | ||
| }); | ||
| AssertSuccess(result, "Deleting the XML fixture should succeed."); | ||
| } | ||
|
|
||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.