Assertj-Swagger PropertyType不匹配:Swagger定义测试报错求助
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-Typeheader 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)
- For raw binary: Ensure it's set to
内容的提问来源于stack exchange,提问作者lazy

