You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Assertj-Swagger PropertyType不匹配:Swagger定义测试报错求助

Fixing AssertJMultipleFailuresError for Swagger Response Schema Mismatch

Let's break down why you're seeing this error and how to fix it:

The Root Cause

Your Swagger YAML defines the 200 response as type: string, format: byte, which according to Swagger 2.0 specifications represents base64-encoded binary data (a string value in the response body). However, your backend returns a raw byte[] array, which translates to a raw binary response (not a base64 string). This mismatch between the schema's expected type (StringProperty) and the actual response type (ByteArrayProperty) is causing the test failure.

Solutions to Fix the Mismatch

Option 1: Adjust the Swagger Schema for Raw Binary Responses

If your endpoint is intended to return raw binary data (like a file download with Content-Type: application/octet-stream), update your YAML to use the correct format for raw binary:

responses:
  200:
    description: Successful binary response
    schema:
      type: string
      format: binary  # Use this for raw binary data, not "byte"
    produces:
      - application/octet-stream  # Explicitly declare the binary content type

This tells your testing tool to expect a raw byte array (ByteArrayProperty) instead of a base64 string, matching your backend's actual response.

Option 2: Modify the Backend to Return a Base64 String

If you intended the response to be a base64-encoded string (e.g., embedded in a JSON response), adjust your backend code to convert the byte[] to a base64 string before returning it. For example, in Java:

import java.util.Base64;

// Convert byte[] to base64 string
String base64Data = Base64.getEncoder().encodeToString(yourByteArray);
return base64Data;

This will make the response body a string that aligns with your original Swagger schema (type: string, format: byte).

Additional Checks

  • Verify the Content-Type header your backend sends:
    • For raw binary: Ensure it's set to application/octet-stream
    • For base64 strings: Use application/json (or your usual JSON content type)

内容的提问来源于stack exchange,提问作者lazy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 23:07:38