Add Path provider and path prefix provider into setting

This commit is contained in:
Sam Xu 2021-02-08 19:57:15 -08:00
parent 5a83e6b553
commit 1643ba5ea1
6 changed files with 103 additions and 5 deletions

View file

@ -49,7 +49,8 @@ namespace Microsoft.OpenApi.OData.Edm
OperationHanderProvider = new OperationHandlerProvider();
PathItemHanderProvider = new PathItemHandlerProvider();
_pathProvider = new ODataPathProvider();
// If no path provider, use the default path provider.
_pathProvider = settings.PathProvider ?? new ODataPathProvider();
if (settings.EnableKeyAsSegment != null)
{

View file

@ -0,0 +1,26 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Collections.Generic;
using Microsoft.OpenApi.Models;
namespace Microsoft.OpenApi.OData.Extensions
{
/// <summary>
/// The interface for route prefix.
/// </summary>
public interface IODataRoutePathPrefixProvider
{
/// <summary>
/// The route prefix.
/// </summary>
public string PathPrefix { get; }
/// <summary>
/// The route prefix parameters.
/// </summary>
public IEnumerable<OpenApiParameter> Parameters { get; }
}
}

View file

@ -0,0 +1,26 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Collections.Generic;
using Microsoft.OpenApi.Models;
namespace Microsoft.OpenApi.OData.Extensions
{
/// <summary>
/// Default implementation of <see cref="IODataRoutePathPrefixProvider"/>.
/// </summary>
public class ODataRoutePathPrefixProvider : IODataRoutePathPrefixProvider
{
/// <summary>
/// Gets/sets the path prefix.
/// </summary>
public string PathPrefix { get; set; }
/// <summary>
/// Gets/sets the associated parameters for the path prefix.
/// </summary>
public IEnumerable<OpenApiParameter> Parameters { get; set; }
}
}

View file

@ -4,6 +4,9 @@
// ------------------------------------------------------------
using System;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Extensions;
namespace Microsoft.OpenApi.OData
{
@ -118,7 +121,35 @@ namespace Microsoft.OpenApi.OData
/// <summary>
/// Gets/sets a value that specifies a prefix to be prepended to all generated paths.
/// </summary>
public string PathPrefix { get; set; } = string.Empty;
public string PathPrefix
{
get
{
if (RoutePathPrefixProvider != null)
{
return RoutePathPrefixProvider.PathPrefix;
}
return null;
}
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw Error.ArgumentNullOrEmpty("value");
}
RoutePathPrefixProvider = new ODataRoutePathPrefixProvider
{
PathPrefix = value
};
}
}
/// <summary>
/// Gets/sets a route path prefix provider.
/// </summary>
public IODataRoutePathPrefixProvider RoutePathPrefixProvider { get; set; }
/// <summary>
/// Gets/Sets a value indicating whether or not to show the OpenAPI links in the responses.
@ -135,6 +166,11 @@ namespace Microsoft.OpenApi.OData
/// </summary>
public bool ShowRootPath { get; set; } = false;
/// <summary>
/// Gets/sets a the path provider.
/// </summary>
public IODataPathProvider PathProvider { get; set; }
internal OpenApiConvertSettings Clone()
{
var newSettings = new OpenApiConvertSettings
@ -159,10 +195,11 @@ namespace Microsoft.OpenApi.OData
EnableDiscriminatorValue = this.EnableDiscriminatorValue,
EnableDerivedTypesReferencesForResponses = this.EnableDerivedTypesReferencesForResponses,
EnableDerivedTypesReferencesForRequestBody = this.EnableDerivedTypesReferencesForRequestBody,
PathPrefix = this.PathPrefix,
RoutePathPrefixProvider = this.RoutePathPrefixProvider,
ShowLinks = this.ShowLinks,
ShowSchemaExamples = this.ShowSchemaExamples,
ShowRootPath = this.ShowRootPath
ShowRootPath = this.ShowRootPath,
PathProvider = this.PathProvider
};
return newSettings;

View file

@ -128,6 +128,15 @@ namespace Microsoft.OpenApi.OData.Operation
}
AppendCustomParameters(operation);
// Add the route prefix parameter v1{data}
if (Context.Settings.RoutePathPrefixProvider != null && Context.Settings.RoutePathPrefixProvider.Parameters != null)
{
foreach (var parameter in Context.Settings.RoutePathPrefixProvider.Parameters)
{
operation.Parameters.Add(parameter);
}
}
}
/// <summary>

View file

@ -95,7 +95,6 @@ namespace Microsoft.OpenApi.OData.Generator.Tests
Assert.Contains("/some/prefix/Me", paths.Keys);
}
[Fact]
public void CreatePathsReturnsForContractModelWithHierarhicalClass()
{