
io.github.microcks.util.openapi.OpenAPIImporter Maven / Gradle / Ivy
/*
* Licensed to Laurent Broudoux (the "Author") under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Author licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.github.microcks.util.openapi;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.github.microcks.domain.*;
import io.github.microcks.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
/**
* An implementation of MockReopsitoryImporter that deals with OpenAPI v3.0.x specification
* file ; whether encoding into JSON or YAML documents.
* @author laurent
*/
public class OpenAPIImporter implements MockRepositoryImporter {
/** A simple logger for diagnostic messages. */
private static Logger log = LoggerFactory.getLogger(OpenAPIImporter.class);
private boolean isYaml = true;
private JsonNode spec;
private String specContent;
private static final List VALID_VERBS = Arrays.asList("get", "put", "post", "delete", "options", "head", "patch", "trace");
/**
* Build a new importer.
* @param specificationFilePath The path to local OpenAPI spec file
* @throws IOException if project file cannot be found or read.
*/
public OpenAPIImporter(String specificationFilePath) throws IOException {
try {
// Analyse first lines of file content to guess repository type.
String line = null;
BufferedReader reader = Files.newBufferedReader(new File(specificationFilePath).toPath(), Charset.forName("UTF-8"));
while ((line = reader.readLine()) != null) {
line = line.trim();
// Check is we start with json object or array definition.
if (line.startsWith("{") || line.startsWith("[")) {
isYaml = false;
break;
}
else if (line.startsWith("---") || line.startsWith("openapi: ")) {
isYaml = true;
break;
}
}
reader.close();
// Read spec bytes.
byte[] bytes = Files.readAllBytes(Paths.get(specificationFilePath));
specContent = new String(bytes, Charset.forName("UTF-8"));
// Convert them to Node using Jackson object mapper.
ObjectMapper mapper = null;
if (isYaml) {
mapper = new ObjectMapper(new YAMLFactory());
} else {
mapper = new ObjectMapper();
}
spec = mapper.readTree(bytes);
} catch (Exception e) {
log.error("Exception while parsing OpenAPI specification file " + specificationFilePath, e);
throw new IOException("OpenAPI spec file parsing error");
}
}
@Override
public List getServiceDefinitions() throws MockRepositoryImportException {
List result = new ArrayList<>();
// Build a new service.
Service service = new Service();
service.setName(spec.path("info").path("title").asText());
service.setVersion(spec.path("info").path("version").asText());
service.setType(ServiceType.REST);
// Then build its operations.
service.setOperations(extractOperations());
result.add(service);
return result;
}
@Override
public List getResourceDefinitions(Service service) {
List results = new ArrayList<>();
// Build a suitable name.
String name = service.getName() + "-" + service.getVersion();
if (isYaml) {
name += ".yaml";
} else {
name += ".json";
}
// Build a brand new resource just with spec content.
Resource resource = new Resource();
resource.setName(name);
resource.setType(ResourceType.OPEN_API_SPEC);
resource.setContent(specContent);
results.add(resource);
return results;
}
@Override
public List getMessageDefinitions(Service service, Operation operation) throws MockRepositoryImportException {
Map result = new HashMap<>();
// Iterate on specification "paths" nodes.
Iterator> paths = spec.path("paths").fields();
while (paths.hasNext()) {
Entry path = paths.next();
String pathName = path.getKey();
// Find examples fragments defined at the path level.
Map> pathPathParametersByExample = extractParametersByExample(path.getValue(), "path");
// Iterate on specification path, "verbs" nodes.
Iterator> verbs = path.getValue().fields();
while (verbs.hasNext()) {
Entry verb = verbs.next();
String verbName = verb.getKey();
// Find the correct operation.
if (operation.getName().equals(verbName.toUpperCase() + " " + pathName.trim())) {
// Find examples fragments defined at the verb level.
Map> pathParametersByExample = extractParametersByExample(verb.getValue(), "path");
pathParametersByExample.putAll(pathPathParametersByExample);
Map> queryParametersByExample = extractParametersByExample(verb.getValue(), "query");
Map> headerParametersByExample = extractParametersByExample(verb.getValue(), "header");
Map requestBodiesByExample = extractRequestBodies(verb.getValue());
// No need to go further if no examples.
if (verb.getValue().has("responses")) {
Iterator> responseCodes = verb.getValue().path("responses").fields();
while (responseCodes.hasNext()) {
Entry responseCode = responseCodes.next();
// Find here potential headers for output of this operation examples.
Map> headersByExample = extractHeadersByExample(responseCode.getValue());
Iterator> contents = getResponseContent(responseCode.getValue()).fields();
while (contents.hasNext()) {
Entry content = contents.next();
String contentValue = content.getKey();
Iterator exampleNames = content.getValue().path("examples").fieldNames();
while (exampleNames.hasNext()) {
String exampleName = exampleNames.next();
JsonNode example = content.getValue().path("examples").path(exampleName);
// We should have everything at hand to build response here.
Response response = new Response();
response.setName(exampleName);
response.setMediaType(contentValue);
response.setStatus(responseCode.getKey());
response.setContent(getExampleValue(example));
if (!responseCode.getKey().startsWith("2")) {
response.setFault(true);
}
List responseHeaders = headersByExample.get(exampleName);
if (responseHeaders != null) {
responseHeaders.stream().forEach(header -> response.addHeader(header));
}
// Do we have a request for this example?
Request request = requestBodiesByExample.get(exampleName);
if (request == null) {
request = new Request();
request.setName(exampleName);
}
// Complete request accept-type with response content-type.
Header header = new Header();
header.setName("Accept");
HashSet values = new HashSet<>();
values.add(contentValue);
header.setValues(values);
request.addHeader(header);
// Do we have to complete request with path parameters?
Map pathParameters = pathParametersByExample.get(exampleName);
if (pathParameters != null) {
for (Entry paramEntry : pathParameters.entrySet()) {
Parameter param = new Parameter();
param.setName(paramEntry.getKey());
param.setValue(paramEntry.getValue());
request.addQueryParameter(param);
}
} else if (DispatchStyles.URI_PARTS.equals(operation.getDispatcher())
|| DispatchStyles.URI_ELEMENTS.equals(operation.getDispatcher())) {
// We've must have at least one path parameters but none...
// Do not register this request / response pair.
break;
}
// Do we have to complete request with query parameters?
Map queryParameters = queryParametersByExample.get(exampleName);
if (queryParameters != null) {
for (Entry paramEntry : queryParameters.entrySet()) {
Parameter param = new Parameter();
param.setName(paramEntry.getKey());
param.setValue(paramEntry.getValue());
request.addQueryParameter(param);
}
}
// Do we have to complete request with header parameters?
Map headerParameters = headerParametersByExample.get(exampleName);
if (headerParameters != null) {
for (Entry headerEntry : headerParameters.entrySet()) {
header = new Header();
header.setName(headerEntry.getKey());
// Values may be multiple and CSV.
Set headerValues = Arrays.stream(headerEntry.getValue().split(","))
.map(value -> value.trim())
.collect(Collectors.toSet());
header.setValues(headerValues);
request.addHeader(header);
}
}
// Finally, take care about dispatchCriteria and complete operation resourcePaths.
String dispatchCriteria = null;
String resourcePathPattern = operation.getName().split(" ")[1];
if (DispatchStyles.URI_PARAMS.equals(operation.getDispatcher())) {
Map queryParams = queryParametersByExample.get(exampleName);
dispatchCriteria = DispatchCriteriaHelper
.buildFromParamsMap(operation.getDispatcherRules(), queryParams);
// We only need the pattern here.
operation.addResourcePath(resourcePathPattern);
} else if (DispatchStyles.URI_PARTS.equals(operation.getDispatcher())) {
Map parts = pathParametersByExample.get(exampleName);
dispatchCriteria = DispatchCriteriaHelper.buildFromPartsMap(parts);
// We should complete resourcePath here.
String resourcePath = URIBuilder.buildURIFromPattern(resourcePathPattern, parts);
operation.addResourcePath(resourcePath);
} else if (DispatchStyles.URI_ELEMENTS.equals(operation.getDispatcher())) {
Map parts = pathParametersByExample.get(exampleName);
Map queryParams = queryParametersByExample.get(exampleName);
dispatchCriteria = DispatchCriteriaHelper.buildFromPartsMap(parts);
dispatchCriteria += DispatchCriteriaHelper
.buildFromParamsMap(operation.getDispatcherRules(), queryParams);
// We should complete resourcePath here.
String resourcePath = URIBuilder.buildURIFromPattern(resourcePathPattern, parts);
operation.addResourcePath(resourcePath);
}
response.setDispatchCriteria(dispatchCriteria);
result.put(request, response);
}
}
}
}
}
}
}
// Adapt map to list of Exchanges.
return result.entrySet().stream()
.map(entry -> new RequestResponsePair(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
}
/**
* Extract the list of operations from Specification.
*/
private List extractOperations() throws MockRepositoryImportException {
List results = new ArrayList<>();
// Iterate on specification "paths" nodes.
Iterator> paths = spec.path("paths").fields();
while (paths.hasNext()) {
Entry path = paths.next();
String pathName = path.getKey();
// Iterate on specification path, "verbs" nodes.
Iterator> verbs = path.getValue().fields();
while (verbs.hasNext()) {
Entry verb = verbs.next();
String verbName = verb.getKey();
// Only deal with real verbs for now.
if (VALID_VERBS.contains(verbName)) {
String operationName = verbName.toUpperCase() + " " + pathName.trim();
Operation operation = new Operation();
operation.setName(operationName);
operation.setMethod(verbName.toUpperCase());
// Deal with dispatcher stuffs.
if (operationHasParameters(verb.getValue(), "query") && urlHasParts(pathName)) {
operation.setDispatcherRules(DispatchCriteriaHelper.extractPartsFromURIPattern(pathName)
+ " ?? " + extractOperationParams(verb.getValue()));
operation.setDispatcher(DispatchStyles.URI_ELEMENTS);
} else if (operationHasParameters(verb.getValue(), "query")) {
operation.setDispatcherRules(extractOperationParams(verb.getValue()));
operation.setDispatcher(DispatchStyles.URI_PARAMS);
} else if (urlHasParts(pathName)) {
operation.setDispatcherRules(DispatchCriteriaHelper.extractPartsFromURIPattern(pathName));
operation.setDispatcher(DispatchStyles.URI_PARTS);
} else {
operation.addResourcePath(pathName);
}
results.add(operation);
}
}
}
return results;
}
/**
* Extract parameters within a specification node and organize them by example. Parameter can be of type 'path',
* 'query', 'header' or 'cookie'. Allow to filter them using parameterType. Key of returned map is example name.
* Key of value map is param name. Value of value map is param value ;-)
*/
private Map> extractParametersByExample(JsonNode node, String parameterType) {
Map> results = new HashMap<>();
Iterator parameters = node.path("parameters").elements();
while (parameters.hasNext()) {
JsonNode parameter = parameters.next();
String parameterName = parameter.path("name").asText();
if (parameter.has("in") && parameter.path("in").asText().equals(parameterType)
&& parameter.has("examples")) {
Iterator exampleNames = parameter.path("examples").fieldNames();
while (exampleNames.hasNext()) {
String exampleName = exampleNames.next();
JsonNode example = parameter.path("examples").path(exampleName);
String exampleValue = getExampleValue(example);
Map exampleParams = results.get(exampleName);
if (exampleParams == null) {
exampleParams = new HashMap<>();
results.put(exampleName, exampleParams);
}
exampleParams.put(parameterName, exampleValue);
}
}
}
return results;
}
/**
* Extract request bodies within verb specification and organize them by example.
* Key of returned map is example name. Value is basic Microcks Request object (no query params, no headers)
*/
private Map extractRequestBodies(JsonNode verbNode) {
Map results = new HashMap<>();
JsonNode requestBody = verbNode.path("requestBody");
Iterator contentTypeNames = requestBody.path("content").fieldNames();
while (contentTypeNames.hasNext()) {
String contentTypeName = contentTypeNames.next();
JsonNode contentType = requestBody.path("content").path(contentTypeName);
if (contentType.has("examples")) {
Iterator exampleNames = contentType.path("examples").fieldNames();
while (exampleNames.hasNext()) {
String exampleName = exampleNames.next();
JsonNode example = contentType.path("examples").path(exampleName);
String exampleValue = getExampleValue(example);
// Build and store a request object.
Request request = new Request();
request.setName(exampleName);
request.setContent(exampleValue);
// We should add a Content-type header here for request body.
Header header = new Header();
header.setName("Content-Type");
HashSet values = new HashSet<>();
values.add(contentTypeName);
header.setValues(values);
request.addHeader(header);
results.put(exampleName, request);
}
}
}
return results;
}
/**
* Extract headers within a header specification node and organize them by example.
* Key of returned map is example name. Value is a list of Microcks Header objects.
*/
private Map> extractHeadersByExample(JsonNode responseNode) {
Map> results = new HashMap<>();
if (responseNode.has("headers")) {
JsonNode headersNode = responseNode.path("headers");
Iterator headerNames = headersNode.fieldNames();
while (headerNames.hasNext()) {
String headerName = headerNames.next();
JsonNode headerNode = headersNode.path(headerName);
if (headerNode.has("examples")) {
Iterator exampleNames = headerNode.path("examples").fieldNames();
while (exampleNames.hasNext()) {
String exampleName = exampleNames.next();
JsonNode example = headerNode.path("examples").path(exampleName);
String exampleValue = getExampleValue(example);
// Example may be multiple CSV.
Set values = Arrays.stream(
exampleValue.split(",")).map(value -> value.trim())
.collect(Collectors.toSet());
Header header = new Header();
header.setName(headerName);
header.setValues(values);
List headersForExample = results.get(exampleName);
if (headersForExample == null) {
headersForExample = new ArrayList<>();
}
headersForExample.add(header);
results.put(exampleName, headersForExample);
}
}
}
}
if (responseNode.has("$ref")) {
// $ref: '#/components/responses/unknown'
String ref = responseNode.path("$ref").asText();
JsonNode component = spec.at(ref.substring(1));
return extractHeadersByExample(component);
}
return results;
}
/** Get the value of an example. This can be direct value field or those of followed $ref */
private String getExampleValue(JsonNode example) {
if (example.has("value")) {
if (example.path("value").getNodeType() == JsonNodeType.ARRAY ||
example.path("value").getNodeType() == JsonNodeType.OBJECT ) {
return example.path("value").toString();
}
return example.path("value").asText();
}
if (example.has("$ref")) {
// $ref: '#/components/examples/param_laurent'
String ref = example.path("$ref").asText();
JsonNode component = spec.at(ref.substring(1));
return getExampleValue(component);
}
return null;
}
/** Get the content of a response. This can be direct content field or those of followed $ref */
private JsonNode getResponseContent(JsonNode response) {
if (response.has("$ref")) {
// $ref: '#/components/responses/unknown'
String ref = response.path("$ref").asText();
JsonNode component = spec.at(ref.substring(1));
return getResponseContent(component);
}
return response.path("content");
}
/** Check parameters presence into given operation node. */
private static boolean operationHasParameters(JsonNode operation, String parameterType) {
if (!operation.has("parameters")) {
return false;
}
Iterator parameters = operation.path("parameters").elements();
while (parameters.hasNext()) {
JsonNode parameter = parameters.next();
String parameterIn = parameter.path("in").asText();
if (parameterIn.equals(parameterType)) {
return true;
}
}
return false;
}
/** Build a string representing operation parameters as used in dispatcher rules (param1 && param2)*/
private static String extractOperationParams(JsonNode operation) {
StringBuilder params = new StringBuilder();
Iterator parameters = operation.path("parameters").elements();
while (parameters.hasNext()) {
JsonNode parameter = parameters.next();
String parameterIn = parameter.path("in").asText();
if (!"path".equals(parameterIn)) {
if (params.length() > 0) {
params.append(" && ");
}
params.append(parameter.path("name").asText());
}
}
return params.toString();
}
/** Check variables parts presence into given url. */
private static boolean urlHasParts(String url) {
return (url.indexOf("/:") != -1 || url.indexOf("/{") != -1);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy