You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/06/25 01:55:08 UTC

[incubator-shenyu] branch master updated: [Task] admin: Fix API document failed to build because of NPE. (#3604)

This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new a5ee50fb7 [Task] admin: Fix API document failed to build because of NPE. (#3604)
a5ee50fb7 is described below

commit a5ee50fb75e989734c8c649defdcf342170b4403
Author: lianjunwei <13...@users.noreply.github.com>
AuthorDate: Sat Jun 25 09:55:00 2022 +0800

    [Task] admin: Fix API document failed to build because of NPE. (#3604)
    
    * apidoc sql
    
    * refact
    
    * commit
    
    * [Task] Shenyu-admin: Fix API document failed to build because of NPE.
    
    * [Task] Shenyu-admin: Fix API document failed to build because of NPE.
    
    * delete useless code.
    
    * delete useless code.
    
    Co-authored-by: lianjunwei <li...@didiglobal.com>
---
 .../shenyu/admin/controller/ApiDocController.java  |  4 +-
 .../service/manager/impl/SwaggerDocParser.java     | 68 +++++++++++-----------
 2 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ApiDocController.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ApiDocController.java
index d1765a8c7..fe762e853 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ApiDocController.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ApiDocController.java
@@ -37,7 +37,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 /**
- * ApiDoc Controller.
+ * Api Documet Controller.
  */
 @RestController
 @RequestMapping("/apidoc")
@@ -64,8 +64,6 @@ public class ApiDocController {
         docVO.setGatewayUrl(apiDocProperties.getGatewayUrl());
         docVO.setMenuProjects(menuProjects);
         docVO.setEnvProps(apiDocProperties.getEnvProps());
-        docVO.setCookie("Fill in the real cookie value.(signature authentication and login free API ignore this item)");
-        docVO.setAppKey("");
         return ShenyuAdminResult.success(docVO);
     }
 
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/SwaggerDocParser.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/SwaggerDocParser.java
index e53df5c9d..238a53cbc 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/SwaggerDocParser.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/SwaggerDocParser.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.admin.service.manager.impl;
 
 import com.google.common.collect.Sets;
+import com.google.gson.JsonElement;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -62,7 +63,7 @@ public class SwaggerDocParser implements DocParser {
         final List<DocItem> docItems = new ArrayList<>();
 
         JsonObject paths = docRoot.getAsJsonObject("paths");
-        if (paths == null) {
+        if (Objects.isNull(paths)) {
             paths = new JsonObject();
         }
         Set<String> pathNameSet = paths.keySet();
@@ -123,17 +124,8 @@ public class SwaggerDocParser implements DocParser {
 
     protected Collection<String> getHttpMethods(final JsonObject pathInfo) {
         // key: get,post,head...
-        List<String> retList;
         Set<String> httpMethodList = pathInfo.keySet();
-        if (httpMethodList.size() <= 2) {
-            retList = new ArrayList<>(httpMethodList);
-        } else {
-//            Set<String> ignoreHttpMethods = DocParserContext.ignoreHttpMethods;
-//            retList = httpMethodList.stream()
-//                    .filter(method -> !ignoreHttpMethods.contains(method.toLowerCase()))
-//                    .collect(Collectors.toList());
-            retList = new ArrayList<>(httpMethodList);
-        }
+        List<String> retList = new ArrayList<>(httpMethodList);
         Collections.sort(retList);
         return retList;
     }
@@ -146,16 +138,27 @@ public class SwaggerDocParser implements DocParser {
         DocItem docItem = new DocItem();
         docItem.setId(UUID.randomUUID().toString());
         docItem.setName(apiName);
-        docItem.setSummary(docInfo.get("summary").getAsString());
-        docItem.setDescription(docInfo.get("description").getAsString());
-        docItem.setProduces(GsonUtils.getGson().fromJson(docInfo.getAsJsonArray("produces"), new TypeToken<List<String>>() { }.getType()));
-        docItem.setMultiple(docInfo.get("multiple").getAsString() != null);
-        String apiResponseStr = docInfo.get("apiResponse").getAsString();
-        if (apiResponseStr != null) {
-            docItem.setBizCodeList(GsonUtils.getInstance().fromList(apiResponseStr, CustomCode.class));
+        if (Objects.nonNull(docInfo.get("summary"))) {
+            docItem.setSummary(docInfo.get("summary").getAsString());
+        }
+        if (Objects.nonNull(docInfo.get("description"))) {
+            docItem.setDescription(docInfo.get("description").getAsString());
+        }
+        docItem.setProduces(GsonUtils.getGson().fromJson(docInfo.getAsJsonArray("produces"), new TypeToken<List<String>>() {
+        }.getType()));
+
+        if (Objects.nonNull(docInfo.get("multiple"))) {
+            docItem.setMultiple(true);
+        }
+        if (Objects.nonNull(docInfo.get("apiResponse"))) {
+            docItem.setBizCodeList(GsonUtils.getInstance().fromList(docInfo.get("apiResponse").getAsString(), CustomCode.class));
+        }
+        if (Objects.nonNull(docInfo.get("module_order"))) {
+            docItem.setModuleOrder(NumberUtils.toInt(docInfo.get("module_order").getAsString(), 0));
+        }
+        if (Objects.nonNull(docInfo.get("api_order"))) {
+            docItem.setApiOrder(NumberUtils.toInt(docInfo.get("api_order").getAsString(), 0));
         }
-        docItem.setModuleOrder(NumberUtils.toInt(docInfo.get("module_order").getAsString(), 0));
-        docItem.setApiOrder(NumberUtils.toInt(docInfo.get("api_order").getAsString(), 0));
         String moduleName = this.buildModuleName(docInfo, docRoot, basePath);
         docItem.setModule(moduleName);
         List<DocParameter> docParameterList = this.buildRequestParameterList(docInfo, docRoot);
@@ -225,7 +228,7 @@ public class SwaggerDocParser implements DocParser {
     protected List<DocParameter> buildResponseParameterList(final JsonObject docInfo, final JsonObject docRoot) {
         RefInfo refInfo = getResponseRefInfo(docInfo);
         List<DocParameter> respParameterList = Collections.emptyList();
-        if (refInfo != null) {
+        if (Objects.nonNull(refInfo)) {
             String responseRef = refInfo.ref;
             respParameterList = this.buildDocParameters(responseRef, docRoot, true);
             // If an array is returned.
@@ -256,15 +259,13 @@ public class SwaggerDocParser implements DocParser {
             DocParameter docParameter = GsonUtils.getInstance().fromJson(fieldInfo, DocParameter.class);
             docParameter.setName(fieldName);
             docParameter.setRequired(
-                    !(requiredProperties == null || requiredProperties.isEmpty()) && requiredProperties.contains(fieldInfo));
+                !(Objects.isNull(requiredProperties) || requiredProperties.isEmpty()) && requiredProperties.contains(fieldInfo));
             if (Objects.nonNull(extProperties)) {
                 JsonObject prop = extProperties.getAsJsonObject(fieldName);
                 if (Objects.nonNull(prop)) {
-                    String maxLength = prop.get("maxLength").getAsString();
-                    docParameter.setMaxLength(Objects.isNull(maxLength) ? "-" : maxLength);
-                    String required = prop.get("required").getAsString();
-                    if (Objects.nonNull(required)) {
-                        docParameter.setRequired(Boolean.parseBoolean(required));
+                    docParameter.setMaxLength(Objects.isNull(prop.get("maxLength")) ? "-" : prop.get("maxLength").getAsString());
+                    if (Objects.nonNull(prop.get("required"))) {
+                        docParameter.setRequired(Boolean.parseBoolean(prop.get("required").getAsString()));
                     }
                 }
             }
@@ -282,7 +283,7 @@ public class SwaggerDocParser implements DocParser {
 
     /**
      * Simple object return, pure array return.
-
+     *
      * @param docInfo docInfo
      * @return RefInfo
      */
@@ -295,17 +296,18 @@ public class SwaggerDocParser implements DocParser {
     }
 
     private RefInfo getRefInfo(final JsonObject jsonObject) {
-        String ref;
-        boolean isArray = "array".equals(jsonObject.get("type").getAsString());
+        JsonElement refElement;
+        boolean isArray = Objects.isNull(jsonObject.get("type")) ? false : "array".equals(jsonObject.get("type").getAsString());
         if (isArray) {
-            ref = jsonObject.getAsJsonObject("items").get("$ref").getAsString();
+            refElement = jsonObject.getAsJsonObject("items").get("$ref");
         } else {
             // #/definitions/xxx
-            ref = jsonObject.get("$ref").getAsString();
+            refElement = jsonObject.get("$ref");
         }
-        if (Objects.isNull(ref)) {
+        if (Objects.isNull(refElement)) {
             return null;
         }
+        String ref = refElement.getAsString();
         int index = ref.lastIndexOf("/");
         if (index > -1) {
             ref = ref.substring(index + 1);