From 7eb6ee25babe219a27dd1237132006457544c2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=88o=20Martiniak?= Date: Tue, 24 Aug 2021 10:50:09 +0200 Subject: [PATCH] Fix of documentation generation for aggregation (#184) * Fix generating aggregation documentation when downstrem docs doesnot contain content * new version --- .../Aggregates/RouteDocs.cs | 30 +++++++++++-------- .../MMLib.SwaggerForOcelot.csproj | 2 +- .../RoutesDocumentationProviderShould.cs | 1 + .../Resources/AggregatesOpenApiResource.json | 26 ++++++++++++++++ 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/MMLib.SwaggerForOcelot/Aggregates/RouteDocs.cs b/src/MMLib.SwaggerForOcelot/Aggregates/RouteDocs.cs index 15a3f2d..259d1a7 100644 --- a/src/MMLib.SwaggerForOcelot/Aggregates/RouteDocs.cs +++ b/src/MMLib.SwaggerForOcelot/Aggregates/RouteDocs.cs @@ -123,7 +123,7 @@ public string GetSummary() /// public OpenApiResponse GetResponse() { - var response = Docs[PathKey].SelectToken("responses.200")?.ToObject(typeof(Response), _serializer) as Response; + var response = Docs[PathKey]?.SelectToken("responses.200")?.ToObject(typeof(Response), _serializer) as Response; return new OpenApiResponse() { @@ -134,22 +134,28 @@ public OpenApiResponse GetResponse() private Dictionary CreateContent(Response response) { - OpenApiMediaTypeEx content = response?.Content[MediaTypeNames.Application.Json]; var ret = new Dictionary(); + if (response?.Content is null) + { + return ret; + } - if (content != null) + OpenApiMediaTypeEx content = response.Content[MediaTypeNames.Application.Json]; + if (content == null) { - if (!content.SchemaExt.IsReference) - { - content.SetSchema(); - } - else - { - FindSchema(content); - } + return ret; + } - ret.Add(MediaTypeNames.Application.Json, content); + if (!content.SchemaExt.IsReference) + { + content.SetSchema(); } + else + { + FindSchema(content); + } + + ret.Add(MediaTypeNames.Application.Json, content); return ret; } diff --git a/src/MMLib.SwaggerForOcelot/MMLib.SwaggerForOcelot.csproj b/src/MMLib.SwaggerForOcelot/MMLib.SwaggerForOcelot.csproj index df03856..87aac44 100644 --- a/src/MMLib.SwaggerForOcelot/MMLib.SwaggerForOcelot.csproj +++ b/src/MMLib.SwaggerForOcelot/MMLib.SwaggerForOcelot.csproj @@ -2,7 +2,7 @@ net5.0 - 4.4.1 + 4.4.2 Milan Martiniak MMLib Swagger generator for Ocelot downstream services. diff --git a/tests/MMLib.SwaggerForOcelot.Tests/Aggregates/RoutesDocumentationProviderShould.cs b/tests/MMLib.SwaggerForOcelot.Tests/Aggregates/RoutesDocumentationProviderShould.cs index 4cf9261..8ce4d1d 100644 --- a/tests/MMLib.SwaggerForOcelot.Tests/Aggregates/RoutesDocumentationProviderShould.cs +++ b/tests/MMLib.SwaggerForOcelot.Tests/Aggregates/RoutesDocumentationProviderShould.cs @@ -20,6 +20,7 @@ public class RoutesDocumentationProviderShould [InlineData("serviceWithMoreSimpleParameter1", "serviceWithMoreSimpleParameter2", "/{id}/{message}")] [InlineData("serviceWithMoreSimpleParameter1", "serviceWithMoreSimpleParameterAnotherNames", "/{id}/{message}", "{id}-{idcko};{message}-{sprava}")] [InlineData("moreMethod1", "moreMethod2")] + [InlineData("withoutcontent1", "withoutcontent2")] public async Task GetDocs(string firstService, string secondService, string parameters = null, string paramsMap = null) { RoutesDocumentationProvider provider = await CreateProviderAsync(); diff --git a/tests/MMLib.SwaggerForOcelot.Tests/Resources/AggregatesOpenApiResource.json b/tests/MMLib.SwaggerForOcelot.Tests/Resources/AggregatesOpenApiResource.json index a97c888..b09b160 100644 --- a/tests/MMLib.SwaggerForOcelot.Tests/Resources/AggregatesOpenApiResource.json +++ b/tests/MMLib.SwaggerForOcelot.Tests/Resources/AggregatesOpenApiResource.json @@ -125,6 +125,32 @@ } } }, + "/api/withoutcontent1/endpoint1": { + "get": { + "tags": [ + "withoutcontent1" + ], + "summary": "withoutcontent1 - endpoint 1", + "responses": { + "200": { + "description": "Success" + } + } + } + }, + "/api/withoutcontent2/endpoint1": { + "get": { + "tags": [ + "withoutcontent2" + ], + "summary": "withoutcontent2 - endpoint 1", + "responses": { + "200": { + "description": "Success" + } + } + } + }, "/api/serviceWithSimpleParameter1/endpoint1/{id}": { "get": { "tags": [