feat: add disallowDefault option to operation-2xx-response - #3046
AlbinaBlazhko17 merged 7 commits 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: 7b26dbf 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. |
Performance Benchmark (Lower is Faster)
|
|
Overall looks good. One question on scope: does a wide-range |
|
and, once I got to doing it I saw that it's a pretty small backwards-compatible change, so I figured it'd be worth having. if you're happy I'm happy! please merge away |
|
Hey @dimitropoulos ! Looks good to me. BTW, have you considered using our SDK generator? The quick overview of the feature you can find there. |
|
@dimitropoulos I think the change is good too. Thank you for the suggestion. We need to add some additional information to our repo to explain we prefer configuration options to have a default value of |
thanks @adamaltman - no, hadn't heard of it! Looks like it's pretty new (last few days?!). congrats! I will say at first glance the most promising thing to me is that you generate TanStack Query - not just because all things TanStack are obviously amazing, but because it shows you're thinking of your generated outputs in a more general way than just what programming languages to target. There's so much more that can be done with codegen from OpenAPI specs than many of the current top companies are doing, and this is a great example of opening up that line of innovation. nice! we'll definitely keep in on our radar. it helps that literally every interaction with the redocly team has been so pleasant. I'm sure there's not reason not to share that internal to the teams working on this at Cloudflare it's common to hear "well if we have a problem we can just make an issue or PR to the redocly team and more often than not it'll be released by tomorrow evening". you all run a tight ship and it really shows in the quality of the software, but also shows in the way it's all managed. thanks for being so responsive! |
|
@adamaltman sure no problemo, flipped it in 5b415f2 @JLekawa I saw you added a little addition too (sortof addressing the same thing) so feel free to either drop 5b415f2 and just merge as it was before, or keep it - either way sounds great to me. |
|
Thanks @dimitropoulos for the contribution and feedback! The change is released in |
What/Why/How?
Adds
disallowDefaulttooperation-2xx-response. Defaultfalse, 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 downstreamdisallowDefault: truerequires 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
disallowDefault: 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
Backward-compatible lint rule extension with default off; only affects OpenAPI validation when users opt in.
Overview
Adds an opt-in
disallowDefaultoption to theoperation-2xx-responselint rule. WhendisallowDefault: true, adefaultresponse no longer satisfies the requirement for a successful response—operations must declare an explicit 2xx status code. Default remainsfalse, so existing configs behave the same.The flag is passed from
operation-2xx-response(paths and webhooks) intovalidateResponseCodes, which only treatsdefaultas a 2xx substitute whendisallowDefaultis off. Docs and a changeset describe configuration and codegen motivation; a unit test coversdefault-only operations failing when the option is enabled.Reviewed by Cursor Bugbot for commit 7b26dbf. Bugbot is set up for automated code reviews on this repo. Configure here.