Title: schema-merging-behavior v1: an allOf of a non-object $ref serializes under the member's type name · Issue #2590 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: schema-merging-behavior v1: an allOf of a non-object $ref serializes under the member's type name · Issue #2590 · oapi-codegen/oapi-codegen
X Title: schema-merging-behavior v1: an allOf of a non-object $ref serializes under the member's type name · Issue #2590 · oapi-codegen/oapi-codegen
Description: Found while reviewing #2514. It's pre-existing on main, and independent of that PR. Problem Under schema-merging-behavior: v1 (or old-merge-schemas: true), an allOf member that is a $ref gets embedded in a struct, whatever its type: type...
Open Graph Description: Found while reviewing #2514. It's pre-existing on main, and independent of that PR. Problem Under schema-merging-behavior: v1 (or old-merge-schemas: true), an allOf member that is a $ref gets embed...
X Description: Found while reviewing #2514. It's pre-existing on main, and independent of that PR. Problem Under schema-merging-behavior: v1 (or old-merge-schemas: true), an allOf member that is a $ref gets e...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/2590
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"schema-merging-behavior v1: an allOf of a non-object $ref serializes under the member's type name","articleBody":"Found while reviewing #2514. It's pre-existing on `main`, and independent of that PR.\n\n## Problem\n\nUnder `schema-merging-behavior: v1` (or `old-merge-schemas: true`), an `allOf` member that is a `$ref` gets embedded in a struct, whatever its type:\n\n```go\ntype WrapName struct {\n\tName `yaml:\",inline\"` // Name = string\n}\n```\n\n`encoding/json` only flattens embedded *structs*. It encodes an embedded field of any other type as a regular field named after the type. So when the referenced schema isn't an object (untyped `{}`, a primitive, an array, or a map from `additionalProperties`), the value goes on the wire wrapped in an object keyed by the Go type name:\n\n| member | v1 marshals as | expected | v1 unmarshals the expected value |\n|---|---|---|---|\n| `Untyped: {}` | `{\"Untyped\":\"x\"}` | `\"x\"` | error: cannot unmarshal string |\n| `Name: {type: string}` | `{\"Name\":\"x\"}` | `\"x\"` | error: cannot unmarshal string |\n| `Names: {type: array, items: {type: string}}` | `{\"Names\":[\"a\",\"b\"]}` | `[\"a\",\"b\"]` | error: cannot unmarshal array |\n| `Labels: {type: object, additionalProperties: {type: string}}` | `{\"Labels\":{\"k\":\"v\"}}` | `{\"k\":\"v\"}` | no error, but `Labels` is left empty |\n| `Obj: {type: object, properties: …}` | `{\"v\":\"x\"}` | `{\"v\":\"x\"}` | works |\n\nThis affects models, clients and servers alike, including strict-server responses. v2 and v3 are fine: they lower a single-member `allOf` to an alias (`type WrapName = Name`).\n\n## Reproduction\n\n```yaml\nopenapi: \"3.0.3\"\ninfo: {title: t, version: \"1\"}\npaths: {}\ncomponents:\n schemas:\n Name: {type: string}\n WrapName:\n allOf:\n - $ref: '#/components/schemas/Name'\n```\n\nConfig: `generate: {models: true}`, `output-options: {skip-prune: true}`, `compatibility: {schema-merging-behavior: v1}`.\n\n```go\nb, _ := json.Marshal(WrapName{Name: \"x\"})\nfmt.Println(string(b)) // {\"Name\":\"x\"}, expected \"x\"\n\nvar w WrapName\nerr := json.Unmarshal([]byte(`\"x\"`), &w) // json: cannot unmarshal string into Go value of type WrapName\n```\n\nThe same happens for an `allOf` of an untyped `$ref` followed by sibling keywords (`allOf: [{$ref: Untyped}, {description: …}]`), and for a component that is itself such an `allOf`.\n\n## Notes\n\n- One fix keeps v1's Go shape, so `WrapName{Name: \"x\"}` still compiles: when every other member contributes no fields, generate `MarshalJSON`/`UnmarshalJSON` on the struct that delegate to the embedded non-object member. Switching those cases to an alias, as v2 does, would also fix the wire but would change the Go type.\n- The wire encoding has never matched the spec, so there's nothing to preserve there.\n- `internal/test/servers/strict/responsebody_v1` (added in the #2514 follow-up) records the current `{\"Untyped\": …}` encoding. Update it with the fix.\n","author":{"url":"https://github.com/mromaszewicz","@type":"Person","name":"mromaszewicz"},"datePublished":"2026-09-27T23:11:57Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/2590"}
| 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:186d61f8-f644-4aaa-7690-542b398fa09a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9AB0:26FE0B:298550A:37312FE:6AF9CACB |
| html-safe-nonce | 1e077922d30e24795dd44eb8c26e792c77a2c063433cf28f0483cb14e4e1c511 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QUIwOjI2RkUwQjoyOTg1NTBBOjM3MzEyRkU6NkFGOUNBQ0IiLCJ2aXNpdG9yX2lkIjoiODMwMDMzODU3Njk1NjcwNTQ4MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e2714fe6c24d7e39f96f64ccb253bfd698b4a9636e844f49b93e0a7af677f420 |
| hovercard-subject-tag | issue:5607139117 |
| 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/2590/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6d520f447e1333a894b3361d89da20852e9028ef68039efe41984ee66cba5197/oapi-codegen/oapi-codegen/issues/2590 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6d520f447e1333a894b3361d89da20852e9028ef68039efe41984ee66cba5197/oapi-codegen/oapi-codegen/issues/2590 |
| og:image:alt | Found while reviewing #2514. It's pre-existing on main, and independent of that PR. Problem Under schema-merging-behavior: v1 (or old-merge-schemas: true), an allOf member that is a $ref gets embed... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mromaszewicz |
| 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