Skip to content

Commit

Permalink
Generate response body from Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Feb 7, 2024
1 parent 9bdcf0f commit 95cf0ac
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package io.quarkiverse.openapi.wiremock.generator.deployment.wiremock;

import static io.swagger.v3.parser.util.SchemaTypeUtil.*;

import java.util.*;

import com.fasterxml.jackson.core.JsonProcessingException;

import io.quarkiverse.openapi.wiremock.generator.deployment.wiremock.model.Request;
import io.quarkiverse.openapi.wiremock.generator.deployment.wiremock.model.Response;
import io.quarkiverse.openapi.wiremock.generator.deployment.wiremock.model.Stubbing;
import io.quarkiverse.openapi.wiremock.generator.deployment.wrapper.OpenApiWiremockGeneratorWrapper;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
Expand Down Expand Up @@ -87,7 +92,35 @@ private static String generateResponseBody(final Schema<?> schema) {
if (schemaType == null) {
return "";
}
return schemaType.equals("string") ? (String) schema.getExample() : "";

return switch (schemaType) {
case STRING_TYPE, INTEGER_TYPE -> (String) schema.getExample();
case OBJECT_TYPE -> readObjectExample(schema);
default -> "";
};
}

public static String readObjectExample(Schema<?> schema) {

try {
HashMap<String, Object> map = new HashMap<>();
mapObjectExample(map, schema);
return OpenApiWiremockGeneratorWrapper.OBJECT_MAPPER_INSTANCE.writeValueAsString(map);
} catch (JsonProcessingException e) {
return "{}";
}
}

private static Map<String, Object> mapObjectExample(Map<String, Object> root, Schema<?> example) {
Optional.ofNullable(example.getProperties()).orElse(Map.of())
.forEach((key, schema) -> {
if (schema.getType().equals(OBJECT_TYPE)) {
root.put(key, mapObjectExample(new HashMap<>(), schema));
} else {
root.put(key, schema.getExample());
}
});
return root;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
import org.junit.jupiter.api.Test;

import io.quarkiverse.openapi.wiremock.generator.deployment.wiremock.model.Stubbing;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.SpecVersion;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.media.Schema;

class OpenApi2WiremockMapperTest {

Expand Down Expand Up @@ -60,4 +57,51 @@ public void should_map_two_paths_to_two_stubbing_correctly() {
&& stub.method().equalsIgnoreCase("DELETE")));
}

@Test
@DisplayName("Should read OpenAPI schema with one level correctly")
void should_convert_schema_with_one_level_correctly() {
// given
Schema name = new Schema<>();
name.setType("string");
name.setExample("John Doe");

Schema user = new Schema<>();
user.setType("object");
user.addProperty("name", name);

// when
String schema = OpenApi2WiremockMapper.readObjectExample(user);

// then
Assertions.assertTrue(schema.contains("\"name\":\"John Doe\""));
}

@Test
@DisplayName("Should read OpenAPI schema with two levels correctly")
void should_read_openapi_schema_with_two_levels_correctly() {
// given
Schema name = new Schema<>();
name.setType("string");
name.setExample("John Doe");

Schema id = new Schema<>();
id.setType("integer");
id.setExample(10);

Schema metadata = new Schema<>();
metadata.setType("object");
metadata.addProperty("id", id);

Schema user = new Schema<>();
user.setType("object");
user.addProperty("name", name);
user.addProperty("metadata", metadata);

// when
String schema = OpenApi2WiremockMapper.readObjectExample(user);

// then
Assertions.assertTrue(schema.equals("{\"metadata\":{\"id\":10},\"name\":\"John Doe\"}"));
}

}

0 comments on commit 95cf0ac

Please sign in to comment.