From 0a7bd3d23ec02e11cf2cad27eab7686d0575f2c7 Mon Sep 17 00:00:00 2001 From: tanghc Date: Wed, 29 May 2019 10:03:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E6=A1=A3=E6=98=BE?= =?UTF-8?q?=E7=A4=BABUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sop-website/website-front/pages/doc/doc.js | 8 +++++++- .../websiteserver/manager/DocManagerImpl.java | 17 +++++++++++------ .../websiteserver/manager/SwaggerDocParser.java | 6 +++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/sop-website/website-front/pages/doc/doc.js b/sop-website/website-front/pages/doc/doc.js index 6cb110d0..1b09e526 100644 --- a/sop-website/website-front/pages/doc/doc.js +++ b/sop-website/website-front/pages/doc/doc.js @@ -183,7 +183,13 @@ layui.config({ function buildExample(parameter) { var refs = parameter.refs; if (refs) { - return '{' + buildResult(refs) + '}'; + // {...} + var content = '{' + buildResult(refs) + '}'; + if (parameter.type == 'array') { + // [{...}] + content = '[' + content + ']'; + } + return content; } else { return '\"' + parameter.example + '\"'; } diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java index c1af2bfe..6fefce7d 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java @@ -2,6 +2,7 @@ package com.gitee.sop.websiteserver.manager; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.parser.Feature; import com.gitee.sop.websiteserver.bean.DocInfo; import com.gitee.sop.websiteserver.bean.DocItem; import com.gitee.sop.websiteserver.bean.EurekaApplication; @@ -76,13 +77,17 @@ public class DocManagerImpl implements DocManager { @Override public void load() { try { + // {"STORY-SERVICE":[{"ipAddr":"10.1.30.54","name":"STORY-SERVICE","serverPort":"2222"}],"API-GATEWAY":[{"ipAddr":"10.1.30.54","name":"API-GATEWAY","serverPort":"8081"}]} Map> listMap = this.getAllServiceList(); log.info("服务列表:{}", JSON.toJSONString(listMap.keySet())); - // {"STORY-SERVICE":[{"ipAddr":"10.1.30.54","name":"STORY-SERVICE","serverPort":"2222"}],"API-GATEWAY":[{"ipAddr":"10.1.30.54","name":"API-GATEWAY","serverPort":"8081"}]} - for (Map.Entry> entry : listMap.entrySet()) { - ServiceInfoVO serviceInfoVo = entry.getValue().get(0); - loadDocInfo(serviceInfoVo); - } + listMap.entrySet() + .stream() + // 网关没有文档提供,需要排除 + .filter(entry -> !"API-GATEWAY".equalsIgnoreCase(entry.getKey())) + .forEach(entry -> { + ServiceInfoVO serviceInfoVo = entry.getValue().get(0); + loadDocInfo(serviceInfoVo); + }); } catch (Exception e) { log.error("加载失败", e); } @@ -97,7 +102,7 @@ public class DocManagerImpl implements DocManager { throw new IllegalAccessException("无权访问"); } String docInfoJson = entity.getBody(); - JSONObject docRoot = JSON.parseObject(docInfoJson); + JSONObject docRoot = JSON.parseObject(docInfoJson, Feature.OrderedField); DocParser docParser = this.buildDocParser(docRoot); DocInfo docInfo = docParser.parseJson(docRoot); docDefinitionMap.put(docInfo.getTitle(), docInfo); diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java index 85f1550c..87c45764 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java @@ -154,7 +154,7 @@ public class SwaggerDocParser implements DocParser { DocParameter respParam = fieldInfo.toJavaObject(DocParameter.class); respParam.setName(fieldName); docParameterList.add(respParam); - String originalRef = getRef(fieldInfo); + String originalRef = isArray(fieldInfo) ? getRef(fieldInfo.getJSONObject(fieldName)) : getRef(fieldInfo); if (StringUtils.isNotBlank(originalRef)) { List refs = buildDocParameters(originalRef, docRoot); respParam.setRefs(refs); @@ -163,6 +163,10 @@ public class SwaggerDocParser implements DocParser { 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"))