Title: Add an output option for generating a `default` case in response parsing function · Issue #1923 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: Add an output option for generating a `default` case in response parsing function · Issue #1923 · oapi-codegen/oapi-codegen
X Title: Add an output option for generating a `default` case in response parsing function · Issue #1923 · oapi-codegen/oapi-codegen
Description: There're times when you don't want to list all of the possible responses and you only care about a few(or you don't all of the responses), but you would like to know if there was an unexpected response. Currently using this yaml file: op...
Open Graph Description: There're times when you don't want to list all of the possible responses and you only care about a few(or you don't all of the responses), but you would like to know if there was an unexpected resp...
X Description: There're times when you don't want to list all of the possible responses and you only care about a few(or you don't all of the responses), but you would like to know if there was an une...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/1923
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add an output option for generating a `default` case in response parsing function","articleBody":"There're times when you don't want to list all of the possible responses and you only care about a few(or you don't all of the responses), but you would like to know if there was an unexpected response. Currently using this yaml file:\n```yaml\nopenapi: 3.0.3\npaths:\n /test:\n post:\n responses:\n '200':\n content:\n application/json:\n schema:\n $ref: '#/components/schemas/Foo'\ncomponents:\n schemas:\n Foo:\n type: object\n properties:\n bar:\n type: string\n required:\n - bar\n```\nYou get such a parsing function:\n```go\nfunc ParsePostTestResponse(rsp *http.Response) (*PostTestResponse, error) {\n bodyBytes, err := io.ReadAll(rsp.Body)\n defer func() { _ = rsp.Body.Close() }()\n if err != nil {\n return nil, err\n }\n\n response := &PostTestResponse{\n Body: bodyBytes,\n HTTPResponse: rsp,\n }\n\n switch {\n case strings.Contains(rsp.Header.Get(\"Content-Type\"), \"json\") && rsp.StatusCode == 200:\n var dest Foo\n if err := json.Unmarshal(bodyBytes, &dest); err != nil {\n return nil, err\n }\n response.JSON200 = &dest\n\n }\n\n return response, nil\n}\n```\nThe new output option would make the `switch` look like this:\n```go\nswitch {\n case strings.Contains(rsp.Header.Get(\"Content-Type\"), \"json\") && rsp.StatusCode == 200:\n var dest Foo\n if err := json.Unmarshal(bodyBytes, &dest); err != nil {\n return nil, err\n }\n response.JSON200 = &dest\n default:\n return nil, ErrUnexpectedResponse\n}\n```","author":{"url":"https://github.com/MilkeeyCat","@type":"Person","name":"MilkeeyCat"},"datePublished":"2025-03-12T20:02:31Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/1923"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2d09d738-486e-c7d8-6835-f7a0af71f759 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9258:39BFDB:2DEAF5B:3BCBD35:6AF9BE81 |
| html-safe-nonce | 4ec33e894fafdbb6065ebde2624250260a33c673c9fe8275fd721c29ef2cf662 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjU4OjM5QkZEQjoyREVBRjVCOjNCQ0JEMzU6NkFGOUJFODEiLCJ2aXNpdG9yX2lkIjoiMTk2MjUyMjUwNjY5MDE1MDAxNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a566f375f52a01d68a01fc4de4c33a6c06cc38d0e04f600961e1ce17396d6cdf |
| hovercard-subject-tag | issue:2915037515 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/oapi-codegen/oapi-codegen/1923/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b463f02058b0badf26631fd1d187b6a540078de0828c90fa301054ba622a4ea3/oapi-codegen/oapi-codegen/issues/1923 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b463f02058b0badf26631fd1d187b6a540078de0828c90fa301054ba622a4ea3/oapi-codegen/oapi-codegen/issues/1923 |
| og:image:alt | There're times when you don't want to list all of the possible responses and you only care about a few(or you don't all of the responses), but you would like to know if there was an unexpected resp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | MilkeeyCat |
| hostname | github.com |
| expected-hostname | github.com |
| None | 612d3e6ffd693e7ea8d9fcf452370995872baef31d825b1fedef704cd3d24ac7 |
| turbo-cache-control | no-preview |
| go-import | github.com/oapi-codegen/oapi-codegen git https://github.com/oapi-codegen/oapi-codegen.git |
| octolytics-dimension-user_id | 142752710 |
| octolytics-dimension-user_login | oapi-codegen |
| octolytics-dimension-repository_id | 173009358 |
| octolytics-dimension-repository_nwo | oapi-codegen/oapi-codegen |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 173009358 |
| octolytics-dimension-repository_network_root_nwo | oapi-codegen/oapi-codegen |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 072e34035c13c31543fba9f025cbbbda4210dae4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width