Search in sources :

Example 66 with ArrayProperty

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;
}
Also used : Path(io.swagger.models.Path) Scheme(io.swagger.models.Scheme) Yaml(io.swagger.util.Yaml) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) SqlPlaceholdersWrapper(com.github.mgramin.sqlboot.model.uri.wrappers.SqlPlaceholdersWrapper) Swagger(io.swagger.models.Swagger) StringProperty(io.swagger.models.properties.StringProperty) Json(io.swagger.util.Json) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ModelImpl(io.swagger.models.ModelImpl) ArrayProperty(io.swagger.models.properties.ArrayProperty) GET(org.springframework.web.bind.annotation.RequestMethod.GET) CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) ArrayList(java.util.ArrayList) ResourceType(com.github.mgramin.sqlboot.model.resource_type.ResourceType) HttpServletRequest(javax.servlet.http.HttpServletRequest) Path(io.swagger.models.Path) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) RefProperty(io.swagger.models.properties.RefProperty) Operation(io.swagger.models.Operation) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) PathParameter(io.swagger.models.parameters.PathParameter) FsResourceTypes(com.github.mgramin.sqlboot.model.resource_type.impl.composite.FsResourceTypes) DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) Set(java.util.Set) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Parameter(io.swagger.models.parameters.Parameter) APPLICATION_JSON_VALUE(org.springframework.http.MediaType.APPLICATION_JSON_VALUE) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) BootException(com.github.mgramin.sqlboot.exceptions.BootException) Info(io.swagger.models.Info) ComponentScan(org.springframework.context.annotation.ComponentScan) Collectors.joining(java.util.stream.Collectors.joining) QueryParameter(io.swagger.models.parameters.QueryParameter) DbConnectionList(com.github.mgramin.sqlboot.model.connection.DbConnectionList) Response(io.swagger.models.Response) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Uri(com.github.mgramin.sqlboot.model.uri.Uri) Entry(java.util.Map.Entry) DbResource(com.github.mgramin.sqlboot.model.resource.DbResource) ResponseEntity(org.springframework.http.ResponseEntity) ArrayProperty(io.swagger.models.properties.ArrayProperty) QueryParameter(io.swagger.models.parameters.QueryParameter) ArrayList(java.util.ArrayList) StringProperty(io.swagger.models.properties.StringProperty) ResourceType(com.github.mgramin.sqlboot.model.resource_type.ResourceType) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) PathParameter(io.swagger.models.parameters.PathParameter) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) Entry(java.util.Map.Entry) FsResourceTypes(com.github.mgramin.sqlboot.model.resource_type.impl.composite.FsResourceTypes) Swagger(io.swagger.models.Swagger) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) ModelImpl(io.swagger.models.ModelImpl)

Example 67 with ArrayProperty

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;
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) MapProperty(io.swagger.models.properties.MapProperty) LinkedHashMap(java.util.LinkedHashMap) RefProperty(io.swagger.models.properties.RefProperty) BaseColumn(org.teiid.metadata.BaseColumn) Column(org.teiid.metadata.Column) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) TranslatorException(org.teiid.translator.TranslatorException) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) TranslatorProperty(org.teiid.translator.TranslatorProperty) ExtensionMetadataProperty(org.teiid.metadata.ExtensionMetadataProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) FileProperty(io.swagger.models.properties.FileProperty)

Example 68 with ArrayProperty

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());
    }
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) FileProperty(io.swagger.models.properties.FileProperty) MapProperty(io.swagger.models.properties.MapProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) TranslatorProperty(org.teiid.translator.TranslatorProperty) ExtensionMetadataProperty(org.teiid.metadata.ExtensionMetadataProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) FileProperty(io.swagger.models.properties.FileProperty) RefProperty(io.swagger.models.properties.RefProperty)

Example 69 with ArrayProperty

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");
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) ModelProperty(io.swagger.models.apideclaration.ModelProperty) Items(io.swagger.models.apideclaration.Items) DoubleProperty(io.swagger.models.properties.DoubleProperty) FloatProperty(io.swagger.models.properties.FloatProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) ModelProperty(io.swagger.models.apideclaration.ModelProperty) LongProperty(io.swagger.models.properties.LongProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Example 70 with ArrayProperty

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;
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty) MapProperty(io.swagger.models.properties.MapProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property)

Aggregations

ArrayProperty (io.swagger.models.properties.ArrayProperty)82 Property (io.swagger.models.properties.Property)62 StringProperty (io.swagger.models.properties.StringProperty)42 RefProperty (io.swagger.models.properties.RefProperty)39 Test (org.testng.annotations.Test)36 MapProperty (io.swagger.models.properties.MapProperty)25 Model (io.swagger.models.Model)23 IntegerProperty (io.swagger.models.properties.IntegerProperty)23 LongProperty (io.swagger.models.properties.LongProperty)14 ModelImpl (io.swagger.models.ModelImpl)13 BooleanProperty (io.swagger.models.properties.BooleanProperty)12 DoubleProperty (io.swagger.models.properties.DoubleProperty)12 RefModel (io.swagger.models.RefModel)10 FloatProperty (io.swagger.models.properties.FloatProperty)10 Operation (io.swagger.models.Operation)9 ByteArrayProperty (io.swagger.models.properties.ByteArrayProperty)9 DecimalProperty (io.swagger.models.properties.DecimalProperty)9 HashMap (java.util.HashMap)9 Response (io.swagger.models.Response)7 Swagger (io.swagger.models.Swagger)7