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 all 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
13 changes: 13 additions & 0 deletions src/main/java/com/ly/doc/model/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ public static ApiConfig getInstance() {
* adoc flag
*/
private boolean adoc;

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

public boolean isRest() {
return rest;
}

public void setRest(boolean rest) {
this.rest = rest;
}

public List<ApiDataDictionary> getDataDictionaries() {
return dataDictionaries;
}
Expand Down
65 changes: 56 additions & 9 deletions src/main/java/com/ly/doc/template/IRestDocTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,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().isRest()
? buildRestJson(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 @@ -766,16 +767,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 buildRestJson(DocJavaMethod docJavaMethod, ApiMethodDoc apiMethodDoc, String methodType, ProjectDocConfigBuilder projectBuilder, FrameworkAnnotations frameworkAnnotations) {
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");
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
1 change: 1 addition & 0 deletions src/test/java/com/ly/doc/ApiDocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void testBuilderControllersApi() {

config.setDebugEnvName("测试环境");
config.setInlineEnum(true);
config.setRest(true);
config.setStyle("randomLight");
config.setCreateDebugPage(true);
// config.setAuthor("test");
Expand Down