From 1643ba5ea1af7cf2c2dae0233f95b49eba5b646d Mon Sep 17 00:00:00 2001 From: Sam Xu Date: Mon, 8 Feb 2021 19:57:15 -0800 Subject: [PATCH] Add Path provider and path prefix provider into setting --- .../Edm/ODataContext.cs | 3 +- .../IODataRoutePathPrefixProvider.cs | 26 +++++++++++ .../ODataRoutePathPrefixProvider.cs | 26 +++++++++++ .../OpenApiConvertSettings.cs | 43 +++++++++++++++++-- .../Operation/OperationHandler.cs | 9 ++++ .../Generator/OpenApiPathsGeneratorTests.cs | 1 - 6 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 src/Microsoft.OpenApi.OData.Reader/Extensions/IODataRoutePathPrefixProvider.cs create mode 100644 src/Microsoft.OpenApi.OData.Reader/Extensions/ODataRoutePathPrefixProvider.cs diff --git a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataContext.cs b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataContext.cs index a82677c..cba4b32 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Edm/ODataContext.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Edm/ODataContext.cs @@ -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) { diff --git a/src/Microsoft.OpenApi.OData.Reader/Extensions/IODataRoutePathPrefixProvider.cs b/src/Microsoft.OpenApi.OData.Reader/Extensions/IODataRoutePathPrefixProvider.cs new file mode 100644 index 0000000..22b3339 --- /dev/null +++ b/src/Microsoft.OpenApi.OData.Reader/Extensions/IODataRoutePathPrefixProvider.cs @@ -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 +{ + /// + /// The interface for route prefix. + /// + public interface IODataRoutePathPrefixProvider + { + /// + /// The route prefix. + /// + public string PathPrefix { get; } + + /// + /// The route prefix parameters. + /// + public IEnumerable Parameters { get; } + } +} diff --git a/src/Microsoft.OpenApi.OData.Reader/Extensions/ODataRoutePathPrefixProvider.cs b/src/Microsoft.OpenApi.OData.Reader/Extensions/ODataRoutePathPrefixProvider.cs new file mode 100644 index 0000000..61b4092 --- /dev/null +++ b/src/Microsoft.OpenApi.OData.Reader/Extensions/ODataRoutePathPrefixProvider.cs @@ -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 +{ + /// + /// Default implementation of . + /// + public class ODataRoutePathPrefixProvider : IODataRoutePathPrefixProvider + { + /// + /// Gets/sets the path prefix. + /// + public string PathPrefix { get; set; } + + /// + /// Gets/sets the associated parameters for the path prefix. + /// + public IEnumerable Parameters { get; set; } + } +} diff --git a/src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs b/src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs index 8cfd511..74df7cf 100644 --- a/src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs +++ b/src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs @@ -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 /// /// Gets/sets a value that specifies a prefix to be prepended to all generated paths. /// - 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 + }; + } + } + + /// + /// Gets/sets a route path prefix provider. + /// + public IODataRoutePathPrefixProvider RoutePathPrefixProvider { get; set; } /// /// Gets/Sets a value indicating whether or not to show the OpenAPI links in the responses. @@ -135,6 +166,11 @@ namespace Microsoft.OpenApi.OData /// public bool ShowRootPath { get; set; } = false; + /// + /// Gets/sets a the path provider. + /// + 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; diff --git a/src/Microsoft.OpenApi.OData.Reader/Operation/OperationHandler.cs b/src/Microsoft.OpenApi.OData.Reader/Operation/OperationHandler.cs index d035434..2a81b1f 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Operation/OperationHandler.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Operation/OperationHandler.cs @@ -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); + } + } } /// diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiPathsGeneratorTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiPathsGeneratorTests.cs index b36f505..ac03f72 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiPathsGeneratorTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiPathsGeneratorTests.cs @@ -95,7 +95,6 @@ namespace Microsoft.OpenApi.OData.Generator.Tests Assert.Contains("/some/prefix/Me", paths.Keys); } - [Fact] public void CreatePathsReturnsForContractModelWithHierarhicalClass() {