Title: strict-server: response types silently drop data for allOf-composed unions and properties + additionalProperties · Issue #2549 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: strict-server: response types silently drop data for allOf-composed unions and properties + additionalProperties · Issue #2549 · oapi-codegen/oapi-codegen
X Title: strict-server: response types silently drop data for allOf-composed unions and properties + additionalProperties · Issue #2549 · oapi-codegen/oapi-codegen
Description: PR #2332 fixed strict-server response types losing their model's MarshalJSON, closing #970 and #1665. Two shapes are still affected: the response is emitted as a defined type with no delegation, and encoding/json quietly serialises an in...
Open Graph Description: PR #2332 fixed strict-server response types losing their model's MarshalJSON, closing #970 and #1665. Two shapes are still affected: the response is emitted as a defined type with no delegation, an...
X Description: PR #2332 fixed strict-server response types losing their model's MarshalJSON, closing #970 and #1665. Two shapes are still affected: the response is emitted as a defined type with no delegation...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/2549
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"strict-server: response types silently drop data for allOf-composed unions and properties + additionalProperties","articleBody":"PR #2332 fixed strict-server response types losing their model's `MarshalJSON`, closing #970 and #1665. Two shapes are still affected: the response is emitted as a defined type with no delegation, and `encoding/json` quietly serialises an incomplete body. No error, no compile failure — just a 200 with fields missing.\n\n## Environment\n\n- `oapi-codegen` **v2.8.0**\n- `oapi-codegen/runtime` v1.6.0\n- Go **1.26.4** (darwin/arm64)\n\n## Configuration\n\n```yaml\npackage: repro\ngenerate:\n strict-server: true\n gorilla-server: true\n models: true\noutput-options:\n skip-prune: true\n```\n\n## Minimal specification\n\n```yaml\nopenapi: \"3.0.0\"\ninfo:\n title: allOf-composed union repro\n version: \"1.0\"\npaths:\n /thing:\n get:\n operationId: getThing\n responses:\n \"200\":\n description: ok\n content:\n application/json:\n schema: { $ref: \"#/components/schemas/Thing\" }\n /bag:\n get:\n operationId: getBag\n responses:\n \"200\":\n description: ok\n content:\n application/json:\n schema: { $ref: \"#/components/schemas/Bag\" }\ncomponents:\n schemas:\n Base:\n type: object\n required: [id]\n properties:\n id: { type: string }\n # case 1: union nested inside an allOf member\n Thing:\n allOf:\n - $ref: \"#/components/schemas/Base\"\n - oneOf:\n - $ref: \"#/components/schemas/Cat\"\n - $ref: \"#/components/schemas/Dog\"\n Cat:\n type: object\n properties: { meow: { type: string } }\n Dog:\n type: object\n properties: { woof: { type: string } }\n # case 2: fixed properties alongside additionalProperties\n Bag:\n type: object\n required: [kind]\n properties:\n kind: { type: string }\n additionalProperties: true\n```\n\n## Current behaviour\n\nThe **models** are generated correctly. Both get a custom marshaller:\n\n```go\ntype Thing struct {\n\tId string `json:\"id\"`\n\tunion json.RawMessage\n}\nfunc (t Thing) MarshalJSON() ([]byte, error) { ... }\n\ntype Bag struct {\n\tKind string `json:\"kind\"`\n\tAdditionalProperties map[string]interface{} `json:\"-\"`\n}\nfunc (a Bag) MarshalJSON() ([]byte, error) { ... }\n```\n\nThe **response types** do not, so nothing delegates to those marshallers:\n\n```go\ntype GetThing200JSONResponse Thing // no MarshalJSON\ntype GetBag200JSONResponse Bag // no MarshalJSON\n```\n\nThe generated visitor encodes the response value directly:\n\n```go\nfunc (response GetThing200JSONResponse) VisitGetThingResponse(w http.ResponseWriter) error {\n\tvar buf bytes.Buffer\n\tif err := json.NewEncoder(&buf).Encode(response); err != nil {\n\t\treturn err\n\t}\n\t...\n}\n```\n\nBecause `GetThing200JSONResponse` is a defined type it does not inherit `Thing`'s method, so `encoding/json` falls back to struct-field encoding and drops the unexported `union` field. Same for `Bag`, whose `AdditionalProperties` map is tagged `json:\"-\"`.\n\nServing each response through its generated visitor:\n\n| Response value | Actual (v2.8.0) | Expected |\n|---|---|---|\n| `Thing{Id: \"abc\"}` + `FromCat(Cat{Meow: \"purr\"})` | `{\"id\":\"abc\"}` | `{\"id\":\"abc\",\"meow\":\"purr\"}` |\n| `Bag{Kind: \"demo\", AdditionalProperties: {\"extra\": 42}}` | `{\"kind\":\"demo\"}` | `{\"extra\":42,\"kind\":\"demo\"}` |\n\n## Expected behaviour\n\nThe response type should delegate to the model's marshaller, exactly as #2332 already does for a response whose schema is directly a `oneOf`/`anyOf`.\n","author":{"url":"https://github.com/PieterVoorwinden","@type":"Person","name":"PieterVoorwinden"},"datePublished":"2026-09-02T09:51:01Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/2549"}
| 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:4330aa4e-dab3-3f83-73aa-64de0ad87c98 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9ACA:110E43:2CA3FED:3AF0826:6AF9CAD1 |
| html-safe-nonce | 5c8bb80c47b0e8899f7585ff5aac67ecfee111b61a8ed025990d94b55b127bb3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QUNBOjExMEU0MzoyQ0EzRkVEOjNBRjA4MjY6NkFGOUNBRDEiLCJ2aXNpdG9yX2lkIjoiMzk0MDA0NDExNTQxOTk3NDM1MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fad6cf855ad62adbdd4135332b5eb0a9d9d9eed9880e8ffb44372efe7e090a26 |
| hovercard-subject-tag | issue:5322386207 |
| 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/2549/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9a4c636ecc581aa01e40c8b5ce93bfcb026ae5b86c70785fb295c21b6469fda6/oapi-codegen/oapi-codegen/issues/2549 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9a4c636ecc581aa01e40c8b5ce93bfcb026ae5b86c70785fb295c21b6469fda6/oapi-codegen/oapi-codegen/issues/2549 |
| og:image:alt | PR #2332 fixed strict-server response types losing their model's MarshalJSON, closing #970 and #1665. Two shapes are still affected: the response is emitted as a defined type with no delegation, an... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | PieterVoorwinden |
| 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