Skip to content

Commit

Permalink
Fixed the behaviour for $ref objects with arrays (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rivarsal authored Jun 7, 2022
1 parent 7c3b369 commit b4f1802
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 94 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ After you have these installed, you need to add the Spring Cloud Contract Maven
<dependency>
<groupId>net.coru</groupId>
<artifactId>scc-multiapi-converter</artifactId>
<version>2.0.0</version>
<version>2.2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.coru</groupId>
<artifactId>scc-multiapi-converter</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
<name>SCC-MultiApi-Converter</name>
<description>Generates Spring Cloud Contracts based on an OpenApi and AsyncApi document</description>
<url>https://github.com/corunet/scc-multiapi-converter</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import java.util.Map.Entry;
import java.util.Objects;

import net.coru.multiapi.converter.exception.MultiApiContractConverterException;
import net.coru.multiapi.converter.utils.BasicTypeConstants;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand All @@ -32,6 +30,8 @@
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import lombok.extern.slf4j.Slf4j;
import net.coru.multiapi.converter.exception.MultiApiContractConverterException;
import net.coru.multiapi.converter.utils.BasicTypeConstants;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.cloud.contract.spec.Contract;
import org.springframework.cloud.contract.spec.internal.Body;
Expand Down Expand Up @@ -196,24 +196,29 @@ private void processBodyAndMatchers(final Map<String, Object> bodyMap, Schema sc
}
if (Objects.nonNull(schema.get$ref())) {
final String ref = OpenApiContractConverterUtils.mapRefName(schema);
final HashMap<String, Schema> properties = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(ref).getProperties();
for (Entry<String, Schema> property : properties.entrySet()) {
if (property.getValue() instanceof ComposedSchema) {
final ComposedSchema subComposedSchema = (ComposedSchema) property.getValue();
processComposedSchema(openAPI, bodyMatchers, bodyMap, subComposedSchema);
} else if (Objects.nonNull(property.getValue().get$ref())) {
String subRef = OpenApiContractConverterUtils.mapRefName(property.getValue());
HashMap<String, Schema> subProperties = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(subRef).getProperties();
bodyMap.put(property.getKey(), processComplexBodyAndMatchers(property.getKey(), subProperties, openAPI, bodyMatchers));
} else {
final String refType;
if (Objects.nonNull(property.getValue().getEnum())) {
refType = BasicTypeConstants.ENUM;
if (Objects.nonNull(openAPI.getComponents().getSchemas().get(ref).getProperties())) {
final HashMap<String, Schema> properties = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(ref).getProperties();
for (Entry<String, Schema> property : properties.entrySet()) {
if (property.getValue() instanceof ComposedSchema) {
final ComposedSchema subComposedSchema = (ComposedSchema) property.getValue();
processComposedSchema(openAPI, bodyMatchers, bodyMap, subComposedSchema);
} else if (Objects.nonNull(property.getValue().get$ref())) {
String subRef = OpenApiContractConverterUtils.mapRefName(property.getValue());
HashMap<String, Schema> subProperties = (HashMap<String, Schema>) openAPI.getComponents().getSchemas().get(subRef).getProperties();
bodyMap.put(property.getKey(), processComplexBodyAndMatchers(property.getKey(), subProperties, openAPI, bodyMatchers));
} else {
refType = property.getValue().getType();
final String refType;
if (Objects.nonNull(property.getValue().getEnum())) {
refType = BasicTypeConstants.ENUM;
} else {
refType = property.getValue().getType();
}
writeBodyMatcher(bodyMap, openAPI, bodyMatchers, property.getKey(), property.getValue(), refType);
}
writeBodyMatcher(bodyMap, openAPI, bodyMatchers, property.getKey(), property.getValue(), refType);
}
} else {
Schema arraySchema = openAPI.getComponents().getSchemas().get(ref);
writeBodyMatcher(bodyMap, openAPI, bodyMatchers, ref, arraySchema, arraySchema.getType());
}
}
}
Expand Down Expand Up @@ -243,7 +248,7 @@ private void writeBodyMatcher(final Map<String, Object> bodyMap, OpenAPI openAPI
} else if (BasicTypeConstants.DOUBLE.equalsIgnoreCase(schema.getFormat())) {
bodyMatchers.jsonPath(fieldName, bodyMatchers.byRegex(BasicTypeConstants.DECIMAL_REGEX));
bodyMap.put(fieldName, Math.abs(BasicTypeConstants.RANDOM.nextDouble()));
} else if (schema.getFormat().isEmpty()) {
} else if (schema.getFormat() == null || schema.getFormat().isEmpty()) {
bodyMatchers.jsonPath(fieldName, bodyMatchers.byRegex(BasicTypeConstants.INT_REGEX));
bodyMap.put(fieldName, BasicTypeConstants.RANDOM.nextInt());
}
Expand Down Expand Up @@ -322,7 +327,7 @@ private HashMap<String, Object> processComplexBodyAndMatchers(final String objec
} else if (BasicTypeConstants.DOUBLE.equalsIgnoreCase(property.getValue().getFormat())) {
bodyMatchers.jsonPath(property.getKey(), bodyMatchers.byRegex(BasicTypeConstants.DECIMAL_REGEX));
propertyMap.put(property.getKey(), BasicTypeConstants.RANDOM.nextDouble());
} else if (property.getValue().getFormat().isEmpty()) {
} else if (property.getValue().getFormat() == null || property.getValue().getFormat().isEmpty()) {
bodyMatchers.jsonPath(newObjectName, bodyMatchers.byRegex(BasicTypeConstants.INT_REGEX));
propertyMap.put(property.getKey(), BasicTypeConstants.RANDOM.nextInt());
}
Expand Down
Loading

0 comments on commit b4f1802

Please sign in to comment.