为何Dredd未按OpenAPI v3规范校验接口响应?
It sounds like you're running into issues with Dredd's default configuration not enforcing strict schema validation for your OpenAPI v3 spec. Let's break down the common reasons this happens and how to fix it:
1. Dredd might not be recognizing your spec as OpenAPI v3
First, ensure your full OpenAPI spec includes the top-level openapi field specifying the version. If this is missing, Dredd may fall back to treating it as a Swagger v2 spec, which has different validation behavior:
openapi: 3.0.0 # Add this at the root of your spec info: title: Your API version: 1.0.0 # ... rest of your paths/components
When running Dredd, explicitly tell it to use OpenAPI v3 with the --openapi flag, or set it in your dredd.yml config:
spec: your-api-spec.yaml endpoint: http://your-server-url openapi: 3.0.0
2. Default validation rules are too lenient
Dredd uses Ajv under the hood for JSON Schema validation, and its default settings don't enforce strict type checking or catch all schema mismatches. To enable strict validation, add these options to your dredd.yml:
options: jsonSchemaStrict: true ajvOptions: strict: true allErrors: true # Report all validation errors, not just the first one
With these settings:
- A response with
"name": "somestring"(string) will fail against a schema expecting an integer - A response object will fail if your schema specifies a plain string type
- Missing required fields (like changing
nametowwwin the schema but gettingnamein the response) will trigger a failure
3. Double-check you haven't disabled response validation
Make sure you're not running Dredd with the --no-validate-responses flag, which skips all response schema checks entirely. If this flag is present, remove it to re-enable validation.
4. Verify your Dredd version is up-to-date
Older versions of Dredd had incomplete support for OpenAPI v3. Run dredd --version to check, and update if needed with npm install -g dredd.
Once you adjust these settings, Dredd should correctly flag all the mismatches you're expecting: type inconsistencies, incorrect schema structures, and missing required fields.
内容的提问来源于stack exchange,提问作者y.luis.rojo

