Skip to content

Commit 96c22f8

Browse files
Fix behavior for array of refs (#55)
* Fix behavior for array of refs * Fixed array behaviour for refs with arrays of refs * Updated Readme version * Updated Readme * Update Pom version Co-authored-by: Pablo José López Rivadulla <59642284+Rivarsal@users.noreply.github.com>
1 parent 7bed432 commit 96c22f8

File tree

6 files changed

+50
-24
lines changed

6 files changed

+50
-24
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ Currently, this plugin has some limitations that will be addressed in the future
178178
179179
- This plugin allows the use of AllOfs and AnyOfs in the Response section. However, OpenApi does not support AllOfs in this section and AnyOf usage might not work depending on the OpenApi version you are using.
180180
- Some OpenApi functionalities are not implemented yet, such as creating example objects, instead you must use the example tag in every property of the object.
181+
- Due to the OpenApi Parser code, when you use a $ref that points to an external file, there are some limitations when using $ref again in that same file.
181182
182183
**Async Api implementation**:
183184
@@ -191,3 +192,4 @@ Currently, this plugin has some limitations that will be addressed in the future
191192
- Add support for generating contracts from avro files.
192193
- Further investigation for OpenApi/AsyncApi and Spring Cloud Contract possibilities.
193194
- More testing and fixing possible bugs that may occur in the future.
195+
- Get rid of the OpenApi parser in order to control our own code.

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.coru</groupId>
88
<artifactId>scc-multiapi-converter</artifactId>
9-
<version>2.6.1</version>
9+
<version>2.6.2</version>
1010
<name>SCC-MultiApi-Converter</name>
1111
<description>Generates Spring Cloud Contracts based on an OpenApi and AsyncApi document</description>
1212
<url>https://github.com/corunet/scc-multiapi-converter</url>
@@ -62,7 +62,7 @@
6262
<organization>Corunet</organization>
6363
<organizationUrl>https://corunet.github.io/</organizationUrl>
6464
<roles>
65-
<role>Junior Developer</role>
65+
<role>Developer</role>
6666
</roles>
6767
<timezone>Europe/Madrid</timezone>
6868
</developer>
@@ -73,7 +73,7 @@
7373
<organization>Corunet</organization>
7474
<organizationUrl>https://corunet.github.io/</organizationUrl>
7575
<roles>
76-
<role>Junior Developer</role>
76+
<role>Developer</role>
7777
</roles>
7878
<timezone>Europe/Madrid</timezone>
7979
</developer>

src/main/java/net/coru/multiapi/converter/openapi/OpenApiContractConverter.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,15 @@ private void processResponseContent(OpenAPI openAPI, ApiResponse apiResponse, Re
131131
OpenApiContractConverterUtils.processBasicResponseTypeBody(response, schema);
132132
} else {
133133
processBodyAndMatchers(bodyMap, schema, openAPI, responseBodyMatchers);
134-
response.setBody(new Body(bodyMap));
134+
Schema<?> checkArraySchema = new Schema<>();
135+
if (schema.get$ref() != null){
136+
checkArraySchema = openAPI.getComponents().getSchemas().get(OpenApiContractConverterUtils.mapRefName(schema));
137+
}
138+
if ((schema.getType() != null && "array".equalsIgnoreCase(schema.getType())) || (checkArraySchema.getType() != null && "array".equalsIgnoreCase(checkArraySchema.getType()))){
139+
response.setBody(new Body(bodyMap.values().toArray()[0]));
140+
} else {
141+
response.setBody(new Body(bodyMap));
142+
}
135143
response.setBodyMatchers(responseBodyMatchers);
136144
}
137145
}
@@ -185,15 +193,19 @@ private void processRequestContent(final OpenAPI openAPI, Operation operation, R
185193
private void processBodyAndMatchers(final Map<String, Object> bodyMap, Schema schema, OpenAPI openAPI, BodyMatchers bodyMatchers) {
186194

187195
if (Objects.nonNull(schema.getType())) {
188-
final Map<String, Schema> basicObjectProperties = schema.getProperties();
189-
for (Entry<String, Schema> property : basicObjectProperties.entrySet()) {
190-
if (Objects.nonNull(property.getValue().get$ref())) {
191-
final String subRef = OpenApiContractConverterUtils.mapRefName(property.getValue());
192-
final HashMap<String, Schema> subProperties = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(subRef).getProperties();
193-
bodyMap.put(property.getKey(), processComplexBodyAndMatchers(property.getKey(), subProperties, openAPI, bodyMatchers));
194-
} else {
195-
writeBodyMatcher(bodyMap, openAPI, bodyMatchers, property.getKey(), property.getValue(), property.getValue().getType());
196+
if (Objects.nonNull(schema.getProperties())){
197+
final Map<String, Schema> basicObjectProperties = schema.getProperties();
198+
for (Entry<String, Schema> property : basicObjectProperties.entrySet()) {
199+
if (Objects.nonNull(property.getValue().get$ref())) {
200+
final String subRef = OpenApiContractConverterUtils.mapRefName(property.getValue());
201+
final HashMap<String, Schema> subProperties = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(subRef).getProperties();
202+
bodyMap.put(property.getKey(), processComplexBodyAndMatchers(property.getKey(), subProperties, openAPI, bodyMatchers));
203+
} else {
204+
writeBodyMatcher(bodyMap, openAPI, bodyMatchers, property.getKey(), property.getValue(), property.getValue().getType());
205+
}
196206
}
207+
} else {
208+
writeBodyMatcher(bodyMap, openAPI, bodyMatchers, "[0]", schema, schema.getType());
197209
}
198210
}
199211
if (Objects.nonNull(schema.get$ref())) {
@@ -230,7 +242,7 @@ private void processBodyAndMatchers(final Map<String, Object> bodyMap, Schema sc
230242
}
231243
} else {
232244
Schema arraySchema = openAPI.getComponents().getSchemas().get(ref);
233-
writeBodyMatcher(bodyMap, openAPI, bodyMatchers, ref, arraySchema, arraySchema.getType());
245+
writeBodyMatcher(bodyMap, openAPI, bodyMatchers, "[0]", arraySchema, arraySchema.getType());
234246
}
235247
}
236248
}
@@ -306,8 +318,14 @@ private HashMap<String, Object> processComplexBodyAndMatchers(final String objec
306318
final String newObjectName = objectName + "." + property.getKey();
307319
if (Objects.nonNull(property.getValue().get$ref())) {
308320
final String ref = OpenApiContractConverterUtils.mapRefName(property.getValue());
309-
final HashMap<String, Schema> subProperties = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(ref).getProperties();
310-
propertyMap.put(property.getKey(), processComplexBodyAndMatchers(newObjectName, subProperties, openAPI, bodyMatchers));
321+
if (Objects.nonNull(openAPI.getComponents().getSchemas().get(ref).getProperties())){
322+
final HashMap<String, Schema> subProperties = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(ref).getProperties();
323+
propertyMap.put(property.getKey(), processComplexBodyAndMatchers(newObjectName, subProperties, openAPI, bodyMatchers));
324+
} else {
325+
final var subProperties = ((ArraySchema) openAPI.getComponents().getSchemas().get(ref)).getItems();
326+
final List<Object> propertyList = new ArrayList<>();
327+
propertyMap.put(property.getKey(), processArray(subProperties, propertyList, newObjectName, bodyMatchers, openAPI));
328+
}
311329
} else {
312330
final String type;
313331
if (Objects.nonNull(property.getValue().getEnum())) {
@@ -386,7 +404,7 @@ private List<Object> processArray(final Schema<?> arraySchema, List<Object> prop
386404
if (Objects.nonNull(arraySchema.get$ref())) {
387405
final String ref = OpenApiContractConverterUtils.mapRefName(arraySchema);
388406
final HashMap<String, Schema> subObject = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(ref).getProperties();
389-
propertyList.add(processComplexBodyAndMatchers(objectName + "[0]", subObject, openAPI, bodyMatchers));
407+
propertyList.add(processComplexBodyAndMatchers("[0]", subObject, openAPI, bodyMatchers));
390408
} else {
391409
final String type = arraySchema.getType();
392410
switch (type) {

src/test/java/net/coru/multiapi/converter/openapi/OpenApiContractConverterTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ void testArrays() {
178178
assertThat(contract).isNotNull();
179179
assertThat(contract.getRequest()).isNotNull();
180180
assertThat(contract.getResponse()).isNotNull();
181-
final Map<String, Object> bodyServerValueMap = (HashMap<String, Object>) contract.getResponse().getBody().getServerValue();
181+
final Map<String, Object> bodyServerValueMap = ((ArrayList<HashMap<String, Object>>) contract.getResponse().getBody().getServerValue()).get(0);
182182
assertThat(bodyServerValueMap)
183183
.containsKey(openApiContractConverterTestFixtures.NAME)
184184
.containsKey(openApiContractConverterTestFixtures.ADDRESS)
@@ -331,15 +331,15 @@ void testRefsInsideArrays() {
331331
List<Contract> contractList = new ArrayList<>(contracts);
332332
Contract contract = contractList.get(0);
333333
Response response = contract.getResponse();
334-
final Map<String, Object> bodyServerValueMap = (Map<String, Object>) response.getBody().getServerValue();
335-
final List<Map<String, Object>> refMap = (List<Map<String, Object>>) bodyServerValueMap.get(openApiContractConverterTestFixtures.SIMILAR_GAMES);
334+
final List<Map<String,Object>> serverValueList = (List<Map<String, Object>>) response.getBody().getServerValue();
335+
final Map<String, Object> bodyServerValueMap = serverValueList.get(0);
336336
assertThat(response).isNotNull();
337337
assertThat(bodyServerValueMap)
338338
.isNotNull()
339-
.containsKey(openApiContractConverterTestFixtures.SIMILAR_GAMES);
340-
assertThat(refMap.get(0))
341-
.isNotNull()
342-
.containsKey(openApiContractConverterTestFixtures.PRICE)
339+
.containsKeys(openApiContractConverterTestFixtures.PRICE,
340+
openApiContractConverterTestFixtures.AVAILABILITY,
341+
openApiContractConverterTestFixtures.ID,
342+
openApiContractConverterTestFixtures.NAME)
343343
.isInstanceOf(HashMap.class);
344344
}
345345

src/test/java/net/coru/multiapi/converter/openapi/OpenApiContractConverterTestFixtures.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ public class OpenApiContractConverterTestFixtures {
66

77
protected final String[] OPENAPI_TEXT_EXTERNAL_REF_KEYS = {"schemaRegistryName", "topic", "kafkaName", "schemaName", "repetitions"};
88

9+
protected final String AVAILABILITY = "availability";
10+
11+
protected final String ID = "id";
12+
913
protected final String PRICE = "price";
1014

1115
protected final String SIMILAR_GAMES = "SimilarGames";

src/test/resources/openapi/testArrays.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ paths:
2727
content:
2828
application/json:
2929
schema:
30-
$ref: "#/components/schemas/Game"
30+
type: array
31+
items:
32+
$ref: "#/components/schemas/Game"
3133
components:
3234
schemas:
3335
Game:

0 commit comments

Comments
 (0)