使用openapi4j解析位于jar文件内的带有引用的Swagger。

huangapple go评论93阅读模式
英文:

Parse Swagger with refs inside jar with openapi4j

问题

我正在尝试使用openapi4j解析以下Swagger文件

{
  "openapi" : "3.0.0",
  "info" : {
    "title" : "My Service",
    "version" : "1.0.0"
  },
  "paths" : {
    "/endpoint" : { "$ref" :  "swagger2.json#/paths/get_endpoint" }
  }
}

你可以看到它在同一文件夹中对另一个文件使用了简单的引用

我使用以下代码解析Swagger文件

URL url = Thread.currentThread().getContextClassLoader().getResource(filePath);
openAPI = new OpenApi3Parser().parse(url, false);

在我的IDE中这在本地工作得很好引用被加载我可以毫无问题地对其进行验证然而当我将项目打包成JAR文件时它能够成功加载初始的Swagger文件但无法加载任何引用我得到以下错误

StackTrace: org.openapi4j.core.exception.ResolutionException:'swagger2.json'中加载文档失败
    at org.openapi4j.core.model.reference.AbstractReferenceResolver.registerDocument(AbstractReferenceResolver.java:118)
    at org.openapi4j.core.model.reference.AbstractReferenceResolver.findReferences(AbstractReferenceResolver.java:92)
    at org.openapi4j.core.model.reference.AbstractReferenceResolver.resolve(AbstractReferenceResolver.java:53)
    at org.openapi4j.core.model.v3.OAI3Context.resolveReferences(OAI3Context.java:103)
    at org.openapi4j.core.model.v3.OAI3Context.<init>(OAI3Context.java:73)
    at org.openapi4j.core.model.v3.OAI3Context.<init>(OAI3Context.java:47)
    at org.openapi4j.parser.OpenApi3Parser.parse(OpenApi3Parser.java:34)
    at org.openapi4j.parser.OpenApi3Parser.parse(OpenApi3Parser.java:18)
    at org.openapi4j.parser.OpenApiParser.parse(OpenApiParser.java:53)

我不确定是否有可能解决这个问题我猜我可能需要在运行时将项目资源复制到文件系统的某个位置以便更容易地访问它们如果可能的话我想避免这条路线
英文:

I'm trying to parse the following swagger file with openapi4j:

{
&quot;openapi&quot; : &quot;3.0.0&quot;,
&quot;info&quot; : {
&quot;title&quot; : &quot;My Service&quot;,
&quot;version&quot; : &quot;1.0.0&quot;
},
&quot;paths&quot; : {
&quot;/endpoint&quot; : { &quot;$ref&quot; :  &quot;swagger2.json#/paths/get_endpoint&quot; },
}
}

You can see it has a simple ref to another file within the same folder.

I parse the Swagger file with the following:

URL url = Thread.currentThread().getContextClassLoader().getResource(filePath);
openAPI = new OpenApi3Parser().parse(url, false);

Locally in my IDE, this works great. The ref is loaded and I am able to validate requests against it with no issues. However, when I jar up the project, it is able to load the initial swagger file fine, but none of the refs. I get the following error:

StackTrace: org.openapi4j.core.exception.ResolutionException: Failed to load document from &#39;swagger2.json&#39;
at org.openapi4j.core.model.reference.AbstractReferenceResolver.registerDocument(AbstractReferenceResolver.java:118)
at org.openapi4j.core.model.reference.AbstractReferenceResolver.findReferences(AbstractReferenceResolver.java:92)
at org.openapi4j.core.model.reference.AbstractReferenceResolver.resolve(AbstractReferenceResolver.java:53)
at org.openapi4j.core.model.v3.OAI3Context.resolveReferences(OAI3Context.java:103)
at org.openapi4j.core.model.v3.OAI3Context.&lt;init&gt;(OAI3Context.java:73)
at org.openapi4j.core.model.v3.OAI3Context.&lt;init&gt;(OAI3Context.java:47)
at org.openapi4j.parser.OpenApi3Parser.parse(OpenApi3Parser.java:34)
at org.openapi4j.parser.OpenApi3Parser.parse(OpenApi3Parser.java:18)
at org.openapi4j.parser.OpenApiParser.parse(OpenApiParser.java:53)

I'm not sure if this is possible. I assume I may have to copy my project resources out at runtime to the filesystem somewhere to be accessed more easily. I would like to avoid that route if possible.

答案1

得分: 1

原来这是在 openapi4j 中的一个错误,它在解析引用时使用了 URI 而不是 URL。这在版本 0.9 中已经修复。

英文:

Turns out this was a bug in openapi4j, which was resolving refs with URI instead of URL. This has been fixed in 0.9

huangapple
  • 本文由 发表于 2020年4月9日 03:57:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61109016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定