Skip to content

Commit 45e1930

Browse files
authored
#49 Added Support for External Files (#51)
* #49 Added Support for External Files * Fixed AnyOfGeneration * Small fix for readability
1 parent fe16920 commit 45e1930

File tree

7 files changed

+84
-18
lines changed

7 files changed

+84
-18
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
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.4.1</version>
9+
<version>2.5.0</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>

src/main/java/net/coru/multiapi/converter/MultiApiContractConverter.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@ public Collection<Contract> convertFrom(final File file) {
4848

4949
Collection<Contract> contracts = null;
5050
JsonNode node;
51-
try {
52-
node = BasicTypeConstants.OBJECT_MAPPER.readTree(file);
53-
if (node != null && node.size() > 0) {
54-
if (Objects.nonNull(node.get(BasicTypeConstants.ASYNCAPI))) {
55-
contracts = ASYNC_API_CONTRACT_CONVERTER.convertFrom(file);
56-
} else if (Objects.nonNull(node.get(BasicTypeConstants.OPENAPI))) {
57-
contracts = OPEN_API_CONTRACT_CONVERTER.convertFrom(file);
51+
if (isAccepted(file)) {
52+
try {
53+
node = BasicTypeConstants.OBJECT_MAPPER.readTree(file);
54+
if (node != null && node.size() > 0) {
55+
if (Objects.nonNull(node.get(BasicTypeConstants.ASYNCAPI))) {
56+
contracts = ASYNC_API_CONTRACT_CONVERTER.convertFrom(file);
57+
} else if (Objects.nonNull(node.get(BasicTypeConstants.OPENAPI))) {
58+
contracts = OPEN_API_CONTRACT_CONVERTER.convertFrom(file);
59+
}
60+
} else {
61+
throw new MultiApiContractConverterException("Yaml file is not correct");
5862
}
59-
} else {
60-
throw new MultiApiContractConverterException("Yaml file is not correct");
63+
} catch (IOException e) {
64+
throw new MultiApiContractConverterException(e);
6165
}
62-
} catch (IOException e) {
63-
throw new MultiApiContractConverterException(e);
6466
}
65-
6667
return contracts;
67-
6868
}
6969

7070
@Override

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import io.swagger.v3.oas.models.media.Schema;
2929
import io.swagger.v3.oas.models.parameters.Parameter;
3030
import io.swagger.v3.oas.models.responses.ApiResponse;
31+
import io.swagger.v3.parser.core.models.ParseOptions;
3132
import io.swagger.v3.parser.core.models.SwaggerParseResult;
33+
import io.swagger.v3.parser.exception.ReadContentException;
3234
import lombok.extern.slf4j.Slf4j;
3335
import net.coru.multiapi.converter.exception.MultiApiContractConverterException;
3436
import net.coru.multiapi.converter.utils.BasicTypeConstants;
@@ -204,8 +206,18 @@ private void processBodyAndMatchers(final Map<String, Object> bodyMap, Schema sc
204206
processComposedSchema(openAPI, bodyMatchers, bodyMap, subComposedSchema);
205207
} else if (Objects.nonNull(property.getValue().get$ref())) {
206208
String subRef = OpenApiContractConverterUtils.mapRefName(property.getValue());
207-
HashMap<String, Schema> subProperties = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(subRef).getProperties();
208-
bodyMap.put(property.getKey(), processComplexBodyAndMatchers(property.getKey(), subProperties, openAPI, bodyMatchers));
209+
Schema<?> subSchema = openAPI.getComponents().getSchemas().get(subRef);
210+
if(Objects.nonNull(subSchema.getProperties())){
211+
Map<String, Schema> subProperties = subSchema.getProperties();
212+
bodyMap.put(property.getKey(), processComplexBodyAndMatchers(property.getKey(), subProperties, openAPI, bodyMatchers));
213+
} else {
214+
final Schema<?> arraySchema = ((ArraySchema) subSchema).getItems();
215+
if(arraySchema instanceof ComposedSchema){
216+
processComposedSchema(openAPI, bodyMatchers, bodyMap, (ComposedSchema) arraySchema);
217+
} else{
218+
writeBodyMatcher(bodyMap, openAPI, bodyMatchers, ref, arraySchema, arraySchema.getType());
219+
}
220+
}
209221
} else {
210222
final String refType;
211223
if (Objects.nonNull(property.getValue().getEnum())) {
@@ -527,10 +539,12 @@ private void processEnum(Map<String, Object> bodyMap, BodyMatchers bodyMatchers,
527539

528540
private OpenAPI getOpenApi(File file) throws MultiApiContractConverterException {
529541
OpenAPI openAPI;
542+
ParseOptions options = new ParseOptions();
543+
options.setResolve(true);
530544
try {
531-
SwaggerParseResult result = new OpenAPIParser().readLocation(file.getPath(), null, null);
545+
SwaggerParseResult result = new OpenAPIParser().readLocation(file.getPath(), null, options);
532546
openAPI = result.getOpenAPI();
533-
} catch (Exception e) {
547+
} catch (ReadContentException e) {
534548
throw new MultiApiContractConverterException("Code generation failed when parser the .yaml file ");
535549
}
536550
if (openAPI == null) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,20 @@ void testRefsInsideArrays() {
343343
.isInstanceOf(HashMap.class);
344344
}
345345

346+
@Test
347+
@DisplayName("OpenApi: Check that Refs inside arrays are being processed okay")
348+
void testExternalRefs() {
349+
final File file = new File(openApiContractConverterTestFixtures.OPENAPI_TEST_EXTERNAL_REF);
350+
Collection<Contract> contracts = multiApiContractConverter.convertFrom(file);
351+
List<Contract> contractList = new ArrayList<>(contracts);
352+
Contract contract = contractList.get(0);
353+
Response response = contract.getResponse();
354+
final Map<String, Object> bodyServerValueMap = (Map<String, Object>) response.getBody().getServerValue();
355+
assertThat(bodyServerValueMap)
356+
.isNotNull()
357+
.hasSize(5)
358+
.containsKeys(openApiContractConverterTestFixtures.OPENAPI_TEXT_EXTERNAL_REF_KEYS);
359+
assertThat(contract).isNotNull();
360+
}
361+
346362
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
public class OpenApiContractConverterTestFixtures {
66

7+
public final String[] OPENAPI_TEXT_EXTERNAL_REF_KEYS = {"schemaRegistryName", "topic", "kafkaName", "schemaName", "repetitions"};
8+
79
public final String PRICE = "price";
810

911
public final String SIMILAR_GAMES = "SimilarGames";
@@ -42,6 +44,7 @@ public class OpenApiContractConverterTestFixtures {
4244

4345
public final String OPENAPI_TEST_REQUEST_HEADERS_YML = "src/test/resources/openapi/testRequestHeaders.yml";
4446

47+
public final String OPENAPI_TEST_EXTERNAL_REF = "src/test/resources/openapi/testExternalRef.yml";
4548
public final String APPLICATION_JSON = "application/json";
4649

4750
public final String OPENAPI_TEST_REQUEST_QUERY_PARAMETERS_YML = "src/test/resources/openapi/testRequestQueryParameters.yml";
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
components:
2+
schema:
3+
generator:
4+
type: object
5+
properties:
6+
schemaName:
7+
type: string
8+
schemaRegistryName:
9+
type: string
10+
kafkaName:
11+
type: string
12+
topic:
13+
type: string
14+
repetitions:
15+
type: int32
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
openapi: 3.0.2
2+
info:
3+
title: Schema Registry Mangement
4+
version: "1.0"
5+
servers:
6+
- url: https://localhost/v1
7+
paths:
8+
/schema_generator:
9+
post:
10+
tags:
11+
- generator
12+
responses:
13+
200:
14+
description: Test File for SCC MultiApi Plugin.
15+
content:
16+
application/json:
17+
schema:
18+
$ref: "test-config/components.yml#/components/schema/generator"

0 commit comments

Comments
 (0)