Skip to content

Commit

Permalink
Refactor OpenAPIRecord creation to reduce duplicate calls and improve…
Browse files Browse the repository at this point in the history
… readability. Update tests to ensure correct description handling and modify HTML overview to display original Swagger version if available.
  • Loading branch information
t-burch committed Dec 20, 2024
1 parent 2c7706d commit d0595f8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@ String getUniqueId(Map<String, OpenAPIRecord> 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;
}
Expand Down Expand Up @@ -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());
}
}
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/resources/openapi/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ <h1 class="title">APIs</h1>

</td>
<td><%= api.value.api.info.version %></td>
<td><%= api.value.api.openapi %></td>
<td>
<%= api.value.api.openapi %>
<% if (api.value.api.extensions?.'x-original-swagger-version') { %>
(from <%= api.value.api.extensions.'x-original-swagger-version' %>)
<% } %>
</td>
<td><a href="${path}/${api.key}"><%= api.key %></a></td>
</tr>
<% } %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d0595f8

Please sign in to comment.