Skip to content

Commit 27d7d61

Browse files
authored
Refactor enum converters (#2389)
Fixed #2371
1 parent 72408ae commit 27d7d61

File tree

9 files changed

+82
-7
lines changed

9 files changed

+82
-7
lines changed

openapi-adoc/src/main/java/io/micronaut/openapi/adoc/utils/SwaggerUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static OpenAPI readOpenApi(String swaggerFileContent, boolean isJson) {
5757
public static OpenAPI readOpenApiFromLocation(String location) {
5858

5959
var isJson = location.endsWith(".json");
60-
var adjustedLocation = location.replace("\\\\", "/").replace("\\", "/").toLowerCase();
60+
var adjustedLocation = location.replace("\\\\", "/").replace("\\", "/");
6161
try {
6262
if (adjustedLocation.startsWith("jar:")) {
6363
try (var in = new URI(adjustedLocation).toURL().openStream()) {

openapi-adoc/src/main/resources/template/content.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<#list content as mediaTypeName, mediaType>
44
<#if mediaType.getExample()?has_content><#assign example = mediaType.getExample() /><#include template_examples /></#if>
55
<#assign schemaType = mediaType.getSchema() />
6-
${mediaTypeName}:: <#include template_schemaType />
6+
${mediaTypeName}:: <#include template_schemaType /><@renderSchemaType schemaType=schemaType />
77

88
<#assign propSchema = schemaType />
99
<#include template_propertyDescription />

openapi-adoc/src/main/resources/template/headers.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ __explode__
3535
</#if>
3636
</#compress>
3737

38-
<.<!<#assign schemaType = header.getSchema() /><#include template_schemaType />
38+
<.<!<#assign schemaType = header.getSchema() /><#include template_schemaType /><@renderSchemaType schemaType=schemaType />
3939
</#list>
4040
!===
4141
</#if>

openapi-adoc/src/main/resources/template/parameters.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Style: ${parameter.getStyle()} +
5050
<#assign content = parameter.getContent() />
5151
<#include template_content />
5252
</#if>
53-
<.<|<#if parameter.getSchema()??><#assign schemaType = parameter.getSchema() /><#include template_schemaType /></#if>
53+
<.<|<#if parameter.getSchema()??><#assign schemaType = parameter.getSchema() /><#include template_schemaType /><@renderSchemaType schemaType=schemaType /></#if>
5454
</#list>
5555
|===
5656
</#if>

openapi-adoc/src/main/resources/template/properties.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
__required__
1313
</#if>
1414
<.<|<#include template_propertyDescription />
15-
<.<|<#assign schemaType = propSchema /><#include template_schemaType />
15+
<.<|<#assign schemaType = propSchema /><#include template_schemaType /><@renderSchemaType schemaType=schemaType />
1616
</#list>
1717
|===
1818
</#if>

openapi-adoc/src/main/resources/template/schemaType.ftl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<#macro renderSchemaType schemaType>
12
<#compress>
23
<#if schemaType.getItems()??>
34
<#if schemaType.getItems().getType()??>
@@ -13,10 +14,17 @@ enum (${schemaType.getEnum()?join(", ")})
1314
<#if schemaType.getType()??>
1415
${schemaType.getType()}
1516
<#else>
17+
<#if schemaType.get$ref()??>
1618
<<${schemaType.get$ref()?replace("#", "")?replace("/", "_")},${schemaType.get$ref()?remove_beginning("#/components/schemas/")}>>
19+
<#else>
20+
<#list schemaType.getAllOf() as allOfSchema>
21+
<@renderSchemaType schemaType=allOfSchema />
22+
</#list>
23+
</#if>
1724
</#if>
1825
<#if schemaType.getFormat()?has_content>
1926
(${schemaType.getFormat()})
2027
</#if>
2128
</#if>
2229
</#compress>
30+
</#macro>

openapi-adoc/src/test/java/io/micronaut/openapi/adoc/OpenApiToAdocConverterTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.micronaut.openapi.adoc;
22

33
import freemarker.template.TemplateException;
4+
import org.junit.jupiter.api.AfterEach;
45
import org.junit.jupiter.api.BeforeEach;
56
import org.junit.jupiter.api.Test;
67

@@ -17,7 +18,7 @@ class OpenApiToAdocConverterTest {
1718
final Path outputDir = Path.of("build/test/freemarker");
1819

1920
@BeforeEach
20-
void setup() throws IOException {
21+
void beforeEach() throws IOException {
2122
if (Files.exists(outputDir)) {
2223
Files.walk(outputDir)
2324
.sorted(Comparator.reverseOrder())
@@ -26,10 +27,27 @@ void setup() throws IOException {
2627
}
2728
}
2829

30+
@AfterEach
31+
void afterEach() throws IOException {
32+
if (Files.exists(outputDir)) {
33+
Files.walk(outputDir)
34+
.sorted(Comparator.reverseOrder())
35+
.map(Path::toFile)
36+
.forEach(File::delete);
37+
}
38+
39+
System.clearProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_ENABLED);
40+
System.clearProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OPENAPI_PATH);
41+
System.clearProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OUTPUT_FILENAME);
42+
System.clearProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OUTPUT_DIR_PATH);
43+
System.clearProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_TEMPLATES_DIR_PATH);
44+
System.clearProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_TEMPLATE_PREFIX + "links");
45+
}
46+
2947
@Test
3048
void testFreemarker() throws IOException, TemplateException {
3149

32-
System.setProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OPENAPI_PATH, "/yaml/swagger_petstore.yaml");
50+
System.setProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OPENAPI_PATH, "/yaml/swagger_petstore.yml");
3351
System.setProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OUTPUT_FILENAME, "myresult.adoc");
3452
System.setProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OUTPUT_DIR_PATH, outputDir.toString());
3553
System.setProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_TEMPLATES_DIR_PATH, "classpath:/customDir");
@@ -43,4 +61,20 @@ void testFreemarker() throws IOException, TemplateException {
4361
var adoc = Files.readString(resultFile);
4462
assertTrue(adoc.contains("!!!!!!test custom template"));
4563
}
64+
65+
@Test
66+
void testSchemaWithAllOf() throws IOException, TemplateException {
67+
68+
System.setProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OPENAPI_PATH, "/yaml/schemaWithAllOf.yml");
69+
System.setProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OUTPUT_FILENAME, "myresult.adoc");
70+
System.setProperty(OpenApiToAdocConfigProperty.MICRONAUT_OPENAPI_ADOC_OUTPUT_DIR_PATH, outputDir.toString());
71+
72+
OpenApiToAdocConverter.convert();
73+
74+
var resultFile = outputDir.resolve("myresult.adoc");
75+
assertTrue(Files.exists(resultFile));
76+
77+
var adoc = Files.readString(resultFile);
78+
assertTrue(adoc.contains("<.<|<<_components_schemas_MyDto,MyDto>>"));
79+
}
4680
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Service
4+
version: 1.0.0
5+
paths:
6+
/:
7+
get:
8+
operationId: fetchOffers
9+
parameters:
10+
- name: p
11+
in: query
12+
explode: false
13+
schema:
14+
nullable: true
15+
allOf:
16+
- $ref: "#/components/schemas/MyDto"
17+
responses:
18+
"201":
19+
description: fetchOffers 201 response
20+
content:
21+
application/json:
22+
schema:
23+
$ref: "#/components/schemas/MyDto"
24+
components:
25+
schemas:
26+
MyDto:
27+
type: object
28+
properties:
29+
payload:
30+
type: string
31+
format: byte
32+
payload2:
33+
type: string
File renamed without changes.

0 commit comments

Comments
 (0)