feat: add allowDefault option to operation-2xx-response - #3046
feat: add allowDefault option to operation-2xx-response#3046dimitropoulos wants to merge 1 commit into
Conversation
The rule treats a `default` response as satisfying the 2xx requirement. `allowDefault: false` turns that off, requiring an explicit 2xx status code. Defaults to `true`, so existing behavior is unchanged.
🦋 Changeset detectedLatest commit: 52677b1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Pull request overview
Adds an opt-in allowDefault option to the operation-2xx-response rule so teams that rely on code generation can require an explicit 2xx response instead of allowing default to satisfy the rule. The default remains true, preserving current behavior unless configured otherwise.
Changes:
- Added
allowDefault?: booleanplumbing throughOperation2xxResponseintovalidateResponseCodes(defaulting totrue). - Updated the
validateResponseCodes“default counts as 2xx” clause to be gated byallowDefault. - Added unit test coverage for
allowDefault: false, plus updated rule documentation and a changeset.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/rules/utils.ts | Gates the default-counts-as-2xx behavior behind the new allowDefault option (default true). |
| packages/core/src/rules/common/operation-2xx-response.ts | Exposes allowDefault as a rule option and forwards it into shared response-code validation. |
| packages/core/src/rules/common/tests/operation-2xx-response.test.ts | Adds a unit test asserting default-only responses fail when allowDefault: false. |
| docs/@v2/rules/oas/operation-2xx-response.md | Documents the new allowDefault option and its motivation/usage. |
| .changeset/olive-pugs-repeat.md | Publishes the new rule option as a minor change for core + CLI packages. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Hi @dimitropoulos! Thanks for the contribution and bringing this up. We will review the PR as soon as possible. To unblock you now, you can get the same check with a custom plugin. const ExplicitSuccessResponse = () => {
return {
Paths: {
Responses(responses, { report }) {
const hasSuccessCode = Object.keys(responses || {}).some((code) =>
/^2[0-9Xx]{2}$/.test(code)
);
if (!hasSuccessCode) {
report({
message: 'Operation must define an explicit 2xx response.',
location: { reportOnKey: true },
});
}
},
},
};
};
export default function ExplicitSuccessResponsePlugin() {
return {
id: 'codegen',
rules: {
oas3: {
'explicit-2xx-response': ExplicitSuccessResponse,
},
},
};
}
plugins:
- explicit-2xx.mjs
rules:
codegen/explicit-2xx-response: errorMore about plugins you can find here. I hope it will help you. |
What/Why/How?
Adds
allowDefaulttooperation-2xx-response. Defaulttrue, so nothing changes unless you opt in.Here at Cloudflare, this is causing code-generation to fail. Today
defaultsatisfies the rule, so an operation whose only response isdefaultpasses. But our code generator reads the 2xx response to produce the operation's return type.defaultis the catch-all for responses that aren't listed, so there's no success shape to model in codegen, and the generated client ends up with an untyped or empty result - so it breaks downstreamallowDefault: falserequires an explicit 2xx.Threaded through
validateResponseCodes, which guards the clause oncodeRange === '2XX', sooperation-4xx-responseis unaffected.Reference
The clause this gates:
validateResponseCodesinpackages/core/src/rules/utils.ts.Testing
Added a rule test for
allowDefault: falseon adefault-only operation. Existing tests cover the unchanged default. Full unit suite passes; the 9tests/e2e/respect/failures are pre-existing onmain.Check yourself
Security
Note
Low Risk
Opt-in lint rule option with backward-compatible default; no auth, data, or runtime behavior changes.
Overview
Adds an opt-in
allowDefaultoption to theoperation-2xx-responserule so adefaultresponse no longer has to count as a successful 2xx.Default remains
true(previous behavior). WithallowDefault: false, operations that only declaredefaultfail the rule, which helps code generators that need an explicit success status for return types. The flag is threaded throughvalidateResponseCodesand only applies to the 2XX range, sooperation-4xx-responseis unchanged.Reviewed by Cursor Bugbot for commit 52677b1. Bugbot is set up for automated code reviews on this repo. Configure here.