【问题标题】:Why does the getPaths method of Swagger Parser not return all paths?为什么 Swagger Parser 的 getPaths 方法不返回所有路径?
【发布时间】:2020-03-28 16:58:42
【问题描述】:

我有一个 Swagger 1.2 doc.json 和以下 Java 代码,它使用 Swagger Parser 来从该文档中提取所有路径。问题是解析器没有得到所有的路径(从 50 只显示了 27 条)。

public class Temps {

    public static void main (String[]args ) {
        int totale=0;
        Swagger swagger = new SwaggerParser().read("C:\\Users\\eya\\Desktop\\nodes.json");
         Map<String, Path> paths = swagger.getPaths(); 
         for (Map.Entry<String, Path> p : paths.entrySet()) {
                Path path = p.getValue();
                totale ++;
                Map<HttpMethod, Operation> operations = path.getOperationMap();
                for (java.util.Map.Entry<HttpMethod, Operation> o : operations.entrySet()) {
                    System.out.println("===");
                    System.out.println("PATH:" + p.getKey());
                    System.out.println("Http method:" + o.getKey());
                    System.out.println("Summary:" + o.getValue().getSummary());
                    System.out.println("Parameters number: " + o.getValue().getParameters().size());
                    for (Parameter parameter : o.getValue().getParameters()) {
                        System.out.println(" - " + parameter.getName());
                    }
                    System.out.println("Responses:");
                    for (Map.Entry<String, Response> r : o.getValue().getResponses().entrySet()) {
                        System.out.println(" - " + r.getKey() + ": " + r.getValue().getDescription());
                    }

                }
         }
         System.out.println(totale);
    }
}

有谁知道是什么导致了这个问题?

【问题讨论】:

  • 您是否将 pathsoperations 混淆了?单个路径(例如/something)可以有多个操作 - GET /somethingPOST /somethingDELETE /something 等。所以你可能有 27 个路径但有 50 个操作。
  • 如果 ^^ 不是这种情况,您能否发布您的 nodes.json 文件或指向它的链接 - 以便其他人可以重现该问题?
  • @Helen link 我需要获取所有方法及其路径!
  • @Helen 我在这个文档中有 50 多个路径,但 java 代码只显示 27 :'(

标签: java swagger


【解决方案1】:

您的 API 定义中有重复的路径,例如:

"path": "api/v2/nodes/{id}",
"description": "Get a node",
...
"path": "api/v2/nodes/{id}",
"description": "Get a virtual folder",
"path": "api/v2/nodes/actions",
"description": "Get actions for the selected node IDs",
...
"path": "api/v2/nodes/actions",
"description": "Get actions for the selected node IDs",

Swagger 1.2 规范的重复路径 are not allowed

apis 数组中,每个path 必须只有一个 API 对象。

解析器只是忽略重复项。

【讨论】:

  • 请问您有什么解决方案吗:( ??
  • 使用 JSON 解析器而不是 Swagger 解析器。通用 JSON 解析器不会关心 Swagger 格式的细节/限制。
  • 第 JSON 解析器也不起作用:'(你能帮帮我吗!!
  • ask a new question,发布新代码并指定您使用的 JSON 解析器。
  • 这是link of the question,非常感谢您的回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多