mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
修复返回数组文档不显示问题
This commit is contained in:
@@ -17,7 +17,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 支付宝服务端,假设签名验证通过后,到达这里进行具体的业务处理。
|
||||
@@ -182,6 +184,23 @@ public class AlipayController {
|
||||
return storyVO;
|
||||
}
|
||||
|
||||
@ApiOperation(value = "返回数组结果", notes = "返回数组结果")
|
||||
@ApiMapping(value = "alipay.story.find2")
|
||||
public List<StoryVO> getStory3(StoryParam story) {
|
||||
int index = 0;
|
||||
StoryVO storyVO = new StoryVO();
|
||||
storyVO.id = 1L;
|
||||
storyVO.name = "白雪公主, index:" + index++;
|
||||
storyVO.gmt_create = new Date();
|
||||
|
||||
StoryVO storyVO2 = new StoryVO();
|
||||
storyVO2.id = 1L;
|
||||
storyVO2.name = "白雪公主, index:" + index++;
|
||||
storyVO2.gmt_create = new Date();
|
||||
|
||||
return Arrays.asList(storyVO, storyVO2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示文档表格树
|
||||
* @param story
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.gitee.sop.storyweb.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -25,8 +26,11 @@ public class FileUploadVO {
|
||||
public FileMeta() {
|
||||
}
|
||||
|
||||
@ApiModelProperty(value = "文件名称", example = "1.txt")
|
||||
private String filename;
|
||||
@ApiModelProperty(value = "文件大小", example = "109")
|
||||
private long size;
|
||||
@ApiModelProperty(value = "文件内容", example = "啊啊啊")
|
||||
private String content;
|
||||
}
|
||||
}
|
||||
|
@@ -119,6 +119,7 @@ public class SandboxController {
|
||||
result.apiResult = responseData;
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("请求失败", e);
|
||||
throw new RuntimeException("请求失败");
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,6 @@ import com.gitee.sop.websiteserver.bean.DocModule;
|
||||
import com.gitee.sop.websiteserver.bean.DocParameter;
|
||||
import com.gitee.sop.websiteserver.bean.DocParserContext;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -138,7 +137,7 @@ public class SwaggerDocParser implements DocParser {
|
||||
})
|
||||
.collect(Collectors.groupingBy(DocParameter::getModule));
|
||||
|
||||
collect.entrySet().stream()
|
||||
collect.entrySet()
|
||||
.forEach(entry -> {
|
||||
DocParameter moduleDoc = new DocParameter();
|
||||
moduleDoc.setName(entry.getKey());
|
||||
@@ -147,18 +146,25 @@ public class SwaggerDocParser implements DocParser {
|
||||
docParameterList.add(moduleDoc);
|
||||
});
|
||||
|
||||
List<DocParameter> ret = docParameterList.stream()
|
||||
return docParameterList.stream()
|
||||
.filter(docParameter -> !docParameter.getName().contains("."))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected List<DocParameter> buildResponseParameterList(JSONObject docInfo, JSONObject docRoot) {
|
||||
String responseRef = getResponseRef(docInfo);
|
||||
RefInfo refInfo = getResponseRefInfo(docInfo);
|
||||
List<DocParameter> respParameterList = Collections.emptyList();
|
||||
if (StringUtils.isNotBlank(responseRef)) {
|
||||
if (refInfo != null) {
|
||||
String responseRef = refInfo.ref;
|
||||
respParameterList = this.buildDocParameters(responseRef, docRoot);
|
||||
// 如果返回数组
|
||||
if (refInfo.isArray) {
|
||||
DocParameter docParameter = new DocParameter();
|
||||
docParameter.setName("items");
|
||||
docParameter.setType("array");
|
||||
docParameter.setRefs(respParameterList);
|
||||
respParameterList = Collections.singletonList(docParameter);
|
||||
}
|
||||
}
|
||||
return respParameterList;
|
||||
}
|
||||
@@ -170,52 +176,104 @@ public class SwaggerDocParser implements DocParser {
|
||||
List<DocParameter> docParameterList = new ArrayList<>();
|
||||
for (String fieldName : fieldNames) {
|
||||
/*
|
||||
{
|
||||
"description": "分类故事",
|
||||
"$ref": "#/definitions/StoryVO",
|
||||
"originalRef": "StoryVO"
|
||||
{
|
||||
"description": "分类故事",
|
||||
"$ref": "#/definitions/StoryVO"
|
||||
}
|
||||
*/
|
||||
JSONObject fieldInfo = properties.getJSONObject(fieldName);
|
||||
DocParameter respParam = fieldInfo.toJavaObject(DocParameter.class);
|
||||
respParam.setName(fieldName);
|
||||
docParameterList.add(respParam);
|
||||
String originalRef = isArray(fieldInfo) ? getRef(fieldInfo.getJSONObject(fieldName)) : getRef(fieldInfo);
|
||||
if (StringUtils.isNotBlank(originalRef)) {
|
||||
List<DocParameter> refs = buildDocParameters(originalRef, docRoot);
|
||||
respParam.setRefs(refs);
|
||||
DocParameter docParameter = fieldInfo.toJavaObject(DocParameter.class);
|
||||
docParameter.setName(fieldName);
|
||||
docParameterList.add(docParameter);
|
||||
RefInfo refInfo = this.getRefInfo(fieldInfo);
|
||||
if (refInfo != null) {
|
||||
List<DocParameter> refs = buildDocParameters(refInfo.ref, docRoot);
|
||||
docParameter.setRefs(refs);
|
||||
}
|
||||
}
|
||||
return docParameterList;
|
||||
}
|
||||
|
||||
protected boolean isArray(JSONObject fieldInfo) {
|
||||
return "array".equalsIgnoreCase(fieldInfo.getString("type"));
|
||||
}
|
||||
|
||||
private String getRef(JSONObject fieldInfo) {
|
||||
return Optional.ofNullable(fieldInfo)
|
||||
.map(jsonObject -> jsonObject.getString("originalRef"))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
protected String getResponseRef(JSONObject docInfo) {
|
||||
/**
|
||||
* 简单对象返回:
|
||||
* "responses": {
|
||||
* "200": {
|
||||
* "description": "OK",
|
||||
* "schema": {
|
||||
* "$ref": "#/definitions/FileUploadVO"
|
||||
* }
|
||||
* },
|
||||
* "401": {
|
||||
* "description": "Unauthorized"
|
||||
* },
|
||||
* "403": {
|
||||
* "description": "Forbidden"
|
||||
* },
|
||||
* "404": {
|
||||
* "description": "Not Found"
|
||||
* }
|
||||
* }
|
||||
* 纯数组返回:
|
||||
* "responses": {
|
||||
* "200": {
|
||||
* "description": "OK",
|
||||
* "schema": {
|
||||
* "type": "array",
|
||||
* "items": {
|
||||
* "$ref": "#/definitions/StoryVO"
|
||||
* }
|
||||
* }
|
||||
* },
|
||||
* "401": {
|
||||
* "description": "Unauthorized"
|
||||
* },
|
||||
* "403": {
|
||||
* "description": "Forbidden"
|
||||
* },
|
||||
* "404": {
|
||||
* "description": "Not Found"
|
||||
* }
|
||||
* }
|
||||
* @param docInfo
|
||||
* @return
|
||||
*/
|
||||
protected RefInfo getResponseRefInfo(JSONObject docInfo) {
|
||||
return Optional.ofNullable(docInfo.getJSONObject("responses"))
|
||||
.flatMap(jsonObject -> Optional.ofNullable(jsonObject.getJSONObject("200")))
|
||||
.flatMap(jsonObject -> Optional.ofNullable(jsonObject.getJSONObject("schema")))
|
||||
.flatMap(jsonObject -> {
|
||||
// #/definitions/Category
|
||||
String $ref = jsonObject.getString("$ref");
|
||||
if ($ref == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
int index = $ref.lastIndexOf("/");
|
||||
if (index > -1) {
|
||||
$ref = $ref.substring(index + 1);
|
||||
}
|
||||
return Optional.of($ref);
|
||||
.flatMap(schema -> {
|
||||
RefInfo refInfo = getRefInfo(schema);
|
||||
return Optional.ofNullable(refInfo);
|
||||
})
|
||||
.orElse("");
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private RefInfo getRefInfo(JSONObject jsonObject) {
|
||||
String $ref;
|
||||
boolean isArray = "array".equals(jsonObject.getString("type"));
|
||||
if (isArray) {
|
||||
$ref = jsonObject.getJSONObject("items").getString("$ref");
|
||||
} else {
|
||||
// #/definitions/Category
|
||||
$ref = jsonObject.getString("$ref");
|
||||
}
|
||||
if ($ref == null) {
|
||||
return null;
|
||||
}
|
||||
int index = $ref.lastIndexOf("/");
|
||||
if (index > -1) {
|
||||
$ref = $ref.substring(index + 1);
|
||||
}
|
||||
RefInfo refInfo = new RefInfo();
|
||||
refInfo.isArray = isArray;
|
||||
refInfo.ref = $ref;
|
||||
return refInfo;
|
||||
}
|
||||
|
||||
private static class RefInfo {
|
||||
private boolean isArray;
|
||||
private String ref;
|
||||
}
|
||||
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user