Search in sources :

Example 46 with PathParameter

use of io.swagger.models.parameters.PathParameter 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 47 with PathParameter

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

the class PathParameterConverterTest method convertStringPathParameter.

@Test
public void convertStringPathParameter() throws Exception {
    io.swagger.models.apideclaration.Parameter param = new io.swagger.models.apideclaration.Parameter();
    param.setParamType(ParamType.PATH);
    param.setDescription("a string path param");
    param.setRequired(false);
    param.setAllowMultiple(false);
    param.setType("string");
    Parameter converted = converter.convertParameter(param);
    assertTrue(converted.getClass().equals(PathParameter.class));
    PathParameter pp = (PathParameter) converted;
    assertEquals(param.getType(), pp.getType());
    assertEquals(param.getDescription(), pp.getDescription());
    assertTrue(pp.getRequired());
    assertNull(pp.getCollectionFormat());
}
Also used : PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) PathParameter(io.swagger.models.parameters.PathParameter) Test(org.testng.annotations.Test)

Example 48 with PathParameter

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

the class SwaggerParserTest method testCodegenPetstore.

@Test
public void testCodegenPetstore() {
    SwaggerParser parser = new SwaggerParser();
    final Swagger swagger = parser.read("src/test/resources/petstore-codegen.yaml");
    ModelImpl enumModel = (ModelImpl) swagger.getDefinitions().get("Enum_Test");
    assertNotNull(enumModel);
    Property enumProperty = enumModel.getProperties().get("enum_integer");
    assertNotNull(enumProperty);
    assertTrue(enumProperty instanceof IntegerProperty);
    IntegerProperty enumIntegerProperty = (IntegerProperty) enumProperty;
    List<Integer> integers = enumIntegerProperty.getEnum();
    assertEquals(integers.get(0), new Integer(1));
    assertEquals(integers.get(1), new Integer(-1));
    Operation getOrderOperation = swagger.getPath("/store/order/{orderId}").getGet();
    assertNotNull(getOrderOperation);
    Parameter orderId = getOrderOperation.getParameters().get(0);
    assertTrue(orderId instanceof PathParameter);
    PathParameter orderIdPathParam = (PathParameter) orderId;
    assertNotNull(orderIdPathParam.getMinimum());
    BigDecimal minimum = orderIdPathParam.getMinimum();
    assertEquals(minimum.toString(), "1");
    FormParameter formParam = (FormParameter) swagger.getPath("/fake").getPost().getParameters().get(3);
    assertEquals(formParam.getMinimum().toString(), "32.1");
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) Swagger(io.swagger.models.Swagger) SerializableParameter(io.swagger.models.parameters.SerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) FormParameter(io.swagger.models.parameters.FormParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) Operation(io.swagger.models.Operation) ModelImpl(io.swagger.models.ModelImpl) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) ByteArrayProperty(io.swagger.models.properties.ByteArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) PathParameter(io.swagger.models.parameters.PathParameter) FormParameter(io.swagger.models.parameters.FormParameter) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 49 with PathParameter

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

the class SwaggerCompatConverter method convertParameter.

public Parameter convertParameter(io.swagger.models.apideclaration.Parameter param) {
    Parameter output = null;
    List<String> _enum = param.getEnumValues();
    if (ParamType.PATH.equals(param.getParamType())) {
        PathParameter p = new PathParameter();
        p.setDefaultValue(param.getDefaultValue());
        p.setEnum(_enum);
        output = p;
    } else if (ParamType.QUERY.equals(param.getParamType())) {
        QueryParameter p = new QueryParameter();
        p.setDefaultValue(param.getDefaultValue());
        p.setEnum(_enum);
        output = p;
    } else if (ParamType.HEADER.equals(param.getParamType())) {
        HeaderParameter p = new HeaderParameter();
        p.setDefaultValue(param.getDefaultValue());
        p.setEnum(_enum);
        output = p;
    } else if (ParamType.BODY.equals(param.getParamType())) {
        BodyParameter p = new BodyParameter();
        output = p;
    } else if (ParamType.FORM.equals(param.getParamType())) {
        FormParameter p = new FormParameter();
        p.setDefaultValue(param.getDefaultValue());
        p.setEnum(_enum);
        output = p;
    }
    output.setName(param.getName());
    output.setDescription(param.getDescription());
    if (param.getRequired() != null) {
        output.setRequired(param.getRequired());
    }
    Property property = null;
    String type = param.getType() == null ? null : param.getType().toString();
    String format = param.getFormat() == null ? null : param.getFormat().toString();
    if (null == type) {
        LOGGER.warn("Empty type in Param: " + param);
    }
    if (output instanceof BodyParameter) {
        BodyParameter bp = (BodyParameter) output;
        bp.setSchema(modelFromExtendedTypedObject(param));
    } else if (output instanceof SerializableParameter) {
        SerializableParameter sp = (SerializableParameter) output;
        Property p = null;
        if (param.getAllowMultiple() != null && param.getAllowMultiple() == true) {
            ArrayProperty arrayProperty = new ArrayProperty();
            Property innerType = PropertyBuilder.build(type, format, null);
            arrayProperty.setItems(innerType);
            p = arrayProperty;
        } else {
            p = propertyFromTypedObject(param);
            if (p == null) {
                LOGGER.warn(String.format("WARNING! No property detected for parameter '%s' (%s)! Falling back to string!", param.getName(), param.getParamType()));
                p = new StringProperty();
            }
        }
        if (p instanceof ArrayProperty) {
            ArrayProperty ap = (ArrayProperty) p;
            sp.setType("array");
            sp.setCollectionFormat("csv");
            sp.setItems(ap.getItems());
        } else {
            sp.setType(p.getType());
            sp.setFormat(p.getFormat());
        }
    }
    // all path parameters are required
    if (output instanceof PathParameter) {
        ((PathParameter) output).setRequired(true);
    }
    return output;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) QueryParameter(io.swagger.models.parameters.QueryParameter) ArrayProperty(io.swagger.models.properties.ArrayProperty) SerializableParameter(io.swagger.models.parameters.SerializableParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) StringProperty(io.swagger.models.properties.StringProperty) HeaderParameter(io.swagger.models.parameters.HeaderParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) FormParameter(io.swagger.models.parameters.FormParameter) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) ModelProperty(io.swagger.models.apideclaration.ModelProperty) RefProperty(io.swagger.models.properties.RefProperty) UntypedProperty(io.swagger.models.properties.UntypedProperty)

Example 50 with PathParameter

use of io.swagger.models.parameters.PathParameter in project killbill by killbill.

the class KillBillApiDefinition method decorateOperation.

private void decorateOperation(final Operation op, final String pathName, final String httpMethod) {
    if (op != null) {
        // Bug in swagger ? somehow when we only specify a 201, swagger adds a 200 response with the schema response
        if (httpMethod.equals("POST")) {
            if (op.getResponses().containsKey("201") && op.getResponses().containsKey("200")) {
                final Response resp200 = op.getResponses().remove("200");
                final Response resp201 = op.getResponses().get("201");
                if (resp201.getSchema() == null) {
                    resp201.setSchema(resp200.getSchema());
                }
            }
        }
        op.addSecurity(BASIC_AUTH_SCHEME, null);
        if (requiresTenantInformation(pathName, httpMethod)) {
            op.addSecurity(API_KEY_SCHEME, null);
            op.addSecurity(API_SECRET_SCHEME, null);
        }
        for (Parameter p : op.getParameters()) {
            if (p instanceof BodyParameter) {
                p.setRequired(true);
            } else if (p instanceof PathParameter) {
                p.setRequired(true);
            } else if (p instanceof HeaderParameter) {
                if (p.getName().equals(HDR_CREATED_BY)) {
                    p.setRequired(true);
                }
            } else if (p instanceof QueryParameter) {
                QueryParameter qp = (QueryParameter) p;
                if (qp.getName().equals(QUERY_AUDIT)) {
                    qp.setRequired(false);
                    qp.setType("string");
                    final List<String> values = ImmutableList.copyOf(Iterables.transform(ImmutableList.<AuditLevel>copyOf(AuditLevel.values()), new Function<AuditLevel, String>() {

                        @Override
                        public String apply(final AuditLevel input) {
                            return input.toString();
                        }
                    }));
                    qp.setEnum(values);
                } else if (qp.getName().equals(JaxrsResource.QUERY_REQUESTED_DT) || qp.getName().equals(JaxrsResource.QUERY_ENTITLEMENT_REQUESTED_DT) || qp.getName().equals(JaxrsResource.QUERY_BILLING_REQUESTED_DT) || qp.getName().equals(JaxrsResource.QUERY_ENTITLEMENT_EFFECTIVE_FROM_DT) || qp.getName().equals(JaxrsResource.QUERY_START_DATE) || qp.getName().equals(JaxrsResource.QUERY_END_DATE) || qp.getName().equals(JaxrsResource.QUERY_TARGET_DATE)) {
                    qp.setType("string");
                    // Yack... See #922
                    if (op.getOperationId().equals("getCatalogJson") || op.getOperationId().equals("getCatalogXml") || op.getOperationId().equals("setTestClockTime")) {
                        qp.setFormat("date-time");
                    } else {
                        qp.setFormat("date");
                    }
                }
            }
        }
    }
}
Also used : Response(io.swagger.models.Response) QueryParameter(io.swagger.models.parameters.QueryParameter) AuditLevel(org.killbill.billing.util.api.AuditLevel) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) BodyParameter(io.swagger.models.parameters.BodyParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) PathParameter(io.swagger.models.parameters.PathParameter)

Aggregations

PathParameter (io.swagger.models.parameters.PathParameter)51 QueryParameter (io.swagger.models.parameters.QueryParameter)35 Parameter (io.swagger.models.parameters.Parameter)32 Test (org.testng.annotations.Test)32 BodyParameter (io.swagger.models.parameters.BodyParameter)26 HeaderParameter (io.swagger.models.parameters.HeaderParameter)16 Swagger (io.swagger.models.Swagger)14 StringProperty (io.swagger.models.properties.StringProperty)12 FormParameter (io.swagger.models.parameters.FormParameter)11 Operation (io.swagger.models.Operation)10 SerializableParameter (io.swagger.models.parameters.SerializableParameter)10 Response (io.swagger.models.Response)8 IntegerProperty (io.swagger.models.properties.IntegerProperty)8 RefProperty (io.swagger.models.properties.RefProperty)8 Path (io.swagger.models.Path)6 ArrayProperty (io.swagger.models.properties.ArrayProperty)6 HashMap (java.util.HashMap)6 ModelImpl (io.swagger.models.ModelImpl)5 RefModel (io.swagger.models.RefModel)5 ArrayList (java.util.ArrayList)5