【问题标题】:Assertj-Swagger PropertyType mismatchAssertj-Swagger PropertyType 不匹配
【发布时间】:2020-03-19 15:00:15
【问题描述】:

在运行我的 swagger 定义测试时,我收到以下错误:

Stack trace:
 org.assertj.core.error.AssertJMultipleFailuresError: 
Multiple Failures (1 failure)
-- failure 1 --
[Checking response schema of response '200' of 'GET' operation of path '/api/controller/method/{param}'] 
Expecting:
 <io.swagger.models.properties.ByteArrayProperty@bf0f8c09>
to be exactly an instance of:
 <io.swagger.models.properties.StringProperty>
but was an instance of:
 <io.swagger.models.properties.ByteArrayProperty>
at ConsumerDrivenValidator.validateProperty(ConsumerDrivenValidator.java:233)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at io.github.robwin.swagger.test.ConsumerDrivenValidator.validateSwagger(ConsumerDrivenValidator.java:90)
    at io.github.robwin.swagger.test.SwaggerAssert.satisfiesContract(SwaggerAssert.java:111)
    at io.github.robwin.swagger.test.SwaggerAssert.satisfiesContract(SwaggerAssert.java:123)
    at com.myapp.api.documentation.MyTestClass.MyTestMethod(MyTestClass.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

这是我的 yaml 文件的内容:

'/api/controller/method/{param}':
    get:
      tags:
        - my-controller
      summary: photo
      operationId: photoUsingGET
      produces:
        - image/jpeg
      parameters:
        - name: param
          in: path
          description: param
          required: true
          type: integer
          format: int64
      responses:
        '200':
          description: OK
          schema:
            type: string
            format: byte
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
        '404':
          description: Not Found
      security:
        - apiKey: []
      deprecated: false

我的方法接受一个参数 int 并以字节数组public @ResponseBody byte[] myMethod(@PathVariable Long param); 的形式返回一个图像。阅读here 后,我的文件内容正确。我真的不知道我的测试出了什么问题。

**Please ignore text below. Only added so that SO can allow me to post my question.**

Lorem Ipsum 只是印刷和排版行业的虚拟文本。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商采用了一种类型的厨房并将其加扰以制作类型样本书。它不仅经历了五个世纪,而且经历了电子排版的飞跃,基本保持不变。它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 表的发布而流行起来,最近还随着 Aldus PageMaker 等桌面出版软件(包括 Lorem Ipsum 的版本)而普及。

【问题讨论】:

    标签: spring-boot swagger-2.0 spring-boot-test


    【解决方案1】:

    在 OpenAPI 2.0 中,作为文件的响应必须具有 type: file

          responses:
            '200':
              description: OK
              schema:
                type: file
    

    更多信息:https://swagger.io/docs/specification/2-0/describing-responses/#response-that-returns-a-file

    【讨论】:

    • 但这与字节数组返回类型兼容吗?
    • type: file 通常作为二进制流或字节数组处理。你能试试看这是否能解决你的问题吗?
    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2010-11-21
    • 2021-09-01
    • 1970-01-01
    相关资源
    最近更新 更多