Search in sources :

Example 51 with Operation

use of io.swagger.models.Operation in project camel by apache.

the class SwaggerRestProducerFactory method getSwaggerOperation.

private Operation getSwaggerOperation(Swagger swagger, String verb, String path) {
    // path may include base path so skip that
    String basePath = swagger.getBasePath();
    if (basePath != null && path.startsWith(basePath)) {
        path = path.substring(basePath.length());
    }
    Path modelPath = swagger.getPath(path);
    if (modelPath == null) {
        return null;
    }
    // get,put,post,head,delete,patch,options
    Operation op = null;
    if ("get".equals(verb)) {
        op = modelPath.getGet();
    } else if ("put".equals(verb)) {
        op = modelPath.getPut();
    } else if ("post".equals(verb)) {
        op = modelPath.getPost();
    } else if ("head".equals(verb)) {
        op = modelPath.getHead();
    } else if ("delete".equals(verb)) {
        op = modelPath.getDelete();
    } else if ("patch".equals(verb)) {
        op = modelPath.getPatch();
    } else if ("options".equals(verb)) {
        op = modelPath.getOptions();
    }
    return op;
}
Also used : Path(io.swagger.models.Path) Operation(io.swagger.models.Operation)

Example 52 with Operation

use of io.swagger.models.Operation in project java-chassis by ServiceComb.

the class ClassUtils method createInterface.

private static Class<?> createInterface(Swagger swagger, ClassLoader classLoader, String packageName, String intfName) {
    ClassConfig classConfig = new ClassConfig();
    classConfig.setClassName(intfName);
    classConfig.setIntf(true);
    StringBuilder sbMethod = new StringBuilder();
    StringBuilder sbMethodGenericSignature = new StringBuilder();
    for (Path path : swagger.getPaths().values()) {
        for (Operation operation : path.getOperations()) {
            boolean hasGenericSignature = false;
            sbMethod.setLength(0);
            sbMethodGenericSignature.setLength(0);
            Response result = operation.getResponses().get(SwaggerConst.SUCCESS_KEY);
            JavaType resultJavaType = ConverterMgr.findJavaType(classLoader, packageName, swagger, result.getSchema());
            hasGenericSignature = hasGenericSignature || resultJavaType.hasGenericTypes();
            sbMethod.append(JavassistUtils.getNameForGenerateCode(resultJavaType)).append(" ").append(operation.getOperationId()).append("(");
            sbMethodGenericSignature.append("(");
            for (Parameter parameter : operation.getParameters()) {
                String paramName = parameter.getName();
                paramName = correctMethodParameterName(paramName);
                JavaType paramJavaType = ConverterMgr.findJavaType(classLoader, packageName, swagger, parameter);
                hasGenericSignature = hasGenericSignature || paramJavaType.hasGenericTypes();
                String code = String.format("%s %s,", paramJavaType.getRawClass().getName(), paramName);
                sbMethod.append(code);
                sbMethodGenericSignature.append(paramJavaType.getGenericSignature());
            }
            if (!operation.getParameters().isEmpty()) {
                sbMethod.setLength(sbMethod.length() - 1);
            }
            sbMethod.append(");");
            sbMethodGenericSignature.append(")");
            sbMethodGenericSignature.append(resultJavaType.getGenericSignature());
            if (hasGenericSignature) {
                classConfig.addMethod(sbMethod.toString(), sbMethodGenericSignature.toString());
            } else {
                classConfig.addMethod(sbMethod.toString(), null);
            }
        }
    }
    return JavassistUtils.createClass(classLoader, classConfig);
}
Also used : Path(io.swagger.models.Path) Response(io.swagger.models.Response) JavaType(com.fasterxml.jackson.databind.JavaType) ClassConfig(io.servicecomb.common.javassist.ClassConfig) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) Operation(io.swagger.models.Operation)

Example 53 with Operation

use of io.swagger.models.Operation in project java-chassis by ServiceComb.

the class TestApiResponse method checkApiResponseHeader.

private void checkApiResponseHeader(SwaggerGenerator generator) {
    Swagger swagger = generator.getSwagger();
    Path path = swagger.getPaths().get("/testApiResponseHeader");
    Operation operation = path.getOperations().get(0);
    Assert.assertEquals("testApiResponseHeader", operation.getOperationId());
    Response response = operation.getResponses().get("200");
    Property property = response.getHeaders().get("k1");
    Assert.assertEquals(Integer.class, ConverterMgr.findJavaType(generator, property).getRawClass());
    property = response.getHeaders().get("k2");
    Assert.assertEquals(String.class, ConverterMgr.findJavaType(generator, property).getRawClass());
}
Also used : Path(io.swagger.models.Path) Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) Property(io.swagger.models.properties.Property)

Example 54 with Operation

use of io.swagger.models.Operation in project java-chassis by ServiceComb.

the class RestOperationMeta method init.

public void init(OperationMeta operationMeta) {
    this.operationMeta = operationMeta;
    Swagger swagger = operationMeta.getSchemaMeta().getSwagger();
    Operation operation = operationMeta.getSwaggerOperation();
    this.produces = operation.getProduces();
    if (produces == null) {
        this.produces = swagger.getProduces();
    }
    setAbsolutePath(concatPath(swagger.getBasePath(), operationMeta.getOperationPath()));
    this.createProduceProcessors();
    Method method = operationMeta.getMethod();
    Type[] genericParamTypes = method.getGenericParameterTypes();
    if (genericParamTypes.length != operation.getParameters().size()) {
        throw new Error("Param count is not equal between swagger and method,  path=" + absolutePath);
    }
    // 初始化所有rest param
    for (int idx = 0; idx < genericParamTypes.length; idx++) {
        Parameter parameter = operation.getParameters().get(idx);
        Type genericParamType = genericParamTypes[idx];
        RestParam param = new RestParam(idx, parameter, genericParamType);
        addParam(param);
    }
    this.pathBuilder = new URLPathBuilder(absolutePath, paramMap);
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) Swagger(io.swagger.models.Swagger) Parameter(io.swagger.models.parameters.Parameter) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) Operation(io.swagger.models.Operation) Method(java.lang.reflect.Method)

Example 55 with Operation

use of io.swagger.models.Operation in project cxf by apache.

the class Swagger2Customizer method customize.

public Swagger customize(Swagger data) {
    if (dynamicBasePath) {
        MessageContext ctx = createMessageContext();
        String currentBasePath = StringUtils.substringBeforeLast(ctx.getHttpServletRequest().getRequestURI(), "/");
        data.setBasePath(currentBasePath);
        if (data.getHost() == null) {
            data.setHost(beanConfig.getHost());
        }
        if (data.getInfo() == null) {
            data.setInfo(beanConfig.getInfo());
        }
        if (beanConfig.getSwagger() != null && beanConfig.getSwagger().getSecurityDefinitions() != null && data.getSecurityDefinitions() == null) {
            data.setSecurityDefinitions(beanConfig.getSwagger().getSecurityDefinitions());
        }
    }
    if (replaceTags || javadocProvider != null) {
        Map<String, ClassResourceInfo> operations = new HashMap<>();
        Map<Pair<String, String>, OperationResourceInfo> methods = new HashMap<>();
        for (ClassResourceInfo cri : cris) {
            for (OperationResourceInfo ori : cri.getMethodDispatcher().getOperationResourceInfos()) {
                String normalizedPath = getNormalizedPath(cri.getURITemplate().getValue(), ori.getURITemplate().getValue());
                operations.put(normalizedPath, cri);
                methods.put(ImmutablePair.of(ori.getHttpMethod(), normalizedPath), ori);
            }
        }
        if (replaceTags && data.getTags() != null) {
            data.getTags().clear();
        }
        for (final Map.Entry<String, Path> entry : data.getPaths().entrySet()) {
            Tag tag = null;
            if (replaceTags && operations.containsKey(entry.getKey())) {
                ClassResourceInfo cri = operations.get(entry.getKey());
                tag = new Tag();
                tag.setName(cri.getURITemplate().getValue().replaceAll("/", "_"));
                if (javadocProvider != null) {
                    tag.setDescription(javadocProvider.getClassDoc(cri));
                }
                data.addTag(tag);
            }
            for (Map.Entry<HttpMethod, Operation> subentry : entry.getValue().getOperationMap().entrySet()) {
                if (replaceTags && tag != null) {
                    subentry.getValue().setTags(Collections.singletonList(tag.getName()));
                }
                Pair<String, String> key = ImmutablePair.of(subentry.getKey().name(), entry.getKey());
                if (methods.containsKey(key) && javadocProvider != null) {
                    OperationResourceInfo ori = methods.get(key);
                    subentry.getValue().setSummary(javadocProvider.getMethodDoc(ori));
                    for (int i = 0; i < subentry.getValue().getParameters().size(); i++) {
                        subentry.getValue().getParameters().get(i).setDescription(javadocProvider.getMethodParameterDoc(ori, i));
                    }
                    addParameters(subentry.getValue().getParameters());
                    if (subentry.getValue().getResponses() != null && !subentry.getValue().getResponses().isEmpty()) {
                        subentry.getValue().getResponses().entrySet().iterator().next().getValue().setDescription(javadocProvider.getMethodResponseDoc(ori));
                    }
                }
            }
        }
    }
    if (replaceTags && data.getTags() != null) {
        Collections.sort(data.getTags(), new Comparator<Tag>() {

            @Override
            public int compare(final Tag tag1, final Tag tag2) {
                return tag1.getName().compareTo(tag2.getName());
            }
        });
    }
    applyDefaultVersion(data);
    return data;
}
Also used : Path(io.swagger.models.Path) HashMap(java.util.HashMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Operation(io.swagger.models.Operation) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) Tag(io.swagger.models.Tag) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(io.swagger.models.HttpMethod) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

Operation (io.swagger.models.Operation)118 Test (org.testng.annotations.Test)80 Swagger (io.swagger.models.Swagger)45 Path (io.swagger.models.Path)39 BodyParameter (io.swagger.models.parameters.BodyParameter)27 Property (io.swagger.models.properties.Property)24 Response (io.swagger.models.Response)23 Parameter (io.swagger.models.parameters.Parameter)20 RefProperty (io.swagger.models.properties.RefProperty)16 ArrayModel (io.swagger.models.ArrayModel)15 Model (io.swagger.models.Model)14 QueryParameter (io.swagger.models.parameters.QueryParameter)13 StringProperty (io.swagger.models.properties.StringProperty)13 HashMap (java.util.HashMap)13 ArrayProperty (io.swagger.models.properties.ArrayProperty)12 ReaderContext (io.swagger.servlet.ReaderContext)12 PathParameter (io.swagger.models.parameters.PathParameter)11 ArrayList (java.util.ArrayList)11 MapProperty (io.swagger.models.properties.MapProperty)10 NicknamedOperation (io.swagger.resources.NicknamedOperation)10