Search in sources :

Example 1 with KeyedList

use of com.terran4j.commons.util.value.KeyedList in project commons by terran4j.

the class ApiResultObjectTest method testParseResultType.

@Test
public void testParseResultType() throws Exception {
    log.info("testParseResultType");
    Method method = ReflectionUtils.findMethod(getClass(), "getUsers");
    Assert.assertNotNull(method);
    KeyedList<String, ApiResultObject> totalResults = new KeyedList<>();
    ApiResultObject object = ApiResultObject.parseResultType(method, totalResults);
    Assert.assertNotNull(object);
    Assert.assertEquals(1, totalResults.size());
    Assert.assertTrue(object == totalResults.get(0));
    Assert.assertTrue(ApiDataType.ARRAY == object.getDataType());
    Assert.assertTrue(User.class == object.getSourceType());
}
Also used : KeyedList(com.terran4j.commons.util.value.KeyedList) Method(java.lang.reflect.Method) ApiResultObject(com.terran4j.commons.api2doc.domain.ApiResultObject) Test(org.junit.Test)

Example 2 with KeyedList

use of com.terran4j.commons.util.value.KeyedList in project commons by terran4j.

the class ApiResultObjectTest method testParseResultTypeWithIgnore.

@Test
public void testParseResultTypeWithIgnore() throws Exception {
    log.info("testParseResultTypeWithIgnore");
    Method method = ReflectionUtils.findMethod(getClass(), "getUsers");
    Assert.assertNotNull(method);
    KeyedList<String, ApiResultObject> totalResults = new KeyedList<>();
    ApiResultObject object = ApiResultObject.parseResultType(method, totalResults);
    Assert.assertNotNull(object);
    Assert.assertNull(object.getChild("deleted"));
}
Also used : KeyedList(com.terran4j.commons.util.value.KeyedList) Method(java.lang.reflect.Method) ApiResultObject(com.terran4j.commons.api2doc.domain.ApiResultObject) Test(org.junit.Test)

Example 3 with KeyedList

use of com.terran4j.commons.util.value.KeyedList in project commons by terran4j.

the class Api2DocCollector method getApiDoc.

ApiDocObject getApiDoc(MappingMethod mappingMethod, String[] basePaths, String beanName, Api2Doc classApi2Doc, Class<?> defaultSeeClass) throws BusinessException {
    Method method = mappingMethod.getMethod();
    // 只要有 @ApiDoc 注解(无论是本方法上,还是类上),有会生成文档,没有这个注解就不会。
    Api2Doc api2Doc = method.getAnnotation(Api2Doc.class);
    if (api2Doc == null && classApi2Doc == null) {
        return null;
    }
    ApiDocObject doc = new ApiDocObject();
    doc.setSourceMethod(method);
    // 获取文档的 id,以 @Api2Doc、方法名 为顺序获取。
    String id = method.getName();
    if (api2Doc != null && StringUtils.hasText(api2Doc.value())) {
        id = api2Doc.value();
    }
    if (api2Doc != null && StringUtils.hasText(api2Doc.id())) {
        id = api2Doc.id();
    }
    doc.setId(id);
    checkId(id);
    // 获取文档的排序。
    if (api2Doc != null) {
        doc.setOrder(api2Doc.order());
    }
    // 获取文档名称,按 @Api2Doc 、@Mapping、方法名的顺序获取。
    String name = method.getName();
    String mappingName = mappingMethod.getName();
    if (StringUtils.hasText(mappingName)) {
        name = mappingName;
    }
    if (api2Doc != null && StringUtils.hasText(api2Doc.name())) {
        name = api2Doc.name();
    }
    doc.setName(name);
    // 获取 API 的注释信息。
    ApiComment apiComment = method.getAnnotation(ApiComment.class);
    defaultSeeClass = ApiCommentUtils.getDefaultSeeClass(apiComment, defaultSeeClass);
    doc.setComment(ApiCommentUtils.getComment(apiComment, defaultSeeClass, name));
    doc.setSample(ApiCommentUtils.getSample(apiComment, defaultSeeClass, name));
    // 获取 API 的访问路径。
    String[] paths = mappingMethod.getPath();
    paths = combine(basePaths, paths);
    doc.setPaths(paths);
    // 获取 HTTP 方法。
    doc.setMethods(mappingMethod.getRequestMethod());
    // 收集参数信息。
    List<ApiParamObject> apiParams = toApiParams(method, defaultSeeClass);
    if (apiParams != null && apiParams.size() > 0) {
        for (ApiParamObject apiParam : apiParams) {
            doc.addParam(apiParam);
        }
    }
    // 收集返回值信息。
    KeyedList<String, ApiResultObject> totalResults = new KeyedList<>();
    ApiResultObject.parseResultType(method, totalResults);
    doc.setResults(totalResults.getAll());
    // 确定返回类型的描述。
    String returnTypeDesc = null;
    List<ApiResultObject> results = doc.getResults();
    if (results != null && results.size() > 0) {
        ApiResultObject result = results.get(0);
        ApiDataType dataType = result.getDataType();
        if (dataType != null) {
            if (dataType == ApiDataType.ARRAY) {
                returnTypeDesc = result.getSourceType().getSimpleName() + "[]";
            } else {
                returnTypeDesc = result.getSourceType().getSimpleName();
            }
        }
    }
    if (returnTypeDesc == null) {
        Class<?> returnType = doc.getSourceMethod().getReturnType();
        if (returnType != null && returnType != void.class) {
            returnTypeDesc = returnType.getSimpleName();
        }
    }
    doc.setReturnTypeDesc(returnTypeDesc);
    // 收集错误码信息。
    ApiErrors errorCodes = method.getAnnotation(ApiErrors.class);
    if (errorCodes != null && errorCodes.value() != null && errorCodes.value().length > 0) {
        for (ApiError errorCode : errorCodes.value()) {
            ApiErrorObject error = getError(errorCode);
            if (error == null) {
                continue;
            }
            doc.addError(error);
        }
    } else {
        ApiError errorCode = method.getAnnotation(ApiError.class);
        ApiErrorObject error = getError(errorCode);
        if (error != null) {
            doc.addError(error);
        }
    }
    return doc;
}
Also used : KeyedList(com.terran4j.commons.util.value.KeyedList) ApiErrors(com.terran4j.commons.api2doc.annotations.ApiErrors) Method(java.lang.reflect.Method) Api2Doc(com.terran4j.commons.api2doc.annotations.Api2Doc) ApiComment(com.terran4j.commons.api2doc.annotations.ApiComment) ApiError(com.terran4j.commons.api2doc.annotations.ApiError)

Example 4 with KeyedList

use of com.terran4j.commons.util.value.KeyedList in project commons by terran4j.

the class CurlBuilder method toCurl.

public static String toCurl(ApiDocObject docObject, String serverURL) {
    final List<ApiParamObject> allParams = docObject.getParams();
    final KeyedList<String, String> headers = new KeyedList<>();
    final KeyedList<String, String> params = new KeyedList<>();
    final KeyedList<String, String> cookies = new KeyedList<>();
    final Map<String, String> pathVars = new HashMap<>();
    if (allParams.size() > 0) {
        for (ApiParamObject param : allParams) {
            String key = param.getId();
            String value = param.getSample().getValue();
            if (StringUtils.isEmpty(value)) {
                Class<?> paramType = param.getSourceType();
                if (paramType == String.class) {
                    value = key;
                } else {
                    value = param.getDataType().getDefault();
                }
            }
            if (param.getLocation() == ApiParamLocation.RequestParam) {
                params.add(key, value);
            }
            if (param.getLocation() == ApiParamLocation.PathVariable) {
                pathVars.put(key, value);
            }
            if (param.getLocation() == ApiParamLocation.RequestHeader) {
                headers.add(key, value);
            }
            if (param.getLocation() == ApiParamLocation.CookieValue) {
                cookies.add(key, value);
            }
        }
    }
    StringBuilder sb = new StringBuilder("curl ");
    sb.append(enter);
    // 将 Header 参数拼接起来。
    if (headers.size() > 0) {
        for (int i = 0; i < headers.size(); i++) {
            String key = headers.getKey(i);
            String value = headers.get(i);
            sb.append(" -H \"").append(key).append(": ").append(value).append("\" ").append(enter);
        }
    }
    if (cookies.size() > 0) {
        sb.append(" -b \"");
        sb.append(joinText(cookies, ";", "="));
        sb.append("\" ").append(enter);
    }
    // 将 URL 中的 {xx} 变量用参数的示例值代替。
    String url = serverURL + docObject.getPaths()[0];
    if (pathVars.size() > 0) {
        ValueSource<String, String> vars = new ValueSource<String, String>() {

            @Override
            public String get(String key) {
                return encode(pathVars.get(key));
            }
        };
        url = Strings.format(url, vars, "{", "}", null);
    }
    // 将“参数”拼起来。
    if (params.size() > 0) {
        RequestMethod[] requestMethods = docObject.getMethods();
        if (requestMethods.length == 1 && requestMethods[0] == RequestMethod.POST) {
            sb.append(" -d \"").append(joinText(params, "&", "=")).append("\" ").append(enter);
        } else {
            url += ("?" + joinText(params, "&", "="));
        }
    }
    // 将 URL 拼接起来。
    sb.append(" \"").append(url).append("\"");
    String curl = sb.toString();
    if (log.isInfoEnabled()) {
        log.info("doc[{}]'s curl:\n{}", docObject.getId(), curl);
    }
    return curl;
}
Also used : HashMap(java.util.HashMap) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) KeyedList(com.terran4j.commons.util.value.KeyedList) ApiParamObject(com.terran4j.commons.api2doc.domain.ApiParamObject) ValueSource(com.terran4j.commons.util.value.ValueSource)

Example 5 with KeyedList

use of com.terran4j.commons.util.value.KeyedList in project commons by terran4j.

the class ApiResultObjectTest method testParseResultTypeWithDate.

@Test
public void testParseResultTypeWithDate() throws Exception {
    log.info("testParseResultTypeWithDate");
    Method method = ReflectionUtils.findMethod(getClass(), "getDateBean");
    Assert.assertNotNull(method);
    KeyedList<String, ApiResultObject> totalResults = new KeyedList<>();
    ApiResultObject object = ApiResultObject.parseResultType(method, totalResults);
    Assert.assertNotNull(object);
    ApiResultObject current = object.getChild("current");
    Assert.assertEquals("long", current.getTypeName());
}
Also used : KeyedList(com.terran4j.commons.util.value.KeyedList) Method(java.lang.reflect.Method) ApiResultObject(com.terran4j.commons.api2doc.domain.ApiResultObject) Test(org.junit.Test)

Aggregations

KeyedList (com.terran4j.commons.util.value.KeyedList)9 Method (java.lang.reflect.Method)8 ApiResultObject (com.terran4j.commons.api2doc.domain.ApiResultObject)7 Test (org.junit.Test)7 Api2Doc (com.terran4j.commons.api2doc.annotations.Api2Doc)1 ApiComment (com.terran4j.commons.api2doc.annotations.ApiComment)1 ApiError (com.terran4j.commons.api2doc.annotations.ApiError)1 ApiErrors (com.terran4j.commons.api2doc.annotations.ApiErrors)1 MemoryCodeOutput (com.terran4j.commons.api2doc.codewriter.MemoryCodeOutput)1 ApiParamObject (com.terran4j.commons.api2doc.domain.ApiParamObject)1 ValueSource (com.terran4j.commons.util.value.ValueSource)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)1