Skip to content

Commit 1643ba5

Browse files
committed
Add Path provider and path prefix provider into setting
1 parent 5a83e6b commit 1643ba5

File tree

6 files changed

+103
-5
lines changed

6 files changed

+103
-5
lines changed

src/Microsoft.OpenApi.OData.Reader/Edm/ODataContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public ODataContext(IEdmModel model, OpenApiConvertSettings settings)
4949
OperationHanderProvider = new OperationHandlerProvider();
5050
PathItemHanderProvider = new PathItemHandlerProvider();
5151

52-
_pathProvider = new ODataPathProvider();
52+
// If no path provider, use the default path provider.
53+
_pathProvider = settings.PathProvider ?? new ODataPathProvider();
5354

5455
if (settings.EnableKeyAsSegment != null)
5556
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using System.Collections.Generic;
7+
using Microsoft.OpenApi.Models;
8+
9+
namespace Microsoft.OpenApi.OData.Extensions
10+
{
11+
/// <summary>
12+
/// The interface for route prefix.
13+
/// </summary>
14+
public interface IODataRoutePathPrefixProvider
15+
{
16+
/// <summary>
17+
/// The route prefix.
18+
/// </summary>
19+
public string PathPrefix { get; }
20+
21+
/// <summary>
22+
/// The route prefix parameters.
23+
/// </summary>
24+
public IEnumerable<OpenApiParameter> Parameters { get; }
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using System.Collections.Generic;
7+
using Microsoft.OpenApi.Models;
8+
9+
namespace Microsoft.OpenApi.OData.Extensions
10+
{
11+
/// <summary>
12+
/// Default implementation of <see cref="IODataRoutePathPrefixProvider"/>.
13+
/// </summary>
14+
public class ODataRoutePathPrefixProvider : IODataRoutePathPrefixProvider
15+
{
16+
/// <summary>
17+
/// Gets/sets the path prefix.
18+
/// </summary>
19+
public string PathPrefix { get; set; }
20+
21+
/// <summary>
22+
/// Gets/sets the associated parameters for the path prefix.
23+
/// </summary>
24+
public IEnumerable<OpenApiParameter> Parameters { get; set; }
25+
}
26+
}

src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// ------------------------------------------------------------
55

66
using System;
7+
using Microsoft.OpenApi.OData.Common;
8+
using Microsoft.OpenApi.OData.Edm;
9+
using Microsoft.OpenApi.OData.Extensions;
710

811
namespace Microsoft.OpenApi.OData
912
{
@@ -118,7 +121,35 @@ public class OpenApiConvertSettings
118121
/// <summary>
119122
/// Gets/sets a value that specifies a prefix to be prepended to all generated paths.
120123
/// </summary>
121-
public string PathPrefix { get; set; } = string.Empty;
124+
public string PathPrefix
125+
{
126+
get
127+
{
128+
if (RoutePathPrefixProvider != null)
129+
{
130+
return RoutePathPrefixProvider.PathPrefix;
131+
}
132+
133+
return null;
134+
}
135+
set
136+
{
137+
if (string.IsNullOrWhiteSpace(value))
138+
{
139+
throw Error.ArgumentNullOrEmpty("value");
140+
}
141+
142+
RoutePathPrefixProvider = new ODataRoutePathPrefixProvider
143+
{
144+
PathPrefix = value
145+
};
146+
}
147+
}
148+
149+
/// <summary>
150+
/// Gets/sets a route path prefix provider.
151+
/// </summary>
152+
public IODataRoutePathPrefixProvider RoutePathPrefixProvider { get; set; }
122153

123154
/// <summary>
124155
/// Gets/Sets a value indicating whether or not to show the OpenAPI links in the responses.
@@ -135,6 +166,11 @@ public class OpenApiConvertSettings
135166
/// </summary>
136167
public bool ShowRootPath { get; set; } = false;
137168

169+
/// <summary>
170+
/// Gets/sets a the path provider.
171+
/// </summary>
172+
public IODataPathProvider PathProvider { get; set; }
173+
138174
internal OpenApiConvertSettings Clone()
139175
{
140176
var newSettings = new OpenApiConvertSettings
@@ -159,10 +195,11 @@ internal OpenApiConvertSettings Clone()
159195
EnableDiscriminatorValue = this.EnableDiscriminatorValue,
160196
EnableDerivedTypesReferencesForResponses = this.EnableDerivedTypesReferencesForResponses,
161197
EnableDerivedTypesReferencesForRequestBody = this.EnableDerivedTypesReferencesForRequestBody,
162-
PathPrefix = this.PathPrefix,
198+
RoutePathPrefixProvider = this.RoutePathPrefixProvider,
163199
ShowLinks = this.ShowLinks,
164200
ShowSchemaExamples = this.ShowSchemaExamples,
165-
ShowRootPath = this.ShowRootPath
201+
ShowRootPath = this.ShowRootPath,
202+
PathProvider = this.PathProvider
166203
};
167204

168205
return newSettings;

src/Microsoft.OpenApi.OData.Reader/Operation/OperationHandler.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ protected virtual void SetParameters(OpenApiOperation operation)
128128
}
129129

130130
AppendCustomParameters(operation);
131+
132+
// Add the route prefix parameter v1{data}
133+
if (Context.Settings.RoutePathPrefixProvider != null && Context.Settings.RoutePathPrefixProvider.Parameters != null)
134+
{
135+
foreach (var parameter in Context.Settings.RoutePathPrefixProvider.Parameters)
136+
{
137+
operation.Parameters.Add(parameter);
138+
}
139+
}
131140
}
132141

133142
/// <summary>

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiPathsGeneratorTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ public void CreatePathsReturnsForBasicModelWithPrefix()
9595
Assert.Contains("/some/prefix/Me", paths.Keys);
9696
}
9797

98-
9998
[Fact]
10099
public void CreatePathsReturnsForContractModelWithHierarhicalClass()
101100
{

0 commit comments

Comments
 (0)