相同类的不同行为取决于依赖性?

时间:2016-02-19 18:39:09

标签: java dependencies sbt swagger

我很困惑为什么我会观察方法调用的不同输出,具体取决于是否包含其他库。

在使用和不使用swagger-compat-spec-parser库解析Swagger v1.2 Doc时,我观察到了不同的结果。

请注意,在以下输出中,我运行了Scala编程语言的REPL。

将Swagger文档解析为null而没有swagger-compat-spec-parser

$cat build.sbt
 libraryDependencies += "io.swagger" % "swagger-parser" % "1.0.17"
$sbt console

scala> import io.swagger.parser.SwaggerParser
import io.swagger.parser.SwaggerParser

scala> "/Users/Kevin/Workspace/project/src/test/resources/api-docs-swagger-v1.2"
res0: String = /Users/Kevin/Workspace/project/src/test/resources/api-docs-swagger-v1.2

scala> new SwaggerParser().read(res0)
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
reading from /Users/Kevin/Workspace/project/src/test/resources/api-docs-swagger-v1.2
reading from /Users/Kevin/Workspace/project/src/test/resources/api-docs-swagger-v1.2
res1: io.swagger.models.Swagger = null

使用swagger-compat-spec-parser库将Swagger文档解析为非null结果

$cat build.sbt
 libraryDependencies += "io.swagger" % "swagger-parser" % "1.0.17"
 libraryDependencies += "io.swagger" % "swagger-compat-spec-parser" % "1.0.17"
$sbt console

scala> import io.swagger.parser.SwaggerParser
import io.swagger.parser.SwaggerParser

scala> "/Users/Kevin/Workspace/project/src/test/resources/api-docs-swagger-v1.2"
res0: String = /Users/Kevin/Workspace/project/src/test/resources/api-docs-swagger-v1.2

scala> new SwaggerParser().read(res0)
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
reading from /Users/Kevin/Workspace/project/src/test/resources/api-docs-swagger-v1.2
reading from /Users/Kevin/Workspace/project/src/test/resources/api-docs-swagger-v1.2
res1: io.swagger.models.Swagger = io.swagger.models.Swagger@5876ed9a

为什么相同(我推测)类read方法会因是否存在其他依赖项而有所不同?

2 个答案:

答案 0 :(得分:1)

这与Swagger有关。

行为是可以的,甚至可能没有记录(至少在源代码中没有)。看看https://github.com/swagger-api/swagger-parser/blob/master/modules/swagger-parser/src/main/java/io/swagger/parser/SwaggerParser.java#L55

更确切地说,在第64行,Swagger读取位置,但仅限于URL。

因此,您的示例将一直运行到第72行,其中使用了扩展名。扩展是通过第60行的getExtensions()提供的(第169行的定义)。

正如您所看到的,它们是在运行时通过ServiceLoader加载的,即没有提供解析时:)

希望清除行为。但你可以测试你的第二个例子,将Swagger指向不存在的文件以查看它的爆炸位置,你会得到:

scala> "/Users/IMNotKevin/Thus/Api/Does/Not/Exist"
res0: String = /Users/IMNotKevin/Thus/Api/Does/Not/Exist

scala> new SwaggerParser().read(res0)
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
reading from /Users/IMNotKevin/Thus/Api/Does/Not/Exist
reading from /Users/IMNotKevin/Thus/Api/Does/Not/Exist
java.io.FileNotFoundException: /Users/IMNotKevin/Thus/Api/Does/Not/Exist (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:708)
    at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:1861)
   at io.swagger.parser.SwaggerCompatConverter.readResourceListing(SwaggerCompatConverter.java:139)
   at io.swagger.parser.SwaggerCompatConverter.read(SwaggerCompatConverter.java:74)
   at io.swagger.parser.SwaggerParser.read(SwaggerParser.java:73)
   at io.swagger.parser.SwaggerParser.read(SwaggerParser.java:52)  

享受:)

答案 1 :(得分:1)

@Teliakto是对的。根据{{​​3}},如果要加载1.2规范,则必须加载swagger-compat-spec-parser。这是通过SPI完成的。