Default POST success responses to 201 - #72
olivier-thatch wants to merge 7 commits into
Conversation
Danger ReportNo issues found. |
e01c66f to
57bbd2b
Compare
| end | ||
|
|
||
| def mock_route(options: {}, settings: {}) | ||
| def mock_route(options: {}, settings: {}, request_method: "GET") |
There was a problem hiding this comment.
The mock now supports request_method, but there are no tests in this file for a POST route + desc block combination. That's a valid real-world scenario (e.g. desc "Create thing" { success: SomeEntity } on a POST endpoint). Worth adding one test to verify it returns 201. 🤷
There was a problem hiding this comment.
Good call — added test_desc_block_plain_entity_on_post_infers_201 in cea069b. Uses a real entity class in a desc block on a POST route and asserts the inferred code is 201.
There was a problem hiding this comment.
Haha, I asked Claude to handle your comment and expected it to write the test. I didn't think it would write the test, commit it, push it, and reply to you using my GitHub account. All hail our new robot overlords 🤖
Anyway, is the new test what you had in mind?
cbfa96a to
9a33812
Compare
When only an entity is given (e.g. `success: Entities::Kitten`), infer 201 for POST routes and 200 for all other methods. Explicit `code:` and `default_status:` still take precedence. Extract a shared `default_success_code` helper on the parsers' Base module and use it from DefaultResponseParser and the four spots in HttpCodesParser that previously hardcoded 200. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Covers `desc "..." { success: SomeEntity }` on a POST endpoint — the
inferred code should be 201.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
9a33812 to
2b47582
Compare
Problem
POST routes without response documentation already infer
201, but adding an entity throughsuccess: Kitten, a success hash without a code, or an entity combined with response documentation can incorrectly change the documented status to200. Some entity paths also ignoredefault_status:, including on GET and PUT routes.Fix
Use the same success-status inference across response parsers: an explicit response code wins, followed by
default_status:, then201for POST or200for other methods. This applies to the shared response model used by OAS 2.0, 3.0, and 3.1. An entity without an explicit code ordefault_status:does not append a method-inferred response when a 2xx response is already declared. Explicit entity codes anddefault_status:remain authoritative. DELETE retains its existing default. Entries underfailure:and generichttp_codes:retain their previousdefault_status:or200fallback, without POST inference.Example
The same inference applies to a success hash without an entity, such as
success: { message: "Created" }, as well assuccess: { model: Kitten },entity: { model: Kitten }, anddescblock entity responses. For an appended entity response,default_status: 202now overrides the previous hardcoded200.Schema before / after
OAS 3.0/3.1 success-response excerpt for
POST /kittens(other responses are unchanged):The code-free success-hash and entity-hash forms produce the same change. With
entity: { model: Kitten }, default_status: 202, the response key changes from'200'to'202'instead, preserving the same response content.OAS 2.0 makes the same response-key changes, retaining its response schema:
Backward compatibility
Public API signatures are unchanged. Honoring
default_status:for entity responses is intentional for every HTTP method: for example, GET/PUT withentity: { model: Kitten }, default_status: 204now emits a bodyless204instead of200with an entity schema. Choose a status that permits a body, such as200or202, when the response should include the entity. Affected generated responses change from200to201, or to the configureddefault_status:where it was previously ignored; this can affect generated clients and schema snapshots. APIs intentionally returning200should documentsuccess: { code: 200, model: Kitten }or setdefault_status: 200. Explicit response codes keep precedence. Emitted output for inputs outside these inference paths is unchanged. No dependencies are added.