This repository was archived by the owner on Dec 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 295
Add HTTP style to the Request-example #588
Open
returncode
wants to merge
5
commits into
TongchengOpenSource:master
Choose a base branch
from
returncode:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
|
Contributor
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的文档格式下不起作用
Author
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"); | ||
|
Contributor
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这个硬编码是不是不太好,也存其它版本
Author
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) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
能否给建议个合适的单词?