use of io.swagger.models.properties.ArrayProperty in project sql-boot by sql-boot.
the class ApiController method getSwaggerDescription.
private Swagger getSwaggerDescription(HttpServletRequest request, String connectionName) {
FsResourceTypes fsResourceTypes = new FsResourceTypes(dbConnectionList.getConnectionByName(connectionName));
final List<ResourceType> resourceTypes = fsResourceTypes.resourceTypes();
Swagger swagger = new Swagger();
swagger.consumes("application/json");
swagger.host(request.getServerName() + ":" + request.getServerPort());
swagger.setInfo(new Info().version("v1").title("API specification"));
swagger.setSchemes(asList(Scheme.HTTP, Scheme.HTTPS));
swagger.path("/connections", new Path().get(new Operation().tag("connections").response(200, new Response().description("Ok").schema(new ArrayProperty(new RefProperty("connection")))).produces("application/json")));
swagger.model("connection", new ModelImpl().property("name", new StringProperty().description("name")).property("url", new StringProperty().description("url")).property("user", new StringProperty().description("user")).property("driverClassName", new StringProperty().description("driverClassName")).property("configurationFolder", new StringProperty().description("configurationFolder")));
// paths
for (ResourceType resourceType : resourceTypes) {
PathParameter parameter = new PathParameter().required(true).type("string").name("connection_name");
parameter.setDefaultValue(connectionName);
swagger.path("/api/{connection_name}/headers/" + resourceType.name(), new Path().get(new Operation().description(resourceType.name()).tag("db_objects").parameter(parameter).parameter(new QueryParameter().name("select").type("string")).parameter(new QueryParameter().name("distinct").type("boolean")).parameter(new QueryParameter().name("where").type("string")).parameter(new QueryParameter().name("page").type("string").description("get page by mask [page_count:page_size]")).parameter(new QueryParameter().name("limit").type("integer")).parameter(new QueryParameter().name("orderby").type("string")).parameter(new QueryParameter().name("cache").type("boolean")).response(200, new Response().description("Ok").schema(new ArrayProperty(new RefProperty(resourceType.name())))).produces("application/json")));
final List<String> path = resourceType.path();
final List<String> newPath = new ArrayList<>();
for (String s : path) {
newPath.add(s + "_name");
List<Parameter> parameterList = new ArrayList<>();
PathParameter parameterConnection = new PathParameter().required(true).type("string").name("connection_name");
parameterConnection.setDefaultValue(connectionName);
parameterList.add(parameterConnection);
for (String s1 : newPath) {
final PathParameter pathParameter = new PathParameter().required(true).type("string").name(s1);
pathParameter.setDefaultValue("*");
parameterList.add(pathParameter);
}
Operation operation = new Operation();
operation.setParameters(parameterList);
operation.parameter(new QueryParameter().name("select").type("string"));
operation.parameter(new QueryParameter().name("distinct").type("boolean"));
operation.parameter(new QueryParameter().name("where").type("string"));
operation.parameter(new QueryParameter().name("page").type("string").description("get page by mask [page_count:page_size]"));
operation.parameter(new QueryParameter().name("limit").type("integer"));
operation.parameter(new QueryParameter().name("orderby").type("string"));
operation.parameter(new QueryParameter().name("cache").type("boolean"));
swagger.path("/api/{connection_name}/headers/" + resourceType.name() + "/" + newPath.stream().map(v -> "{" + v + "}").collect(joining(".")), new Path().get(operation.description(resourceType.name()).tag("db_objects").response(200, new Response().description("Ok").schema(new ArrayProperty(new RefProperty(resourceType.name())))).produces("application/json")));
}
}
// definitions
for (ResourceType resourceType : resourceTypes) {
ModelImpl model = new ModelImpl();
Map<String, String> stringStringMap = resourceType.metaData();
if (stringStringMap != null) {
Set<Entry<String, String>> entries = stringStringMap.entrySet();
for (Entry<String, String> stringStringEntry : entries) {
model.property(stringStringEntry.getKey(), new StringProperty().description(stringStringEntry.getValue()));
}
}
swagger.model(resourceType.name(), model);
}
return swagger;
}
use of io.swagger.models.properties.ArrayProperty in project teiid by teiid.
the class SwaggerMetadataProcessor method buildResponse.
private boolean buildResponse(final MetadataFactory mf, final Swagger swagger, final Procedure procedure, final Response resp) throws TranslatorException {
PropertyAction pa = new PropertyAction() {
@Override
public void execute(String name, String nameInSource, Property property, boolean array) {
String type = getPropertyType(property, array);
Column c = mf.addProcedureResultSetColumn(name, type, procedure);
if (!name.equalsIgnoreCase(nameInSource)) {
c.setNameInSource(nameInSource);
}
}
};
Property schema = resp.getSchema();
if (schema != null) {
if (isSimple(schema)) {
boolean array = false;
if (schema instanceof ArrayProperty) {
schema = ((ArrayProperty) schema).getItems();
array = true;
}
if (resp.getHeaders() == null || resp.getHeaders().isEmpty()) {
String type = SwaggerTypeManager.teiidType(schema.getType(), schema.getFormat(), array);
mf.addProcedureParameter("return", type, ProcedureParameter.Type.ReturnValue, procedure);
} else {
Map<String, Property> properties = new LinkedHashMap<String, Property>();
properties.put("return", schema);
walkProperties(swagger, properties, null, null, pa);
}
} else {
// since the return is always a collection unwrap the array without any issues.
if (schema instanceof ArrayProperty) {
schema = ((ArrayProperty) schema).getItems();
}
if (schema instanceof ObjectProperty) {
walkProperties(swagger, ((ObjectProperty) schema).getProperties(), null, null, pa);
} else if (schema instanceof RefProperty) {
String modelName = ((RefProperty) schema).getSimpleRef();
Model model = swagger.getDefinitions().get(modelName);
walkProperties(swagger, model.getProperties(), null, null, pa);
} else if (schema instanceof MapProperty) {
Property property = ((MapProperty) schema).getAdditionalProperties();
String type = SwaggerTypeManager.teiidType(property.getType(), property.getFormat(), false);
Column c = mf.addProcedureResultSetColumn(KEY_NAME, "string", procedure);
c.setNameInSource(KEY_NAME);
c = mf.addProcedureResultSetColumn(KEY_VALUE, type, procedure);
c.setNameInSource(KEY_VALUE);
} else {
throw new TranslatorException("File properties are not supported");
}
}
}
Map<String, Property> headers = resp.getHeaders();
if (headers != null && !headers.isEmpty()) {
walkProperties(swagger, headers, null, null, pa);
}
return procedure.getResultSet() != null;
}
use of io.swagger.models.properties.ArrayProperty in project teiid by teiid.
the class SwaggerMetadataProcessor method walkProperties.
private void walkProperties(final Swagger swagger, Set<Property> parents, final Map<String, Property> properties, final String namePrefix, final String nisPrefix, final PropertyAction pa) {
if (properties == null) {
return;
}
final PropertyVisitor visitor = new PropertyVisitor() {
@Override
public void visit(String name, Property property) {
pa.execute(fqn(namePrefix, name), nis(nisPrefix, name, false), property, false);
}
@Override
public void visit(String name, ArrayProperty property) {
if (isSimple(property)) {
// the array type defined in the type of the property
pa.execute(fqn(namePrefix, name), nis(nisPrefix, name, false), property.getItems(), true);
} else {
// if Object or Ref, array does not matter as return is already a resultset.
Property items = property.getItems();
parents.add(property);
if (items instanceof ObjectProperty) {
String modelName = ((ObjectProperty) items).getName();
walkProperties(swagger, parents, ((ObjectProperty) items).getProperties(), fqn(fqn(namePrefix, name), modelName), nis(nis(nisPrefix, name, true), modelName, false), pa);
} else if (items instanceof RefProperty) {
String modelName = ((RefProperty) items).getSimpleRef();
Model model = swagger.getDefinitions().get(modelName);
walkProperties(swagger, parents, model.getProperties(), fqn(fqn(namePrefix, name), modelName), nis(nis(nisPrefix, name, true), modelName, false), pa);
} else {
walkProperties(swagger, parents, properties, fqn(namePrefix, name), nis(nisPrefix, name, true), pa);
}
parents.remove(property);
}
}
@Override
public void visit(String name, FileProperty property) {
// TODO:
}
@Override
public void visit(String name, MapProperty property) {
// TODO:
}
@Override
public void visit(String name, ObjectProperty property) {
parents.add(property);
walkProperties(swagger, parents, property.getProperties(), fqn(namePrefix, name), nis(nisPrefix, name, false), pa);
parents.remove(property);
}
@Override
public void visit(String name, RefProperty property) {
parents.add(property);
Model model = swagger.getDefinitions().get(property.getSimpleRef());
walkProperties(swagger, parents, model.getProperties(), fqn(namePrefix, name), nis(nisPrefix, name, false), pa);
parents.remove(property);
}
};
for (Entry<String, Property> p : properties.entrySet()) {
if (parents.contains(p.getValue())) {
// TODO: could be an error condition
continue;
}
visitor.accept(p.getKey(), p.getValue());
}
}
use of io.swagger.models.properties.ArrayProperty in project swagger-parser by swagger-api.
the class ModelPropertyConverterTest method convertRefArrayModelProperty.
@Test
public void convertRefArrayModelProperty() throws Exception {
ModelProperty property = new ModelProperty();
property.setType("array");
Items items = new Items();
items.setRef("Pet");
property.setItems(items);
Property converted = converter.convertProperty(property);
assertEquals(converted.getClass(), ArrayProperty.class);
ArrayProperty prop = (ArrayProperty) converted;
Property innerType = prop.getItems();
RefProperty ref = (RefProperty) innerType;
assertEquals(ref.getSimpleRef(), "Pet");
}
use of io.swagger.models.properties.ArrayProperty in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalDefinition.
public String processRefToExternalDefinition(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final Model model = cache.loadRef($ref, refFormat, Model.class);
if (model == null) {
// stop! There's a problem. retain the original ref
LOGGER.warn("unable to load model reference from `" + $ref + "`. It may not be available " + "or the reference isn't a valid model schema");
return $ref;
}
String newRef;
Map<String, Model> definitions = swagger.getDefinitions();
if (definitions == null) {
definitions = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
String tryName = null;
Model existingModel = definitions.get(possiblyConflictingDefinitionName);
if (existingModel != null) {
LOGGER.debug("A model for " + existingModel + " already exists");
if (existingModel instanceof RefModel) {
// use the new model
existingModel = null;
} else {
// We add a number at the end of the definition name
int i = 2;
for (String name : definitions.keySet()) {
if (name.equals(possiblyConflictingDefinitionName)) {
tryName = possiblyConflictingDefinitionName + "_" + i;
existingModel = definitions.get(tryName);
i++;
}
}
}
}
if (StringUtils.isNotBlank(tryName)) {
newRef = tryName;
} else {
newRef = possiblyConflictingDefinitionName;
}
cache.putRenamedRef($ref, newRef);
if (existingModel == null) {
// don't overwrite existing model reference
swagger.addDefinition(newRef, model);
cache.addReferencedKey(newRef);
String file = $ref.split("#/")[0];
if (model instanceof RefModel) {
RefModel refModel = (RefModel) model;
if (isAnExternalRefFormat(refModel.getRefFormat())) {
refModel.set$ref(processRefToExternalDefinition(refModel.get$ref(), refModel.getRefFormat()));
} else {
processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
}
}
if (model instanceof ComposedModel) {
ComposedModel composedModel = (ComposedModel) model;
List<Model> listOfAllOF = composedModel.getAllOf();
for (Model allOfModel : listOfAllOF) {
if (allOfModel instanceof RefModel) {
RefModel refModel = (RefModel) allOfModel;
if (isAnExternalRefFormat(refModel.getRefFormat())) {
String joinedRef = join(file, refModel.get$ref());
refModel.set$ref(processRefToExternalDefinition(joinedRef, refModel.getRefFormat()));
} else {
processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
}
} else if (allOfModel instanceof ModelImpl) {
// Loop through additional properties of allOf and recursively call this method;
processProperties(allOfModel.getProperties(), file);
}
}
}
// Loop the properties and recursively call this method;
processProperties(model.getProperties(), file);
if (model instanceof ModelImpl) {
ModelImpl modelImpl = (ModelImpl) model;
Property additionalProperties = modelImpl.getAdditionalProperties();
if (additionalProperties != null) {
if (additionalProperties instanceof RefProperty) {
processRefProperty(((RefProperty) additionalProperties), file);
} else if (additionalProperties instanceof ArrayProperty) {
ArrayProperty arrayProp = (ArrayProperty) additionalProperties;
if (arrayProp.getItems() instanceof RefProperty) {
processRefProperty((RefProperty) arrayProp.getItems(), file);
}
} else if (additionalProperties instanceof MapProperty) {
MapProperty mapProp = (MapProperty) additionalProperties;
if (mapProp.getAdditionalProperties() instanceof RefProperty) {
processRefProperty((RefProperty) mapProp.getAdditionalProperties(), file);
} else if (mapProp.getAdditionalProperties() instanceof ArrayProperty && ((ArrayProperty) mapProp.getAdditionalProperties()).getItems() instanceof RefProperty) {
processRefProperty((RefProperty) ((ArrayProperty) mapProp.getAdditionalProperties()).getItems(), file);
}
}
}
}
if (model instanceof ArrayModel && ((ArrayModel) model).getItems() instanceof RefProperty) {
processRefProperty((RefProperty) ((ArrayModel) model).getItems(), file);
}
}
return newRef;
}
Aggregations