From d0595f8f991fef9c3e385a4d8d4a6d0638c48e83 Mon Sep 17 00:00:00 2001 From: Torben Burchgart Date: Fri, 20 Dec 2024 12:37:37 +0100 Subject: [PATCH] Refactor OpenAPIRecord creation to reduce duplicate calls and improve readability. Update tests to ensure correct description handling and modify HTML overview to display original Swagger version if available. --- .../openapi/serviceproxy/OpenAPIRecordFactory.java | 8 +++++--- core/src/main/resources/openapi/overview.html | 7 ++++++- .../openapi/serviceproxy/OpenAPIRecordFactoryTest.java | 10 ++-------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIRecordFactory.java b/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIRecordFactory.java index 306177b848..86723370ee 100644 --- a/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIRecordFactory.java +++ b/core/src/main/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIRecordFactory.java @@ -140,13 +140,15 @@ String getUniqueId(Map apiRecords, OpenAPIRecord rec) { } private OpenAPIRecord create(OpenAPISpec spec) throws IOException { - OpenAPIRecord record = new OpenAPIRecord(getOpenAPI(spec), getSpec(getOpenAPI(spec)), spec); + OpenAPI api = getOpenAPI(spec); + OpenAPIRecord record = new OpenAPIRecord(api, getSpec(getOpenAPI(spec)), spec); setExtensionOnAPI(spec, record.api); return record; } private OpenAPIRecord create(OpenAPISpec spec, File file) throws IOException { - OpenAPIRecord record = new OpenAPIRecord(parseFileAsOpenAPI(file), getSpec(parseFileAsOpenAPI(file)), spec); + OpenAPI api = parseFileAsOpenAPI(file); + OpenAPIRecord record = new OpenAPIRecord(api, getSpec(parseFileAsOpenAPI(file)), spec); setExtensionOnAPI(spec, record.api); return record; } @@ -189,7 +191,7 @@ private void addConversionNoticeIfSwagger2(OpenAPI api, JsonNode node) { StringBuilder builder = new StringBuilder(); builder.append(api.getInfo().getDescription()); if (api.getInfo().getDescription() != null) builder.append("\n\n"); - builder.append("OpenAPI description was converted to OAS 3 from Swagger 2 by Membrane API Gateway."); + builder.append("***Note:*** *OpenAPI description was converted to OAS 3 from Swagger 2 by Membrane API Gateway.*"); api.getInfo().setDescription(builder.toString()); } } diff --git a/core/src/main/resources/openapi/overview.html b/core/src/main/resources/openapi/overview.html index 5d10e17f05..ebdf8358ff 100644 --- a/core/src/main/resources/openapi/overview.html +++ b/core/src/main/resources/openapi/overview.html @@ -97,7 +97,12 @@

APIs

<%= api.value.api.info.version %> - <%= api.value.api.openapi %> + + <%= api.value.api.openapi %> + <% if (api.value.api.extensions?.'x-original-swagger-version') { %> + (from <%= api.value.api.extensions.'x-original-swagger-version' %>) + <% } %> + <%= api.key %> <% } %> diff --git a/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIRecordFactoryTest.java b/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIRecordFactoryTest.java index b6ad3b4b74..eb43e5d891 100644 --- a/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIRecordFactoryTest.java +++ b/core/src/test/java/com/predic8/membrane/core/openapi/serviceproxy/OpenAPIRecordFactoryTest.java @@ -81,15 +81,9 @@ void swagger2ConversionNoticeAddedWithExistingDescription() throws IOException { @Test void openapi3NoConversionNoticeAdded() throws IOException { OpenAPIRecord rec = getOpenAPIRecord("fruitshop-api-v2-openapi-3.yml", "fruit-shop-api-v2-0-0"); - assertEquals(""" - ![Logo](https://www.predic8.de/logo6.png) - - Showcases REST API design and serves as a public API for - educational usage. Feel free to use this API even by using the POST, PUT and DELETE methods. You - cannot do any harm, the API will be reset automatically. - """, + assertFalse("OpenAPI description was converted to OAS 3 from Swagger 2 by Membrane API Gateway.".contains( rec.api.getInfo().getDescription() - ); + )); } @Test