Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.shalousun</groupId>
<modelVersion>4.0.0</modelVersion>
<artifactId>smart-doc</artifactId>
<packaging>jar</packaging>
<version>2.7.5</version>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/power/doc/model/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public class ApiConfig {
* adoc flag
*/
private boolean adoc;

/**
* http flag
*/
private boolean http;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个定义是不是语义不够精细。http代表配置http协议呢还是http样例呢,可以换个更详细的单词

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

能否给建议个合适的单词?

/**
* default /src/main/java
*/
Expand Down Expand Up @@ -597,6 +602,14 @@ public void setAdoc(boolean adoc) {
this.adoc = adoc;
}

public boolean isHttp() {
return http;
}

public void setHttp(boolean http) {
this.http = http;
}

public List<ApiDataDictionary> getDataDictionaries() {
return dataDictionaries;
}
Expand Down
65 changes: 56 additions & 9 deletions src/main/java/com/power/doc/template/IRestDocTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下面这新增的内容是不是在html的文档格式下不起作用

Copy link
Author

Choose a reason for hiding this comment

The 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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http/1.1这个硬编码是不是不太好,也存其它版本

Copy link
Author

Choose a reason for hiding this comment

The 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) {
Expand Down