Adds discriminator support to OpenApi (#54)
* Adding setting for discriminator value support * Adding the discriminator value constant * Adding discriminator support during schema generation Co-authored-by: Irvine Sunday <v-irsund@microsoft.com>
This commit is contained in:
parent
2755da6a59
commit
f723b671d8
|
@ -1,4 +1,4 @@
|
|||
// ------------------------------------------------------------
|
||||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
|
||||
// ------------------------------------------------------------
|
||||
|
@ -64,5 +64,10 @@ namespace Microsoft.OpenApi.OData.Common
|
|||
/// extension for paging
|
||||
/// </summary>
|
||||
public static string xMsPageable = "x-ms-pageable";
|
||||
|
||||
/// <summary>
|
||||
/// extension for discriminator value support
|
||||
/// </summary>
|
||||
public static string xMsDiscriminatorValue = "x-ms-discriminator-value";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ using Microsoft.OpenApi.OData.Edm;
|
|||
using Microsoft.OpenApi.OData.Common;
|
||||
using Microsoft.OpenApi.Exceptions;
|
||||
using System.Linq;
|
||||
using Microsoft.OpenApi.Interfaces;
|
||||
|
||||
namespace Microsoft.OpenApi.OData.Generator
|
||||
{
|
||||
|
@ -207,17 +208,35 @@ namespace Microsoft.OpenApi.OData.Generator
|
|||
}
|
||||
}
|
||||
|
||||
private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext context, IEdmStructuredType structuredType, bool processBase, bool processExample)
|
||||
private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext context, IEdmStructuredType structuredType, bool processBase, bool processExample,
|
||||
IEnumerable<IEdmEntityType> derivedTypes = null)
|
||||
{
|
||||
Debug.Assert(context != null);
|
||||
Debug.Assert(structuredType != null);
|
||||
|
||||
if (context.Settings.EnableDiscriminatorValue && derivedTypes == null)
|
||||
{
|
||||
derivedTypes = context.Model.FindDirectlyDerivedTypes(structuredType).OfType<IEdmEntityType>();
|
||||
}
|
||||
|
||||
if (processBase && structuredType.BaseType != null)
|
||||
{
|
||||
// The x-ms-discriminator-value extension is added to structured types which are derived types.
|
||||
Dictionary<string, IOpenApiExtension> extension = null;
|
||||
if (context.Settings.EnableDiscriminatorValue && !derivedTypes.Any())
|
||||
{
|
||||
extension = new Dictionary<string, IOpenApiExtension>
|
||||
{
|
||||
{ Constants.xMsDiscriminatorValue, new OpenApiString("#" + structuredType.FullTypeName()) }
|
||||
};
|
||||
}
|
||||
|
||||
// A structured type with a base type is represented as a Schema Object
|
||||
// that contains the keyword allOf whose value is an array with two items:
|
||||
return new OpenApiSchema
|
||||
{
|
||||
Extensions = extension,
|
||||
|
||||
AllOf = new List<OpenApiSchema>
|
||||
{
|
||||
// 1. a JSON Reference to the Schema Object of the base type
|
||||
|
@ -231,7 +250,7 @@ namespace Microsoft.OpenApi.OData.Generator
|
|||
},
|
||||
|
||||
// 2. a Schema Object describing the derived type
|
||||
context.CreateStructuredTypeSchema(structuredType, false, false)
|
||||
context.CreateStructuredTypeSchema(structuredType, false, false, derivedTypes)
|
||||
},
|
||||
|
||||
AnyOf = null,
|
||||
|
@ -242,6 +261,16 @@ namespace Microsoft.OpenApi.OData.Generator
|
|||
}
|
||||
else
|
||||
{
|
||||
// The discriminator object is added to structured types which have derived types.
|
||||
OpenApiDiscriminator discriminator = null;
|
||||
if (context.Settings.EnableDiscriminatorValue && derivedTypes.Any() && structuredType.BaseType != null)
|
||||
{
|
||||
discriminator = new OpenApiDiscriminator
|
||||
{
|
||||
PropertyName = "@odata.type"
|
||||
};
|
||||
}
|
||||
|
||||
// A structured type without a base type is represented as a Schema Object of type object
|
||||
OpenApiSchema schema = new OpenApiSchema
|
||||
{
|
||||
|
@ -249,6 +278,8 @@ namespace Microsoft.OpenApi.OData.Generator
|
|||
|
||||
Type = "object",
|
||||
|
||||
Discriminator = discriminator,
|
||||
|
||||
// Each structural property and navigation property is represented
|
||||
// as a name/value pair of the standard OpenAPI properties object.
|
||||
Properties = context.CreateStructuredTypePropertiesSchema(structuredType),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// ------------------------------------------------------------
|
||||
// ------------------------------------------------------------
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
|
||||
// ------------------------------------------------------------
|
||||
|
@ -100,6 +100,11 @@ namespace Microsoft.OpenApi.OData
|
|||
/// </summary>
|
||||
public string PageableOperationName { get; set; } = "listMore";
|
||||
|
||||
/// <summary>
|
||||
/// Gets/sets a value indicating whether or not to allow discriminator value support.
|
||||
/// </summary>
|
||||
public bool EnableDiscriminatorValue { get; set; } = false;
|
||||
|
||||
internal OpenApiConvertSettings Clone()
|
||||
{
|
||||
var newSettings = new OpenApiConvertSettings();
|
||||
|
@ -121,6 +126,7 @@ namespace Microsoft.OpenApi.OData
|
|||
newSettings.EnableUriEscapeFunctionCall = this.EnableUriEscapeFunctionCall;
|
||||
newSettings.EnablePagination = this.EnablePagination;
|
||||
newSettings.PageableOperationName = this.PageableOperationName;
|
||||
newSettings.EnableDiscriminatorValue = this.EnableDiscriminatorValue;
|
||||
|
||||
return newSettings;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue