io.automatiko.engine.quarkus.function.deployment.ExampleGenerator Maven / Gradle / Ivy
package io.automatiko.engine.quarkus.function.deployment;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.eclipse.microprofile.openapi.models.media.Schema;
import org.eclipse.microprofile.openapi.models.media.Schema.SchemaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* Inspired by OpenAPI tools implementation of example generator
*
*/
public class ExampleGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(ExampleGenerator.class);
private static final String URL = "url";
private static final String URI = "uri";
private Random random;
public ExampleGenerator() {
// use a fixed seed to make the "random" numbers reproducible.
this.random = new Random("ExampleGenerator".hashCode());
}
public Map generate(Schema property, OpenAPI openApi) {
LOGGER.debug("debugging generate in ExampleGenerator");
Set processedModels = new HashSet<>();
Map kv = new LinkedHashMap<>();
if (property != null) {
for (Entry prop : property.getProperties().entrySet()) {
Object example = resolvePropertyToExample(prop.getKey(), prop.getValue(), processedModels, openApi);
if (example != null) {
kv.put(prop.getKey(), example);
}
}
}
return kv;
}
private Object resolvePropertyToExample(String propertyName, Schema property,
Set processedModels, OpenAPI openApi) {
LOGGER.debug("Resolving example for property {}...", property);
if (property.getRef() != null) {
property = openApi.getComponents().getSchemas().get(getSimpleRef(property.getRef()));
}
if (propertyName.equals("metadata") || propertyName.equals("data") || propertyName.equals("workflowdata")) {
return null;
}
if (property.getType().equals(SchemaType.BOOLEAN)) {
Object defaultValue = property.getDefaultValue();
if (defaultValue != null) {
return defaultValue;
}
return Boolean.TRUE;
} else if (property.getType().equals(SchemaType.ARRAY)) {
Schema innerType = property.getItems();
if (innerType != null) {
int arrayLength = 2;
// avoid memory issues by limiting to max. 5 items
arrayLength = Math.min(arrayLength, 5);
Object[] objectProperties = new Object[arrayLength];
Object objProperty = resolvePropertyToExample(propertyName, innerType, processedModels, openApi);
for (int i = 0; i < arrayLength; i++) {
objectProperties[i] = objProperty;
}
return objectProperties;
}
} else if (property.getType().equals(SchemaType.NUMBER)) {
Double min = getPropertyValue(property.getMinimum());
Double max = getPropertyValue(property.getMaximum());
return randomNumber(min, max);
} else if (property.getType().equals(SchemaType.INTEGER)) {
Double min = getPropertyValue(property.getMinimum());
Double max = getPropertyValue(property.getMaximum());
return randomIntNumber(min, max);
} else if (property.getType().equals(SchemaType.STRING)) {
LOGGER.debug("String property");
String defaultValue = (String) property.getDefaultValue();
if (defaultValue != null && !defaultValue.isEmpty()) {
LOGGER.debug("Default value found: '{}'", defaultValue);
return defaultValue;
}
List