Skip to content

Commit 78b73fe

Browse files
author
Matheus Cruz
committed
Add inner object at examples
1 parent 130f627 commit 78b73fe

File tree

8 files changed

+125
-48
lines changed

8 files changed

+125
-48
lines changed

moqu/core/src/main/java/io/quarkiverse/openapi/generator/OpenAPIMoquImporter.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.quarkiverse.openapi.generator;
22

3+
import static io.swagger.v3.parser.util.SchemaTypeUtil.INTEGER_TYPE;
4+
import static io.swagger.v3.parser.util.SchemaTypeUtil.OBJECT_TYPE;
5+
import static io.swagger.v3.parser.util.SchemaTypeUtil.STRING_TYPE;
6+
37
import java.util.ArrayList;
48
import java.util.Collections;
59
import java.util.HashMap;
@@ -10,11 +14,10 @@
1014
import java.util.Set;
1115
import java.util.stream.Collectors;
1216

13-
import com.google.common.base.Strings;
14-
import io.swagger.v3.oas.models.media.Schema;
1517
import org.slf4j.Logger;
1618
import org.slf4j.LoggerFactory;
1719

20+
import com.google.common.base.Strings;
1821
import com.google.common.collect.ArrayListMultimap;
1922
import com.google.common.collect.Multimap;
2023

@@ -27,15 +30,12 @@
2730
import io.swagger.v3.oas.models.PathItem;
2831
import io.swagger.v3.oas.models.examples.Example;
2932
import io.swagger.v3.oas.models.media.MediaType;
33+
import io.swagger.v3.oas.models.media.Schema;
3034
import io.swagger.v3.oas.models.parameters.Parameter;
3135
import io.swagger.v3.oas.models.responses.ApiResponse;
3236
import io.swagger.v3.parser.OpenAPIV3Parser;
3337
import io.swagger.v3.parser.core.models.SwaggerParseResult;
3438

35-
import static io.swagger.v3.parser.util.SchemaTypeUtil.INTEGER_TYPE;
36-
import static io.swagger.v3.parser.util.SchemaTypeUtil.OBJECT_TYPE;
37-
import static io.swagger.v3.parser.util.SchemaTypeUtil.STRING_TYPE;
38-
3939
public class OpenAPIMoquImporter implements MoquImporter {
4040

4141
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAPIMoquImporter.class);
@@ -66,7 +66,7 @@ public Moqu parse(String content) {
6666
private List<RequestResponsePair> getRequestResponsePairs(OpenAPI openAPI) {
6767
Map<Request, Response> requestResponsePairs = new HashMap<>();
6868

69-
Map<String, Schema> localSchemas = openAPI.getComponents().getSchemas();
69+
Map<String, Schema> localSchemas = getSchemas(openAPI);
7070

7171
Set<Map.Entry<String, PathItem>> entries = Optional.ofNullable(openAPI.getPaths())
7272
.orElseThrow(IllegalArgumentException::new)
@@ -102,6 +102,13 @@ private List<RequestResponsePair> getRequestResponsePairs(OpenAPI openAPI) {
102102
.collect(Collectors.toList());
103103
}
104104

105+
private Map<String, Schema> getSchemas(OpenAPI openAPI) {
106+
if (openAPI.getComponents() == null) {
107+
return Map.of();
108+
}
109+
return Objects.requireNonNullElse(openAPI.getComponents().getSchemas(), Map.of());
110+
}
111+
105112
private int tryGetStatusCode(Map.Entry<String, ApiResponse> statusApiResponse) {
106113
try {
107114
return Integer.parseInt(statusApiResponse.getKey());
@@ -138,7 +145,8 @@ private boolean isEligibleForExtraction(Parameter parameter, ParameterType type)
138145
}
139146

140147
private Map<Request, Response> getContentRequestResponsePairs(Map.Entry<String, ApiResponse> statusApiResponse,
141-
Map<String, Multimap<String, String>> parametersOnPath, PathItem.HttpMethod httpMethod, String url, Map<String, Schema> localSchemas) {
148+
Map<String, Multimap<String, String>> parametersOnPath, PathItem.HttpMethod httpMethod, String url,
149+
Map<String, Schema> localSchemas) {
142150
Map<Request, Response> requestResponseMap = new HashMap<>();
143151

144152
ApiResponse apiResponse = statusApiResponse.getValue();
@@ -208,8 +216,9 @@ private String resolveUrlParameters(String url, List<io.quarkiverse.openapi.gene
208216

209217
private String resolveRef(String ref, Map<String, Schema> localSchemas) {
210218
if (!ref.startsWith(REFERENCE_PREFIX)) {
211-
throw new IllegalArgumentException("There is no support for external $ref schemas. Please, configure the %s as local schema"
212-
.formatted(ref));
219+
throw new IllegalArgumentException(
220+
"There is no support for external $ref schemas. Please, configure the %s as local schema"
221+
.formatted(ref));
213222
}
214223

215224
String refName = ref.substring(REFERENCE_PREFIX.length(), ref.length());
@@ -220,7 +229,7 @@ private String resolveRef(String ref, Map<String, Schema> localSchemas) {
220229
throw new IllegalArgumentException("Schema not found: " + refName);
221230
}
222231

223-
return generateResponseBody(schema);
232+
return generateResponseBodyFromRefSchema(schema);
224233
}
225234

226235
private String resolveContent(Object object) {
@@ -233,7 +242,7 @@ private String resolveContent(Object object) {
233242
throw new IllegalArgumentException("Object is not a String");
234243
}
235244

236-
private static String generateResponseBody(final Schema<?> schema) {
245+
private static String generateResponseBodyFromRefSchema(final Schema<?> schema) {
237246
String schemaType = Optional.ofNullable(schema.getType()).orElse(OBJECT_TYPE);
238247
return switch (schemaType) {
239248
case STRING_TYPE, INTEGER_TYPE -> (String) schema.getExample();

moqu/core/src/main/java/io/quarkiverse/openapi/generator/SchemaReader.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package io.quarkiverse.openapi.generator;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import io.quarkiverse.openapi.generator.marshall.ObjectMapperFactory;
5-
import io.swagger.v3.oas.models.media.Schema;
3+
import static io.swagger.v3.parser.util.SchemaTypeUtil.OBJECT_TYPE;
64

75
import java.util.HashMap;
86
import java.util.Map;
97
import java.util.Optional;
108

11-
import static io.swagger.v3.parser.util.SchemaTypeUtil.OBJECT_TYPE;
9+
import com.fasterxml.jackson.core.JsonProcessingException;
10+
11+
import io.quarkiverse.openapi.generator.marshall.ObjectMapperFactory;
12+
import io.swagger.v3.oas.models.media.Schema;
1213

1314
public class SchemaReader {
1415

@@ -23,17 +24,24 @@ static String readObjectExample(Schema<?> schema) {
2324
}
2425
}
2526

26-
private static Map<String, Object> mapObjectExample(Schema<?> example) {
27+
private static Map<String, Object> mapObjectExample(Schema<?> schema) {
2728
Map<String, Object> currentRoot = new HashMap<>();
28-
Optional.ofNullable(example.getProperties()).orElse(Map.of())
29-
.forEach((key, schema) -> {
30-
if (schema.getType().equals(OBJECT_TYPE)) {
31-
currentRoot.put(key, mapObjectExample(schema));
29+
30+
Optional.ofNullable(schema.getProperties())
31+
.orElse(Map.of())
32+
.forEach((key, value) -> {
33+
if (value.getType().equals(OBJECT_TYPE)) {
34+
if (value.getExample() != null) {
35+
currentRoot.put(key, value.getExample());
36+
} else {
37+
currentRoot.put(key, mapObjectExample(value));
38+
}
3239
} else {
33-
currentRoot.put(key, schema.getExample());
40+
currentRoot.put(key, value.getExample());
3441
}
3542
});
43+
44+
3645
return currentRoot;
3746
}
3847
}
39-

moqu/core/src/test/java/io/quarkiverse/openapi/generator/OpenAPIMoquImporterTest.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package io.quarkiverse.openapi.generator;
22

3-
import io.quarkiverse.openapi.generator.marshall.ObjectMapperFactory;
3+
import java.util.List;
4+
import java.util.Map;
5+
46
import org.assertj.core.api.SoftAssertions;
57
import org.junit.jupiter.api.Assertions;
68
import org.junit.jupiter.api.DisplayName;
79
import org.junit.jupiter.api.Test;
810

9-
import java.util.List;
10-
import java.util.Map;
11+
import io.quarkiverse.openapi.generator.marshall.ObjectMapperFactory;
1112

1213
class OpenAPIMoquImporterTest {
1314

@@ -173,10 +174,12 @@ void shouldGenerateAResponseFromRefAsArray() {
173174
Map map = ObjectMapperFactory.getInstance().readValue(
174175
requestResponsePair.response()
175176
.content(),
176-
Map.class
177-
);
178-
softly.assertThat((List) map.get("versions"))
177+
Map.class);
178+
softly.assertThat((List<?>) map.get("versions"))
179179
.hasSize(2);
180+
181+
softly.assertThat(map.get("supportsJava")).isEqualTo(true);
182+
180183
});
181184
});
182185
}
@@ -193,8 +196,7 @@ void shouldGenerateAResponseFromRefAndNoRef() {
193196
Map map = ObjectMapperFactory.getInstance().readValue(
194197
requestResponsePair.response()
195198
.content(),
196-
Map.class
197-
);
199+
Map.class);
198200
softly.assertThat((List) map.get("versions"))
199201
.hasSize(2);
200202
});
@@ -203,11 +205,35 @@ void shouldGenerateAResponseFromRefAndNoRef() {
203205
Map map = ObjectMapperFactory.getInstance().readValue(
204206
requestResponsePair.response()
205207
.content(),
206-
Map.class
207-
);
208+
Map.class);
208209
softly.assertThat((List) map.get("versions"))
209210
.hasSize(1);
210211
});
211212
});
212213
}
214+
215+
@Test
216+
@DisplayName("Should generate a response from ref as array")
217+
void shouldGenerateAFullResponse() {
218+
String content = Testing.readContentFromFile("wiremock/complete.yml");
219+
Moqu moqu = sut.parse(content);
220+
SoftAssertions.assertSoftly(softly -> {
221+
softly.assertThat(moqu.getRequestResponsePairs()).isNotEmpty();
222+
softly.assertThat(moqu.getRequestResponsePairs()).hasSize(1);
223+
softly.assertThat(moqu.getRequestResponsePairs().get(0)).satisfies(requestResponsePair -> {
224+
Map map = ObjectMapperFactory.getInstance().readValue(
225+
requestResponsePair.response()
226+
.content(),
227+
Map.class);
228+
softly.assertThat((List<?>) map.get("versions"))
229+
.hasSize(2);
230+
231+
softly.assertThat(map.get("supportsJava")).isEqualTo(true);
232+
233+
softly.assertThat(map.get("contributors")).isEqualTo(1000);
234+
235+
softly.assertThat(((Map) map.get("rules")).get("hello")).isEqualTo("world");
236+
});
237+
});
238+
}
213239
}

moqu/core/src/test/java/io/quarkiverse/openapi/generator/wiremock/mapper/WiremockPathParamTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.quarkiverse.openapi.generator.wiremock.mapper;
22

3-
import java.io.IOException;
4-
import java.net.URISyntaxException;
53
import java.util.List;
64

75
import org.assertj.core.api.Assertions;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
openapi: 3.0.3
2+
servers:
3+
- url: http://localhost:8888
4+
info:
5+
version: 999-SNAPSHOT
6+
title: Method GET one path param
7+
paths:
8+
"/frameworks/{id}":
9+
get:
10+
parameters:
11+
- name: id
12+
in: path
13+
examples:
14+
quarkus:
15+
value: 1
16+
responses:
17+
200:
18+
content:
19+
"application/json":
20+
examples:
21+
quarkus:
22+
$ref: "#/components/schemas/Framework"
23+
description: Ok
24+
components:
25+
schemas:
26+
Framework:
27+
type: object
28+
properties:
29+
name:
30+
type: string
31+
example: "Quarkus"
32+
versions:
33+
type: array
34+
example: ["999-SNAPSHOT", "3.15.1"]
35+
supportsJava:
36+
type: boolean
37+
example: true
38+
contributors:
39+
type: integer
40+
example: 1000
41+
rules:
42+
type: object
43+
example:
44+
hello: world
45+

moqu/core/src/test/resources/wiremock/response_from_ref_array.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ components:
3232
versions:
3333
type: array
3434
example: ["999-SNAPSHOT", "3.15.1"]
35+
supportsJava:
36+
type: boolean
37+
example: true

moqu/deployment/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</dependency>
2020
<dependency>
2121
<groupId>io.quarkiverse.openapi.generator</groupId>
22-
<artifactId>quarkus-openapi-generator-mock</artifactId>
22+
<artifactId>quarkus-openapi-generator-moqu</artifactId>
2323
<version>${project.version}</version>
2424
</dependency>
2525
</dependencies>

moqu/deployment/src/main/java/io/quarkiverse/openapi/generator/MockWiremockProcessor.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)