Skip to content

Commit

Permalink
[OpenAPI] Add "type" property with value "object" response schema to …
Browse files Browse the repository at this point in the history
…fix Infragistics AppBuilder tooling (#2283)

## Why make this change?

- Closes #2212 which describes how the missing `"type":"object"`
key/value pair on the response child object schema breaks certain client
tooling. in this case: Infragistics AppBuilder.

### Background

I found a relevant thread that discusses whether type is a required
property. Consensus is that type isn't required:

OAI/OpenAPI-Specification#1657
OpenAPI-Specification discussion
PaloAltoNetworks/docusaurus-openapi-docs#430
Example of how different tooling handles type or missing type
differently.
Ultimately, different tooling handles the presence of the type property
differently. Some may try to guess the type when not present:

The fact that the type isn't required means that
https://github.com/microsoft/OpenAPI.NET didn't complain about missing
type. An error if type were required would have helped prevent this
becoming an issue in the first place.

## What is this change?

- Adds `"type": "object"` to the openapi document for describing the
response schema:
```json
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object", // <--- This property/value
                                    "properties": {
                                        "value": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/Book"
                                            }
                                        },
                                        "nextLink": {
                                            "type": "string"
                                        }
                                    }
                                }
                            }
                        }
                    }
```

## How was this tested?

- [x] Integration Tests
- [ ] Unit Tests

## Sample Request(s)

View generated schema at
```https
GET localhost:5001/api/openapi
```

Co-authored-by: Abhishek  Kumar <[email protected]>
  • Loading branch information
seantleonard and abhishekkumams authored Jul 3, 2024
1 parent 3b58f34 commit 591d818
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Core/Services/OpenAPI/OpenApiDocumentor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ private static OpenApiMediaType CreateResponseContainer(string responseObjectSch
{
Schema = new()
{
Type = SCHEMA_OBJECT_TYPE,
Properties = responseBodyProperties
}
};
Expand Down
87 changes: 87 additions & 0 deletions src/Service.Tests/OpenApiDocumentor/DocumentVerbosityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Linq;
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>
/// Integration tests validating that expected properties are present in the OpenApiDocument object.
/// </summary>
[TestCategory(TestCategory.MSSQL)]
[TestClass]
public class DocumentVerbosityTests
{
private const string CUSTOM_CONFIG = "doc-verbosity.MsSql.json";
private const string MSSQL_ENVIRONMENT = TestCategory.MSSQL;
private const string MISSING_TYPE_PROPERTY_ERROR = "Response object schema does not include a 'type' property.";
private const string UNEXPECTED_CONTENTS_ERROR = "Unexpected number of response objects to validate.";

/// <summary>
/// Validates that for the Book entity, 7 response object schemas generated by OpenApiDocumentor
/// contain a 'type' property with value 'object'.
///
/// Two paths:
/// - "/Books/id/{id}"
/// - 4 operations GET PUT PATCH DELETE
/// - Validate responses that return result contents:
/// GET (200), PUT (200, 201), PATCH (200, 201)
/// - "/Books"
/// - 2 operations GET(all) POST
/// - Validate responses that return result contents:
/// GET (200), POST (201)
/// </summary>
[TestMethod]
public async Task ResponseObjectSchemaIncludesTypeProperty()
{
// Arrange
Entity entity = new(
Source: new(Object: "books", EntitySourceType.Table, null, null),
GraphQL: new(Singular: null, Plural: null, Enabled: false),
Rest: new(Methods: EntityRestOptions.DEFAULT_SUPPORTED_VERBS),
Permissions: OpenApiTestBootstrap.CreateBasicPermissions(),
Mappings: null,
Relationships: null);

Dictionary<string, Entity> entities = new()
{
{ "Book", entity }
};

RuntimeEntities runtimeEntities = new(entities);

// Act - Create OpenApi document
OpenApiDocument openApiDocument = await OpenApiTestBootstrap.GenerateOpenApiDocumentAsync(
runtimeEntities: runtimeEntities,
configFileName: CUSTOM_CONFIG,
databaseEnvironment: MSSQL_ENVIRONMENT);

// Assert - Validate responses that return result contents: 200, 201
List<OpenApiResponse> responses = openApiDocument.Paths.Values
.SelectMany(pathObject => pathObject.Operations.Values)
.SelectMany(operation => operation.Responses)
// Responses Dictionary: Key: HttpStatusCode, Value: OpenApiResponse
.Where(pair => pair.Key == "200" || pair.Key == "201")
.Select(pair => pair.Value)
.ToList();

// Validate that 7 response object schemas contain a 'type' property with value 'object'
// Test summary describes all 7 expected responses.
Assert.IsTrue(
condition: responses.Count == 7,
message: UNEXPECTED_CONTENTS_ERROR);

foreach (OpenApiResponse response in responses)
{
Assert.IsTrue(
condition: response.Content["application/json"].Schema.Type == "object",
message: MISSING_TYPE_PROPERTY_ERROR);
}
}
}
}

0 comments on commit 591d818

Please sign in to comment.