Search in sources :

Example 51 with RefProperty

use of io.swagger.models.properties.RefProperty in project herd by FINRAOS.

the class RestControllerProcessor method processRestMethodReturnValue.

/**
 * Processes the return value of a RequestMapping annotated method.
 *
 * @param returnType the return type.
 * @param operation the operation.
 * @param returnDescription the description of the return value.
 *
 * @throws MojoExecutionException if the return type isn't an XmlType.
 */
private void processRestMethodReturnValue(Class<?> returnType, Operation operation, String returnDescription) throws MojoExecutionException {
    log.debug("Processing REST method return value \"" + returnType.getName() + "\".");
    // Add the class name to the list of classes which we will create an example for.
    exampleClassNames.add(returnType.getSimpleName());
    // Add the success response
    operation.response(200, new Response().description(returnDescription == null ? "Success" : returnDescription).schema(new RefProperty(getXmlType(returnType).name().trim())));
    // If we have an error class, add that as the default response.
    if (modelErrorClass != null) {
        operation.defaultResponse(new Response().description("General Error").schema(new RefProperty(getXmlType(modelErrorClass).name().trim())));
    }
}
Also used : Response(io.swagger.models.Response) RefProperty(io.swagger.models.properties.RefProperty)

Example 52 with RefProperty

use of io.swagger.models.properties.RefProperty 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 53 with RefProperty

use of io.swagger.models.properties.RefProperty 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 54 with RefProperty

use of io.swagger.models.properties.RefProperty 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 55 with RefProperty

use of io.swagger.models.properties.RefProperty in project endpoints-java by cloudendpoints.

the class SwaggerGenerator method convertToSwaggerProperty.

private Property convertToSwaggerProperty(Field f) {
    Property p = null;
    Class<? extends Property> propertyClass = FIELD_TYPE_TO_PROPERTY_CLASS_MAP.get(f.type());
    if (propertyClass != null) {
        try {
            p = propertyClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
        // cannot happen, as Property subclasses are guaranteed to have a default constructor
        }
    } else {
        if (f.type() == FieldType.OBJECT || f.type() == FieldType.ENUM) {
            p = new RefProperty(f.schemaReference().get().name());
        } else if (f.type() == FieldType.ARRAY) {
            p = new ArrayProperty(convertToSwaggerProperty(f.arrayItemSchema()));
        }
    }
    if (p == null) {
        throw new IllegalArgumentException("could not convert field " + f);
    }
    p.description(f.description());
    return p;
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) ByteArrayProperty(io.swagger.models.properties.ByteArrayProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) ByteArrayProperty(io.swagger.models.properties.ByteArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) FloatProperty(io.swagger.models.properties.FloatProperty) DateProperty(io.swagger.models.properties.DateProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) RefProperty(io.swagger.models.properties.RefProperty)

Aggregations

RefProperty (io.swagger.models.properties.RefProperty)74 Property (io.swagger.models.properties.Property)50 ArrayProperty (io.swagger.models.properties.ArrayProperty)46 StringProperty (io.swagger.models.properties.StringProperty)35 Test (org.testng.annotations.Test)35 Model (io.swagger.models.Model)23 MapProperty (io.swagger.models.properties.MapProperty)20 ModelImpl (io.swagger.models.ModelImpl)18 Response (io.swagger.models.Response)18 IntegerProperty (io.swagger.models.properties.IntegerProperty)18 Operation (io.swagger.models.Operation)17 RefModel (io.swagger.models.RefModel)17 LongProperty (io.swagger.models.properties.LongProperty)14 Path (io.swagger.models.Path)12 Swagger (io.swagger.models.Swagger)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 ArrayModel (io.swagger.models.ArrayModel)11 BodyParameter (io.swagger.models.parameters.BodyParameter)11 Map (java.util.Map)11