-
Notifications
You must be signed in to change notification settings - Fork 355
[Phase 3b] MSSQL JSON - return json as string on read + OpenAPI/GraphQL tests #3738
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
Merged
+208
−17
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9639b33
test(mssql-json): add OpenAPI + GraphQL schema-discovery tests (Phase…
souvikghosh04 bc69acd
test(mssql-json): drop OpenAPI Nullable assertion (not expressed by d…
souvikghosh04 d34b7a6
Merge branch 'main' into Usr/sogh/mssql-json-phase3b
souvikghosh04 2f714e7
fix(mssql-json): return native json as string in read path + address …
souvikghosh04 99196a1
Merge branch 'main' into Usr/sogh/mssql-json-phase3b
souvikghosh04 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
80 changes: 80 additions & 0 deletions
80
src/Service.Tests/OpenApiDocumentor/JsonTypeSchemaTests.cs
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,80 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Azure.DataApiBuilder.Config.ObjectModel; | ||
| using Microsoft.OpenApi.Models; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Azure.DataApiBuilder.Service.Tests.OpenApiIntegration | ||
| { | ||
| /// <summary> | ||
| /// Validates how a SQL Server 2025 native <c>json</c> column is surfaced in the generated | ||
| /// OpenAPI document. DAB treats a json column as a normal <c>string</c> for input and output, | ||
| /// so it must be described with <c>type: string</c> and no custom <c>format</c> in the response | ||
| /// schema as well as the POST / PUT / PATCH request-body schemas. This is the schema-discovery | ||
| /// counterpart to the REST round-trip coverage in | ||
| /// <see cref="SqlTests.RestApiTests.MsSqlRestJsonTypesTests"/>. | ||
| /// NOTE: The native JSON data type requires SQL Server 2025 / Azure SQL. | ||
| /// </summary> | ||
| [TestCategory(TestCategory.MSSQL)] | ||
| [TestClass] | ||
| public class JsonTypeSchemaTests | ||
| { | ||
| private const string CONFIG_FILE = "json-type-openapi-config.MsSql.json"; | ||
| private const string DB_ENV = TestCategory.MSSQL; | ||
|
|
||
| /// <summary> | ||
| /// The <c>profiles.metadata</c> (json) column must be described as a plain string with no format | ||
| /// in the response schema and in both request-body schemas (POST => <c>Profile_NoAutoPK</c>, | ||
| /// PUT/PATCH => <c>Profile_NoPK</c>) - proving json is treated like a normal string for both input | ||
| /// and output, with no bespoke scalar/format. (DAB does not express column nullability on the | ||
| /// property schema for any type, so that is not asserted here.) | ||
| /// </summary> | ||
| [TestMethod] | ||
| public async Task JsonColumn_IsDescribedAsStringWithoutFormat_InResponseAndRequestBodies() | ||
| { | ||
| OpenApiDocument doc = await GenerateProfileDocumentAsync(); | ||
|
|
||
| // Response body schema, plus the POST and PUT/PATCH request-body schemas. | ||
| AssertMetadataIsPlainString(doc, "Profile"); | ||
| AssertMetadataIsPlainString(doc, "Profile_NoAutoPK"); | ||
| AssertMetadataIsPlainString(doc, "Profile_NoPK"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Asserts the named component schema exposes <c>metadata</c> as a plain string with no format. | ||
| /// </summary> | ||
| private static void AssertMetadataIsPlainString(OpenApiDocument doc, string schemaName) | ||
| { | ||
| Assert.IsTrue(doc.Components.Schemas.ContainsKey(schemaName), $"Schema {schemaName} should exist."); | ||
|
|
||
| OpenApiSchema schema = doc.Components.Schemas[schemaName]; | ||
| Assert.IsTrue(schema.Properties.ContainsKey("metadata"), $"The json metadata column should be present in {schemaName}."); | ||
|
|
||
| OpenApiSchema metadataSchema = schema.Properties["metadata"]; | ||
| Assert.AreEqual("string", metadataSchema.Type, $"A json column must be described as a plain string in {schemaName}."); | ||
| Assert.IsTrue(string.IsNullOrEmpty(metadataSchema.Format), $"A json column must not carry a bespoke OpenAPI format in {schemaName}."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Builds an OpenAPI document for a Profile entity sourced from the <c>profiles</c> table, | ||
| /// with REST + GraphQL enabled and anonymous/authenticated CRUD permissions. | ||
| /// </summary> | ||
| private static async Task<OpenApiDocument> GenerateProfileDocumentAsync() | ||
| { | ||
| Entity entity = new( | ||
| Source: new("profiles", EntitySourceType.Table, null, null), | ||
| Fields: null, | ||
| GraphQL: new("Profile", "Profiles", true), | ||
| Rest: new(EntityRestOptions.DEFAULT_SUPPORTED_VERBS), | ||
| Permissions: OpenApiTestBootstrap.CreateBasicPermissions(), | ||
| Mappings: null, | ||
| Relationships: null); | ||
|
|
||
| RuntimeEntities entities = new(new Dictionary<string, Entity> { { "Profile", entity } }); | ||
| return await OpenApiTestBootstrap.GenerateOpenApiDocumentAsync(entities, CONFIG_FILE, DB_ENV); | ||
| } | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
src/Service.Tests/SqlTests/GraphQLQueryTests/MsSqlGraphQLJsonSchemaTests.cs
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,86 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Azure.DataApiBuilder.Service.Tests.SqlTests.GraphQLQueryTests | ||
| { | ||
| /// <summary> | ||
| /// GraphQL schema-discovery (introspection) tests for a SQL Server 2025 native <c>json</c> column. | ||
| /// DAB does nothing special for a JSON column - it is treated as a normal <c>string</c>, so the | ||
| /// GraphQL schema must expose it using the built-in <c>String</c> scalar (no bespoke JSON scalar), | ||
| /// honoring the column's nullability. This is the GraphQL counterpart to the REST/OpenAPI coverage | ||
| /// in <see cref="RestApiTests.MsSqlRestJsonTypesTests"/> and <c>JsonTypeSchemaTests</c>. | ||
| /// NOTE: The native JSON data type requires SQL Server 2025 / Azure SQL. | ||
| /// </summary> | ||
| [TestClass, TestCategory(TestCategory.MSSQL)] | ||
| public class MsSqlGraphQLJsonSchemaTests : SqlTestBase | ||
| { | ||
| [ClassInitialize] | ||
| public static async Task SetupAsync(TestContext context) | ||
| { | ||
| DatabaseEngine = TestCategory.MSSQL; | ||
| await InitializeTestFixture(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Introspecting the Profile type must report its json-backed <c>metadata</c> field as the | ||
| /// built-in nullable <c>String</c> scalar - proving JSON gets no custom scalar in the schema. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public async Task JsonColumn_IsIntrospectedAsBuiltInStringScalar() | ||
| { | ||
| string introspectionQuery = @"{ | ||
| __type(name: ""Profile"") { | ||
| name | ||
| fields { | ||
| name | ||
| type { kind name ofType { kind name } } | ||
| } | ||
| } | ||
| }"; | ||
|
|
||
| JsonElement type = await ExecuteGraphQLRequestAsync(introspectionQuery, "__type", isAuthenticated: false); | ||
|
|
||
| Assert.AreEqual("Profile", type.GetProperty("name").GetString(), "Introspection should resolve the Profile GraphQL type."); | ||
|
|
||
| JsonElement metadataField = type.GetProperty("fields").EnumerateArray() | ||
| .Single(f => f.GetProperty("name").GetString() == "metadata"); | ||
|
|
||
| // A nullable column surfaces as the bare scalar (no NON_NULL wrapper), so type.kind/name | ||
| // describe the scalar directly. | ||
| JsonElement metadataType = metadataField.GetProperty("type"); | ||
| Assert.AreEqual("SCALAR", metadataType.GetProperty("kind").GetString(), "A json column must map to a scalar, not a custom object/scalar type."); | ||
| Assert.AreEqual("String", metadataType.GetProperty("name").GetString(), "A json column must use the built-in String scalar (no bespoke JSON scalar)."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// profile_by_pk(id: 1) { metadata } - Verify that reading the json-backed field through GraphQL | ||
| /// succeeds and returns the payload as a JSON string. The String leaf resolver calls | ||
| /// JsonElement.GetString(), which only works because the engine casts the json column to | ||
| /// NVARCHAR(MAX) so it is emitted as an escaped string rather than an inlined JSON object. | ||
| /// This guards against the introspection test passing while a real read throws a GraphQLMapping error. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public async Task JsonColumn_GraphQLRead_ReturnsPayloadAsString() | ||
| { | ||
| string query = @"{ | ||
| profile_by_pk(id: 1) { | ||
| metadata | ||
| } | ||
| }"; | ||
|
|
||
| JsonElement result = await ExecuteGraphQLRequestAsync(query, "profile_by_pk", isAuthenticated: false); | ||
|
|
||
| JsonElement metadata = result.GetProperty("metadata"); | ||
| Assert.AreEqual(JsonValueKind.String, metadata.ValueKind, "A json column must be returned as a JSON string through GraphQL (treated as a normal string)."); | ||
|
|
||
| JsonElement parsed = JsonDocument.Parse(metadata.GetString()!).RootElement; | ||
| Assert.AreEqual("admin", parsed.GetProperty("role").GetString()); | ||
| Assert.AreEqual(3, parsed.GetProperty("tier").GetInt32()); | ||
| } | ||
| } | ||
| } | ||
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
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.