-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HTTP style to the Request-example #588
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -426,9 +426,10 @@ default List<ApiMethodDoc> buildEntryPointMethod( | |
} | ||
|
||
// build request json | ||
ApiRequestExample requestExample = buildReqJson(docJavaMethod, apiMethodDoc, requestMapping.getMethodType(), | ||
projectBuilder, frameworkAnnotations); | ||
String requestJson = requestExample.getExampleBody(); | ||
ApiRequestExample requestExample = buildReqJson(docJavaMethod, apiMethodDoc, requestMapping.getMethodType(), projectBuilder, frameworkAnnotations); | ||
String requestJson = projectBuilder.getApiConfig().isHttp() | ||
? buildHttpJson(docJavaMethod, apiMethodDoc, requestMapping.getMethodType(), projectBuilder, frameworkAnnotations) | ||
: requestExample.getExampleBody(); | ||
// set request example detail | ||
apiMethodDoc.setRequestExample(requestExample); | ||
apiMethodDoc.setRequestUsage(requestJson == null ? requestExample.getUrl() : requestJson); | ||
|
@@ -742,16 +743,62 @@ else if (javaClass.isEnum()) { | |
return ApiParamTreeUtil.buildMethodReqParam(paramList, queryReqParamMap, pathReqParamMap, requestBodyCounter); | ||
} | ||
|
||
default ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc apiMethodDoc, String methodType, | ||
ProjectDocConfigBuilder configBuilder, FrameworkAnnotations frameworkAnnotations) { | ||
default String buildHttpJson(DocJavaMethod docJavaMethod, ApiMethodDoc apiMethodDoc, String methodType, ProjectDocConfigBuilder projectBuilder, FrameworkAnnotations frameworkAnnotations) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 下面这新增的内容是不是在html的文档格式下不起作用 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. html的文档格式下指什么功能及作用 |
||
Boolean isGet = methodType.equals("GET"); | ||
Boolean isFile = apiMethodDoc.getContentType().equals(FILE_CONTENT_TYPE); | ||
String boundary = UUIDUtil.getUuid32(); | ||
List<ApiParam> queryParams = apiMethodDoc.getQueryParams(); | ||
List<ApiParam> requestParams = apiMethodDoc.getRequestParams(); | ||
StringBuilder title = new StringBuilder().append("### ").append(apiMethodDoc.getDesc()).append("\n"); | ||
StringBuilder request = new StringBuilder().append(methodType).append(" ").append(apiMethodDoc.getUrl()).append(isGet ? " " : " HTTP/1.1").append("\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. http/1.1这个硬编码是不是不太好,也存其它版本 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是参考Rest-client标准,有无对代码无影响。 |
||
if (isGet && queryParams.size() > 0) { | ||
StringJoiner params = new StringJoiner("\n&"); | ||
queryParams.forEach(p -> params.add(p.getField() + "=" + p.getValue())); | ||
request.append("?").append(params.toString()).append("\n"); | ||
} | ||
StringBuilder header = new StringBuilder().append("Content-Type:").append(apiMethodDoc.getContentType()).append(isFile ? "; boundary=" + boundary + "\n" : "\n"); | ||
apiMethodDoc.getRequestHeaders().forEach(h -> header.append(h.getName()).append(":").append(h.getValue()).append("\n")); | ||
if (isGet) { | ||
return new StringBuilder().append(title).append(request).append(header).toString(); | ||
} | ||
StringBuilder body = new StringBuilder().append("\n"); | ||
if (apiMethodDoc.getContentType().equals(DocGlobalConstants.URL_CONTENT_TYPE) && queryParams.size() > 0) { | ||
StringJoiner params = new StringJoiner("\n&"); | ||
queryParams.forEach(p -> params.add(p.getField() + "=" + p.getValue())); | ||
body.append(params).append("\n"); | ||
} | ||
if (apiMethodDoc.getContentType().equals(JSON_CONTENT_TYPE) && requestParams.size() > 0) { | ||
Map<String, Object> params = new HashMap<>(requestParams.size()); | ||
requestParams.forEach(p -> params.put(p.getField(), p.getValue())); | ||
body.append(JsonUtil.toPrettyJson(params)).append("\n"); | ||
} | ||
if (apiMethodDoc.getContentType().equals(FILE_CONTENT_TYPE) && queryParams.size() > 0) { | ||
queryParams.forEach(p -> { | ||
body.append("--").append(boundary).append("\n"); | ||
if ("file".equals(p.getType())) { | ||
body.append("Content-Disposition: form-data; name=\"" + p.getField() + "\"; filename=\"xxxxx.pdf\"\n"); | ||
body.append("Content-Type: application/pdf\n"); | ||
body.append("\n"); | ||
body.append("< xxxxx.pdf\n"); | ||
} | ||
if ("string".equals(p.getType())) { | ||
body.append("Content-Disposition: form-data; name=\"" + p.getField() + "\"\n"); | ||
body.append("\n"); | ||
body.append(p.getValue() + "\n"); | ||
} | ||
}); | ||
body.append("--").append(boundary).append("--\n"); | ||
} | ||
return new StringBuilder().append(title).append(request).append(header).append(body).toString(); | ||
} | ||
|
||
default ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc apiMethodDoc, String methodType, ProjectDocConfigBuilder configBuilder, FrameworkAnnotations frameworkAnnotations) { | ||
JavaMethod method = javaMethod.getJavaMethod(); | ||
Map<String, String> pathParamsMap = new LinkedHashMap<>(); | ||
Map<String, String> queryParamsMap = new LinkedHashMap<>(); | ||
|
||
apiMethodDoc.getPathParams().stream().filter(Objects::nonNull).filter(p -> StringUtil.isNotEmpty(p.getValue()) || p.isConfigParam()) | ||
.forEach(param -> pathParamsMap.put(param.getSourceField(), param.getValue())); | ||
apiMethodDoc.getQueryParams().stream().filter(Objects::nonNull).filter(p -> StringUtil.isNotEmpty(p.getValue()) || p.isConfigParam()) | ||
.forEach(param -> queryParamsMap.put(param.getSourceField(), param.getValue())); | ||
apiMethodDoc.getPathParams().stream().filter(Objects::nonNull).filter(p -> StringUtil.isNotEmpty(p.getValue()) || p.isConfigParam()).forEach(param -> pathParamsMap.put(param.getSourceField(), param.getValue())); | ||
apiMethodDoc.getQueryParams().stream().filter(Objects::nonNull).filter(p -> StringUtil.isNotEmpty(p.getValue()) || p.isConfigParam()).forEach(param -> queryParamsMap.put(param.getSourceField(), param.getValue())); | ||
List<JavaAnnotation> methodAnnotations = method.getAnnotations(); | ||
Map<String, MappingAnnotation> mappingAnnotationMap = frameworkAnnotations.getMappingAnnotations(); | ||
for (JavaAnnotation annotation : methodAnnotations) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个定义是不是语义不够精细。http代表配置http协议呢还是http样例呢,可以换个更详细的单词
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
能否给建议个合适的单词?