-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add support for OpenApi v3 * fix handling of relative url
- Loading branch information
1 parent
5d513d6
commit 3e53127
Showing
11 changed files
with
1,118 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace MMLib.SwaggerForOcelot | ||
{ | ||
/// <summary> | ||
/// Swagger properties constants. | ||
/// </summary> | ||
public static class OpenApiProperties | ||
{ | ||
/// <summary> | ||
/// The servers property name. | ||
/// </summary> | ||
public const string Servers = "servers"; | ||
|
||
/// <summary> | ||
/// The uri property name. Property is a child of <seealso cref="Servers"/>. | ||
/// </summary> | ||
public const string Url = "url"; | ||
|
||
/// <summary> | ||
/// The paths property name. | ||
/// </summary> | ||
public const string Paths = "paths"; | ||
|
||
/// <summary> | ||
/// The components property name. | ||
/// </summary> | ||
public const string Components = "components"; | ||
|
||
/// <summary> | ||
/// The schemas property name. Property is a child of <seealso cref="Components"/>. | ||
/// </summary> | ||
public const string Schemas = "schemas"; | ||
|
||
/// <summary> | ||
/// The tags property name. | ||
/// </summary> | ||
public const string Tags = "tags"; | ||
|
||
/// <summary> | ||
/// The tag's name property name. Property is a child of <seealso cref="Tags"/>. | ||
/// </summary> | ||
public const string TagName = "name"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
tests/MMLib.SwaggerForOcelot.Tests/OpenApiJsonFormatterShould.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using MMLib.SwaggerForOcelot.Configuration; | ||
using MMLib.SwaggerForOcelot.Transformation; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace MMLib.SwaggerForOcelot.Tests | ||
{ | ||
public class OpenApiJsonFormatterShould | ||
{ | ||
[Fact] | ||
public async Task CreateNewJsonByBasicConfiguration() | ||
{ | ||
var reroutes = new List<ReRouteOptions> | ||
{ | ||
new ReRouteOptions | ||
{ | ||
SwaggerKey = "projects", | ||
UpstreamPathTemplate ="/api/projects/{everything}", | ||
DownstreamPathTemplate ="/api/{everything}"} | ||
}; | ||
|
||
await TransformAndCheck(reroutes, "OpenApiBase", "OpenApiBaseTransformed", "localhost:8000"); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateNewJsonByBasicConfigurationWithVirtualDirectory() | ||
{ | ||
var reroutes = new List<ReRouteOptions> | ||
{ | ||
new ReRouteOptions | ||
{ | ||
SwaggerKey = "projects", | ||
VirtualDirectory = "/project", | ||
UpstreamPathTemplate ="/api/projects/{everything}", | ||
DownstreamPathTemplate ="/project/api/{everything}"} | ||
}; | ||
|
||
await TransformAndCheck(reroutes, "OpenApiBase", "OpenApiBaseTransformed", "localhost:8000"); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateNewJsonWithServers() | ||
{ | ||
var reroutes = new List<ReRouteOptions> | ||
{ | ||
new ReRouteOptions | ||
{ | ||
SwaggerKey = "projects", | ||
UpstreamPathTemplate ="/api/projects/{everything}", | ||
DownstreamPathTemplate ="/api/{everything}" | ||
} | ||
}; | ||
|
||
await TransformAndCheck(reroutes, "OpenApiWithServersBase", "OpenApiWithServersBaseTransformed"); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateNewJsonWithHostOverride() | ||
{ | ||
var reroutes = new List<ReRouteOptions> | ||
{ | ||
new ReRouteOptions | ||
{ | ||
SwaggerKey = "projects", | ||
UpstreamPathTemplate ="/api/projects/{everything}", | ||
DownstreamPathTemplate ="/api/{everything}" | ||
} | ||
}; | ||
|
||
await TransformAndCheck(reroutes, "OpenApiWithServersBase", "OpenApiWithHostOverrideBaseTransformed", "http://override.host.it"); | ||
} | ||
|
||
private async Task TransformAndCheck( | ||
IEnumerable<ReRouteOptions> reroutes, | ||
string openApiBaseFileName, | ||
string expectedOpenApiFileName, | ||
string servers = "") | ||
{ | ||
var transformer = new SwaggerJsonTransformer(); | ||
string openApiBase = await GetBaseOpenApi(openApiBaseFileName); | ||
|
||
var transformed = transformer.Transform(openApiBase, reroutes, servers); | ||
|
||
await AreEqual(transformed, expectedOpenApiFileName); | ||
} | ||
|
||
private static async Task<string> GetBaseOpenApi(string openApiName) | ||
=> await AssemblyHelper.GetStringFromResourceFileAsync($"{openApiName}.json"); | ||
|
||
private static async Task AreEqual(string transformed, string expectedOpenApiFileName) | ||
{ | ||
var transformedJson = JObject.Parse(transformed); | ||
var expectedJson = JObject.Parse(await AssemblyHelper | ||
.GetStringFromResourceFileAsync($"{expectedOpenApiFileName}.json")); | ||
|
||
JObject.DeepEquals(transformedJson, expectedJson) | ||
.Should() | ||
.BeTrue(); | ||
} | ||
} | ||
} |
Oops, something went wrong.