Search in sources :

Example 86 with ModelImpl

use of io.swagger.models.ModelImpl in project candlepin by candlepin.

the class CandlepinSwaggerModelConverter method resolveSubtypes.

private boolean resolveSubtypes(ModelImpl model, BeanDescription bean, ModelConverterContext context) {
    final List<NamedType> types = pIntr.findSubtypes(bean.getClassInfo());
    if (types == null) {
        return false;
    }
    int count = 0;
    final Class<?> beanClass = bean.getClassInfo().getAnnotated();
    for (NamedType subtype : types) {
        final Class<?> subtypeType = subtype.getType();
        if (!beanClass.isAssignableFrom(subtypeType)) {
            continue;
        }
        final Model subtypeModel = context.resolve(subtypeType);
        if (subtypeModel instanceof ModelImpl) {
            final ModelImpl impl = (ModelImpl) subtypeModel;
            // check if model name was inherited
            if (impl.getName().equals(model.getName())) {
                impl.setName(pTypeNameResolver.nameForType(pMapper.constructType(subtypeType), TypeNameResolver.Options.SKIP_API_MODEL));
            }
            // remove shared properties defined in the parent
            final Map<String, Property> baseProps = model.getProperties();
            final Map<String, Property> subtypeProps = impl.getProperties();
            if (baseProps != null && subtypeProps != null) {
                for (Map.Entry<String, Property> entry : baseProps.entrySet()) {
                    if (entry.getValue().equals(subtypeProps.get(entry.getKey()))) {
                        subtypeProps.remove(entry.getKey());
                    }
                }
            }
            impl.setDiscriminator(null);
            ComposedModel child = new ComposedModel().parent(new RefModel(model.getName())).child(impl);
            context.defineModel(impl.getName(), child);
            ++count;
        }
    }
    return count != 0;
}
Also used : RefModel(io.swagger.models.RefModel) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ApiModel(io.swagger.annotations.ApiModel) ModelImpl(io.swagger.models.ModelImpl) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) UUIDProperty(io.swagger.models.properties.UUIDProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 87 with ModelImpl

use of io.swagger.models.ModelImpl 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 88 with ModelImpl

use of io.swagger.models.ModelImpl in project teiid by teiid.

the class SwaggerMetadataProcessor method addProcedureParameters.

private void addProcedureParameters(final MetadataFactory mf, final Swagger swagger, final Procedure procedure, final Operation operation) throws TranslatorException {
    for (final Parameter parameter : operation.getParameters()) {
        if (parameter instanceof BodyParameter) {
            PropertyAction pa = new PropertyAction() {

                @Override
                public void execute(String name, String nameInSource, Property property, boolean array) {
                    String type = getPropertyType(property, array);
                    if (procedure.getParameterByName(nameInSource) == null) {
                        ProcedureParameter param = mf.addProcedureParameter(name, type, Type.In, procedure);
                        param.setProperty(PARAMETER_TYPE, parameter.getIn());
                        if (property != null && !property.getRequired()) {
                            param.setProperty(BaseColumn.DEFAULT_HANDLING, BaseColumn.OMIT_DEFAULT);
                        }
                        param.setNullType(NullType.No_Nulls);
                        param.setAnnotation(property != null ? property.getDescription() : null);
                        if (!name.equalsIgnoreCase(nameInSource)) {
                            param.setNameInSource(nameInSource);
                        }
                    }
                }
            };
            Model model = ((BodyParameter) parameter).getSchema();
            if (model instanceof RefModel) {
                RefModel refModel = (RefModel) model;
                if (refModel.getProperties() != null) {
                    walkProperties(swagger, refModel.getProperties(), null, null, pa);
                } else if (refModel.getReference() != null) {
                    Model m = swagger.getDefinitions().get(refModel.getSimpleRef());
                    walkProperties(swagger, m.getProperties(), null, null, pa);
                }
            } else {
                if ((model instanceof ModelImpl) && model.getProperties() != null) {
                    walkProperties(swagger, model.getProperties(), null, null, pa);
                } else {
                    ProcedureParameter p = mf.addProcedureParameter(parameter.getName(), DataTypeManager.DefaultDataTypes.CLOB, Type.In, procedure);
                    p.setProperty(PARAMETER_TYPE, parameter.getIn());
                    p.setNullType(NullType.No_Nulls);
                    p.setAnnotation(parameter.getDescription());
                }
            }
        } else {
            String name = parameter.getName();
            ProcedureParameter pp = null;
            String type = null;
            Object defaultValue = null;
            String collectionFormat = null;
            if (parameter instanceof AbstractSerializableParameter) {
                AbstractSerializableParameter p = (AbstractSerializableParameter) parameter;
                type = p.getType();
                if (p.getType().equalsIgnoreCase("array")) {
                    Property ap = p.getItems();
                    type = ap.getType();
                }
                type = SwaggerTypeManager.teiidType(type, p.getFormat(), p.getItems() != null);
                defaultValue = p.getDefaultValue();
                collectionFormat = p.getCollectionFormat();
            } else {
                // $NON-NLS-1$
                throw new MetadataException("Unknown property type" + parameter.getClass().getName());
            }
            pp = mf.addProcedureParameter(name, type, Type.In, procedure);
            pp.setProperty(PARAMETER_TYPE, parameter.getIn());
            boolean required = parameter.getRequired();
            if (!required) {
                pp.setProperty(BaseColumn.DEFAULT_HANDLING, BaseColumn.OMIT_DEFAULT);
            }
            pp.setNullType(NullType.No_Nulls);
            pp.setAnnotation(parameter.getDescription());
            if (defaultValue != null) {
                pp.setDefaultValue(defaultValue.toString());
            }
            if (collectionFormat != null) {
                pp.setProperty(COLLECION_FORMAT, collectionFormat);
            }
            // extended properties
            for (Entry<String, Object> extension : parameter.getVendorExtensions().entrySet()) {
                pp.setProperty(extension.getKey(), extension.getValue().toString());
            }
        }
    }
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter) MetadataException(org.teiid.metadata.MetadataException) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) ProcedureParameter(org.teiid.metadata.ProcedureParameter) ModelImpl(io.swagger.models.ModelImpl) 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 89 with ModelImpl

use of io.swagger.models.ModelImpl in project swagger-parser by swagger-api.

the class BodyParameterConverterTest method convertInt32BodyParameter.

@Test
public void convertInt32BodyParameter() throws Exception {
    io.swagger.models.apideclaration.Parameter param = new io.swagger.models.apideclaration.Parameter();
    param.setParamType(ParamType.BODY);
    param.setDescription("an int body param");
    param.setRequired(false);
    param.setAllowMultiple(false);
    param.setType("integer");
    param.setFormat(Format.INT32);
    Parameter converted = converter.convertParameter(param);
    assertTrue(converted.getClass().equals(BodyParameter.class));
    BodyParameter bp = (BodyParameter) converted;
    ModelImpl m = (ModelImpl) bp.getSchema();
    assertEquals(param.getType(), m.getType());
    assertEquals(param.getDescription(), bp.getDescription());
    assertEquals((Boolean) param.getRequired(), (Boolean) bp.getRequired());
}
Also used : BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) Test(org.testng.annotations.Test)

Example 90 with ModelImpl

use of io.swagger.models.ModelImpl in project swagger-parser by swagger-api.

the class BodyParameterConverterTest method convertBooleanBodyParameter.

@Test
public void convertBooleanBodyParameter() throws Exception {
    io.swagger.models.apideclaration.Parameter param = new io.swagger.models.apideclaration.Parameter();
    param.setParamType(ParamType.BODY);
    param.setDescription("a boolean body param");
    param.setRequired(false);
    param.setAllowMultiple(false);
    param.setType("boolean");
    Parameter converted = converter.convertParameter(param);
    assertTrue(converted.getClass().equals(BodyParameter.class));
    BodyParameter bp = (BodyParameter) converted;
    ModelImpl m = (ModelImpl) bp.getSchema();
    assertEquals(param.getType(), m.getType());
    assertEquals(param.getDescription(), bp.getDescription());
    assertEquals((Boolean) param.getRequired(), (Boolean) bp.getRequired());
}
Also used : BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) Test(org.testng.annotations.Test)

Aggregations

ModelImpl (io.swagger.models.ModelImpl)112 Test (org.testng.annotations.Test)46 Model (io.swagger.models.Model)43 Property (io.swagger.models.properties.Property)40 StringProperty (io.swagger.models.properties.StringProperty)32 RefProperty (io.swagger.models.properties.RefProperty)29 BodyParameter (io.swagger.models.parameters.BodyParameter)28 ArrayProperty (io.swagger.models.properties.ArrayProperty)27 RefModel (io.swagger.models.RefModel)24 IntegerProperty (io.swagger.models.properties.IntegerProperty)18 Swagger (io.swagger.models.Swagger)16 JavaType (com.fasterxml.jackson.databind.JavaType)15 Map (java.util.Map)15 MapProperty (io.swagger.models.properties.MapProperty)14 Test (org.junit.Test)14 Parameter (io.swagger.models.parameters.Parameter)12 HashMap (java.util.HashMap)12 ArrayModel (io.swagger.models.ArrayModel)11 ComposedModel (io.swagger.models.ComposedModel)11 ArrayList (java.util.ArrayList)11