From 3e53127d16463622b568f87f88890650c5808e2f Mon Sep 17 00:00:00 2001 From: Dana Desrosiers Date: Thu, 13 Jun 2019 21:40:33 -0700 Subject: [PATCH] Add support for OpenApi v3 (#33) * add support for OpenApi v3 * fix handling of relative url --- .../OpenApiProperties.cs | 43 ++++ .../Transformation/SwaggerJsonTransformer.cs | 72 ++++++- .../BuilderExtensionsShould.cs | 2 +- .../MMLib.SwaggerForOcelot.Tests.csproj | 5 + .../OpenApiJsonFormatterShould.cs | 104 ++++++++++ .../Resources/OpenApiBase.json | 172 ++++++++++++++++ .../Resources/OpenApiBaseTransformed.json | 172 ++++++++++++++++ ...penApiWithHostOverrideBaseTransformed.json | 183 ++++++++++++++++++ .../Resources/OpenApiWithServersBase.json | 183 ++++++++++++++++++ .../OpenApiWithServersBaseTransformed.json | 183 ++++++++++++++++++ .../SwaggerJsonFormatterShould.cs | 12 +- 11 files changed, 1118 insertions(+), 13 deletions(-) create mode 100644 src/MMLib.SwaggerForOcelot/OpenApiProperties.cs create mode 100644 tests/MMLib.SwaggerForOcelot.Tests/OpenApiJsonFormatterShould.cs create mode 100644 tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiBase.json create mode 100644 tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiBaseTransformed.json create mode 100644 tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithHostOverrideBaseTransformed.json create mode 100644 tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithServersBase.json create mode 100644 tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithServersBaseTransformed.json diff --git a/src/MMLib.SwaggerForOcelot/OpenApiProperties.cs b/src/MMLib.SwaggerForOcelot/OpenApiProperties.cs new file mode 100644 index 0000000..0241e8d --- /dev/null +++ b/src/MMLib.SwaggerForOcelot/OpenApiProperties.cs @@ -0,0 +1,43 @@ +namespace MMLib.SwaggerForOcelot +{ + /// + /// Swagger properties constants. + /// + public static class OpenApiProperties + { + /// + /// The servers property name. + /// + public const string Servers = "servers"; + + /// + /// The uri property name. Property is a child of . + /// + public const string Url = "url"; + + /// + /// The paths property name. + /// + public const string Paths = "paths"; + + /// + /// The components property name. + /// + public const string Components = "components"; + + /// + /// The schemas property name. Property is a child of . + /// + public const string Schemas = "schemas"; + + /// + /// The tags property name. + /// + public const string Tags = "tags"; + + /// + /// The tag's name property name. Property is a child of . + /// + public const string TagName = "name"; + } +} diff --git a/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.cs b/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.cs index 706f092..982cc80 100644 --- a/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.cs +++ b/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Newtonsoft.Json; namespace MMLib.SwaggerForOcelot.Transformation { @@ -13,11 +14,26 @@ namespace MMLib.SwaggerForOcelot.Transformation /// public class SwaggerJsonTransformer : ISwaggerJsonTransformer { - /// public string Transform(string swaggerJson, IEnumerable reRoutes, string hostOverride) { JObject swagger = JObject.Parse(swaggerJson); + + if (swagger.ContainsKey("swagger")) + { + return TransformSwagger(swagger, reRoutes, hostOverride); + } + + if (swagger.ContainsKey("openapi")) + { + return TransformOpenApi(swagger, reRoutes, hostOverride); + } + + throw new InvalidOperationException("Unknown swagger/openapi version"); + } + + private string TransformSwagger(JObject swagger, IEnumerable reRoutes, string hostOverride) + { var paths = swagger[SwaggerProperties.Paths]; var basePath = swagger.ContainsKey(SwaggerProperties.BasePath) ? swagger.GetValue(SwaggerProperties.BasePath).ToString() @@ -37,18 +53,62 @@ public string Transform(string swaggerJson, IEnumerable reRoutes RemoveItems( swagger[SwaggerProperties.Definitions], paths, - (i) => $"$..[?(@*.$ref == '#/definitions/{i.Name}')]", - (i) => $"$..[?(@*.*.items.$ref == '#/definitions/{i.Name}')]"); + i => $"$..[?(@*.$ref == '#/{SwaggerProperties.Definitions}/{i.Name}')]", + i => $"$..[?(@*.*.items.$ref == '#/{SwaggerProperties.Definitions}/{i.Name}')]"); if (swagger["tags"] != null) { RemoveItems( swagger[SwaggerProperties.Tags], paths, - (i) => $"$..tags[?(@ == '{i[SwaggerProperties.TagName]}')]"); + i => $"$..tags[?(@ == '{i[SwaggerProperties.TagName]}')]"); + } + } + + return swagger.ToString(Formatting.Indented); + } + + private string TransformOpenApi(JObject openApi, IEnumerable reRoutes, string hostOverride = "") + { + var paths = openApi[OpenApiProperties.Paths]; + if (openApi.ContainsKey(OpenApiProperties.Servers)) + { + foreach (var server in openApi.GetValue(OpenApiProperties.Servers)) + { + if (server[OpenApiProperties.Url] != null) + { + var url = new Uri(server.Value(OpenApiProperties.Url), UriKind.RelativeOrAbsolute); + server[OpenApiProperties.Url] = hostOverride + (url.IsAbsoluteUri ? url.AbsolutePath : url.OriginalString); + } + } + } + + // NOTE: Only supporting one server for now. + var basePath = ""; + if (openApi.ContainsKey(OpenApiProperties.Servers)) + { + var firstUrl = openApi.GetValue(OpenApiProperties.Servers).First.Value(OpenApiProperties.Url); + basePath = hostOverride.Length > 0 ? new Uri(firstUrl).AbsolutePath : firstUrl; + } + + if (paths != null) + { + RemovePaths(reRoutes, paths, basePath); + + RemoveItems( + openApi[OpenApiProperties.Components][OpenApiProperties.Schemas], + paths, + i => $"$..[?(@*.$ref == '#/{OpenApiProperties.Components}/{OpenApiProperties.Schemas}/{i.Name}')]", + i => $"$..[?(@*.*.items.$ref == '#/{OpenApiProperties.Components}/{OpenApiProperties.Schemas}/{i.Name}')]"); + if (openApi["tags"] != null) + { + RemoveItems( + openApi[OpenApiProperties.Tags], + paths, + i => $"$..tags[?(@ == '{i[OpenApiProperties.TagName]}')]"); } } - return swagger.ToString(Newtonsoft.Json.Formatting.Indented); + return openApi.ToString(Formatting.Indented); } private void RemovePaths(IEnumerable reRoutes, JToken paths, string basePath) @@ -188,4 +248,4 @@ private static void RenameToken(JProperty property, string newName) property.Replace(newProperty); } } -} \ No newline at end of file +} diff --git a/tests/MMLib.SwaggerForOcelot.Tests/BuilderExtensionsShould.cs b/tests/MMLib.SwaggerForOcelot.Tests/BuilderExtensionsShould.cs index f4dba77..0043cdb 100644 --- a/tests/MMLib.SwaggerForOcelot.Tests/BuilderExtensionsShould.cs +++ b/tests/MMLib.SwaggerForOcelot.Tests/BuilderExtensionsShould.cs @@ -13,7 +13,7 @@ namespace MMLib.SwaggerForOcelot.Tests public class BuilderExtensionsShould { [Fact] - public void ThrowWhenSwaggerEndPointsSectionIsMising() => TestWithInvalidConfiguration("{}"); + public void ThrowWhenSwaggerEndPointsSectionIsMissing() => TestWithInvalidConfiguration("{}"); [Fact] public void ThrowWhenSwaggerEndPointsSectionIsEmpty() diff --git a/tests/MMLib.SwaggerForOcelot.Tests/MMLib.SwaggerForOcelot.Tests.csproj b/tests/MMLib.SwaggerForOcelot.Tests/MMLib.SwaggerForOcelot.Tests.csproj index 167646d..18683bc 100644 --- a/tests/MMLib.SwaggerForOcelot.Tests/MMLib.SwaggerForOcelot.Tests.csproj +++ b/tests/MMLib.SwaggerForOcelot.Tests/MMLib.SwaggerForOcelot.Tests.csproj @@ -15,6 +15,9 @@ + + + @@ -34,6 +37,8 @@ + + diff --git a/tests/MMLib.SwaggerForOcelot.Tests/OpenApiJsonFormatterShould.cs b/tests/MMLib.SwaggerForOcelot.Tests/OpenApiJsonFormatterShould.cs new file mode 100644 index 0000000..98f2f54 --- /dev/null +++ b/tests/MMLib.SwaggerForOcelot.Tests/OpenApiJsonFormatterShould.cs @@ -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 + { + 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 + { + 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 + { + 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 + { + 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 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 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(); + } + } +} diff --git a/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiBase.json b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiBase.json new file mode 100644 index 0000000..cd300bc --- /dev/null +++ b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiBase.json @@ -0,0 +1,172 @@ +{ + "openapi": "3.0", + "info": { + "version": "v1", + "title": "Projects API" + }, + "paths": { + "/api/Projects": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "$ref": "#/components/schemas/Project" + } + } + } + } + } + }, + "/api/Projects/{id}": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "integer", + "format": "int32" + } + ], + "responses": { + "200": { + "description": "Success", + "schema": { + "$ref": "#/components/schemas/Project" + } + }, + "404": { + "description": "Not Found" + } + } + } + }, + "/api/Projects/projectCreate": { + "post": { + "tags": [ + "Projects" + ], + "operationId": "CreateProject", + "consumes": [ + "application/json-patch+json", + "application/json", + "text/json", + "application/*+json" + ], + "produces": [], + "parameters": [ + { + "name": "projectViewModel", + "in": "body", + "required": false, + "schema": { + "$ref": "#/components/schemas/ProjectViewModel" + } + } + ], + "responses": { + "201": { + "description": "Success" + }, + "400": { + "description": "Bad Request" + } + } + } + }, + "/api/Values": { + "get": { + "tags": [ + "Values" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Project": { + "type": "object", + "properties": { + "id": { + "format": "int32", + "type": "integer" + }, + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "owner": { + "type": "string" + } + } + }, + "ProjectViewModel": { + "required": [ + "name", + "owner" + ], + "type": "object", + "properties": { + "name": { + "maxLength": 50, + "type": "string" + }, + "description": { + "maxLength": 255, + "type": "string" + }, + "owner": { + "type": "string" + } + } + } + } + } +} diff --git a/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiBaseTransformed.json b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiBaseTransformed.json new file mode 100644 index 0000000..23252bc --- /dev/null +++ b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiBaseTransformed.json @@ -0,0 +1,172 @@ +{ + "openapi": "3.0", + "info": { + "version": "v1", + "title": "Projects API" + }, + "paths": { + "/api/projects/Projects": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "$ref": "#/components/schemas/Project" + } + } + } + } + } + }, + "/api/projects/Projects/{id}": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "integer", + "format": "int32" + } + ], + "responses": { + "200": { + "description": "Success", + "schema": { + "$ref": "#/components/schemas/Project" + } + }, + "404": { + "description": "Not Found" + } + } + } + }, + "/api/projects/Projects/projectCreate": { + "post": { + "tags": [ + "Projects" + ], + "operationId": "CreateProject", + "consumes": [ + "application/json-patch+json", + "application/json", + "text/json", + "application/*+json" + ], + "produces": [], + "parameters": [ + { + "name": "projectViewModel", + "in": "body", + "required": false, + "schema": { + "$ref": "#/components/schemas/ProjectViewModel" + } + } + ], + "responses": { + "201": { + "description": "Success" + }, + "400": { + "description": "Bad Request" + } + } + } + }, + "/api/projects/Values": { + "get": { + "tags": [ + "Values" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Project": { + "type": "object", + "properties": { + "id": { + "format": "int32", + "type": "integer" + }, + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "owner": { + "type": "string" + } + } + }, + "ProjectViewModel": { + "required": [ + "name", + "owner" + ], + "type": "object", + "properties": { + "name": { + "maxLength": 50, + "type": "string" + }, + "description": { + "maxLength": 255, + "type": "string" + }, + "owner": { + "type": "string" + } + } + } + } + } +} diff --git a/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithHostOverrideBaseTransformed.json b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithHostOverrideBaseTransformed.json new file mode 100644 index 0000000..72bc09d --- /dev/null +++ b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithHostOverrideBaseTransformed.json @@ -0,0 +1,183 @@ +{ + "openapi": "3.0", + "info": { + "version": "v1", + "title": "Projects API" + }, + "servers": [ + { + "url": "http://override.host.it/api" + }, + { + "url": "http://override.host.it/" + }, + { + "url": "http://override.host.it/api2" + } + ], + "paths": { + "/projects/Projects": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "$ref": "#/components/schemas/Project" + } + } + } + } + } + }, + "/projects/Projects/{id}": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "integer", + "format": "int32" + } + ], + "responses": { + "200": { + "description": "Success", + "schema": { + "$ref": "#/components/schemas/Project" + } + }, + "404": { + "description": "Not Found" + } + } + } + }, + "/projects/Projects/projectCreate": { + "post": { + "tags": [ + "Projects" + ], + "operationId": "CreateProject", + "consumes": [ + "application/json-patch+json", + "application/json", + "text/json", + "application/*+json" + ], + "produces": [], + "parameters": [ + { + "name": "projectViewModel", + "in": "body", + "required": false, + "schema": { + "$ref": "#/components/schemas/ProjectViewModel" + } + } + ], + "responses": { + "201": { + "description": "Success" + }, + "400": { + "description": "Bad Request" + } + } + } + }, + "/projects/Values": { + "get": { + "tags": [ + "Values" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Project": { + "type": "object", + "properties": { + "id": { + "format": "int32", + "type": "integer" + }, + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "owner": { + "type": "string" + } + } + }, + "ProjectViewModel": { + "required": [ + "name", + "owner" + ], + "type": "object", + "properties": { + "name": { + "maxLength": 50, + "type": "string" + }, + "description": { + "maxLength": 255, + "type": "string" + }, + "owner": { + "type": "string" + } + } + } + } + } +} diff --git a/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithServersBase.json b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithServersBase.json new file mode 100644 index 0000000..666d9e0 --- /dev/null +++ b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithServersBase.json @@ -0,0 +1,183 @@ +{ + "openapi": "3.0", + "info": { + "version": "v1", + "title": "Projects API" + }, + "servers": [ + { + "url": "http://test.it/api" + }, + { + "url": "http://dev.test.it" + }, + { + "url": "/api2" + } + ], + "paths": { + "/Projects": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "$ref": "#/components/schemas/Project" + } + } + } + } + } + }, + "/Projects/{id}": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "integer", + "format": "int32" + } + ], + "responses": { + "200": { + "description": "Success", + "schema": { + "$ref": "#/components/schemas/Project" + } + }, + "404": { + "description": "Not Found" + } + } + } + }, + "/Projects/projectCreate": { + "post": { + "tags": [ + "Projects" + ], + "operationId": "CreateProject", + "consumes": [ + "application/json-patch+json", + "application/json", + "text/json", + "application/*+json" + ], + "produces": [], + "parameters": [ + { + "name": "projectViewModel", + "in": "body", + "required": false, + "schema": { + "$ref": "#/components/schemas/ProjectViewModel" + } + } + ], + "responses": { + "201": { + "description": "Success" + }, + "400": { + "description": "Bad Request" + } + } + } + }, + "/Values": { + "get": { + "tags": [ + "Values" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Project": { + "type": "object", + "properties": { + "id": { + "format": "int32", + "type": "integer" + }, + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "owner": { + "type": "string" + } + } + }, + "ProjectViewModel": { + "required": [ + "name", + "owner" + ], + "type": "object", + "properties": { + "name": { + "maxLength": 50, + "type": "string" + }, + "description": { + "maxLength": 255, + "type": "string" + }, + "owner": { + "type": "string" + } + } + } + } + } +} diff --git a/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithServersBaseTransformed.json b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithServersBaseTransformed.json new file mode 100644 index 0000000..f86fdbf --- /dev/null +++ b/tests/MMLib.SwaggerForOcelot.Tests/Resources/OpenApiWithServersBaseTransformed.json @@ -0,0 +1,183 @@ +{ + "openapi": "3.0", + "info": { + "version": "v1", + "title": "Projects API" + }, + "servers": [ + { + "url": "/api" + }, + { + "url": "/" + }, + { + "url": "/api2" + } + ], + "paths": { + "/projects/Projects": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "$ref": "#/components/schemas/Project" + } + } + } + } + } + }, + "/projects/Projects/{id}": { + "get": { + "tags": [ + "Projects" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "integer", + "format": "int32" + } + ], + "responses": { + "200": { + "description": "Success", + "schema": { + "$ref": "#/components/schemas/Project" + } + }, + "404": { + "description": "Not Found" + } + } + } + }, + "/projects/Projects/projectCreate": { + "post": { + "tags": [ + "Projects" + ], + "operationId": "CreateProject", + "consumes": [ + "application/json-patch+json", + "application/json", + "text/json", + "application/*+json" + ], + "produces": [], + "parameters": [ + { + "name": "projectViewModel", + "in": "body", + "required": false, + "schema": { + "$ref": "#/components/schemas/ProjectViewModel" + } + } + ], + "responses": { + "201": { + "description": "Success" + }, + "400": { + "description": "Bad Request" + } + } + } + }, + "/projects/Values": { + "get": { + "tags": [ + "Values" + ], + "operationId": "Get", + "consumes": [], + "produces": [ + "text/plain", + "application/json", + "text/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Success", + "schema": { + "uniqueItems": false, + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Project": { + "type": "object", + "properties": { + "id": { + "format": "int32", + "type": "integer" + }, + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "owner": { + "type": "string" + } + } + }, + "ProjectViewModel": { + "required": [ + "name", + "owner" + ], + "type": "object", + "properties": { + "name": { + "maxLength": 50, + "type": "string" + }, + "description": { + "maxLength": 255, + "type": "string" + }, + "owner": { + "type": "string" + } + } + } + } + } +} diff --git a/tests/MMLib.SwaggerForOcelot.Tests/SwaggerJsonFormatterShould.cs b/tests/MMLib.SwaggerForOcelot.Tests/SwaggerJsonFormatterShould.cs index 74d4469..06608ed 100644 --- a/tests/MMLib.SwaggerForOcelot.Tests/SwaggerJsonFormatterShould.cs +++ b/tests/MMLib.SwaggerForOcelot.Tests/SwaggerJsonFormatterShould.cs @@ -38,7 +38,7 @@ public async Task CreateNewJsonByBasicConfigurationWithVirtualDirectory() await TransformAndCheck(reroutes, "SwaggerBase", "SwaggerBaseTransformed", "localhost:8000"); } - + [Fact] public async Task CreateNewJsonWithBasePath() { @@ -158,17 +158,17 @@ private async Task TransformAndCheck( var transformer = new SwaggerJsonTransformer(); string swaggerBase = await GetBaseSwagger(swaggerBaseFileName); - var transfomed = transformer.Transform(swaggerBase, reroutes, basePath); + var transformed = transformer.Transform(swaggerBase, reroutes, basePath); - await AreEquel(transfomed, expectedSwaggerFileName); + await AreEqual(transformed, expectedSwaggerFileName); } private static async Task GetBaseSwagger(string swaggerName) => await AssemblyHelper.GetStringFromResourceFileAsync($"{swaggerName}.json"); - private static async Task AreEquel(string transfomed, string expectedSwaggerFileName) + private static async Task AreEqual(string transformed, string expectedSwaggerFileName) { - var transformedJson = JObject.Parse(transfomed); + var transformedJson = JObject.Parse(transformed); var expectedJson = JObject.Parse(await AssemblyHelper .GetStringFromResourceFileAsync($"{expectedSwaggerFileName}.json")); @@ -177,4 +177,4 @@ private static async Task AreEquel(string transfomed, string expectedSwaggerFile .BeTrue(); } } -} \ No newline at end of file +}