英文:
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:
{
"openapi" : "3.0.0",
"info" : {
"title" : "My Service",
"version" : "1.0.0"
},
"paths" : {
"/endpoint" : { "$ref" : "swagger2.json#/paths/get_endpoint" },
}
}
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 '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 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论