首页 文章

在swagger.json中检索“$ ref”字段

提问于
浏览
1

我试图使用swagger解析器来解析和检索“swagger.json”中的信息(io.swagger.parser.SwaggerParser;)

下面是“swagger.json”的摘录 . 我正在尝试检索“$ ref”:“#/ definitions / abc” .

"responses" : {
      "200" : {
        "description" : "abc",
        "schema" : {
          "$ref" : "#/definitions/abc"
        }
      },

这是解析它的代码 .

SwaggerParser sparse = new SwaggerParser();
    Swagger swagger = sparse.read("swagger.json");

//下一行就是我遇到的问题 . . . swagger.getPath( “/ endpointurl”)getGet()getResponses()得到( “200”)的getSchema();

此时,上面一行中的“.getSchema()”只能调用“getType()” . 它没有“get $ ref()” . 这是因为“.getSchema()”返回“Property”(io.swagger.models.properties.Property) . 它没有“get $ ref()” .

get Ref()在“RefProperty”中可用(io.swagger.models.properties.RefProperty)

但“ . getSchema()”不会返回“RefProperty” . 将“.getSchema()”的结果类型转换为“RefProperty”也不起作用 . 它最终出现在这个错误中 . java.lang.ClassCastException:io.swagger.models.properties.ArrayProperty无法强制转换为io.swagger.models.properties.RefProperty

有没有人尝试解析“swagger.json”并能够在“响应”块中的“模式”下检索“$ ref”:行?

知道我怎么能这样做?

1 回答

  • 0

    我想办法做到这一点 . 也许不是最好的方法,但它检索“#ref”中的信息 .

    Object obj = xxxxx.getSchema(); // xxxxx is whatever your code that gives you ".getSchema()". Mine is in a map and I don't want to distract people.
    
            ArrayProperty arrProp = new ArrayProperty();
    
            arrProp = (ArrayProperty)obj; // .getSchema() returns an ArrayProperty
    
            RefProperty refProperty = (RefProperty) arrProp.getItems(); // .getItems() will return the RefProperty type you need to call ".get$ref()".
            String refStr = refProperty.get$ref(); // Voila, there's your content in "#ref".
            String simpleRefStr = refProperty.getSimpleRef();
    

    我不得不做几种类型的铸件 . 如果您有更优雅的方式,请在此处发布 .

相关问题