Title: Merging schema's with undefined properties doesn't work · Issue #1883 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: Merging schema's with undefined properties doesn't work · Issue #1883 · oapi-codegen/oapi-codegen
X Title: Merging schema's with undefined properties doesn't work · Issue #1883 · oapi-codegen/oapi-codegen
Description: oapi-codegen isn't able to merge schema's which have incompatible properties, even if the properties aren't defined (!). If a property needs to be overwritten, or should be defined at a later moment depending on context, it's common to u...
Open Graph Description: oapi-codegen isn't able to merge schema's which have incompatible properties, even if the properties aren't defined (!). If a property needs to be overwritten, or should be defined at a later momen...
X Description: oapi-codegen isn't able to merge schema's which have incompatible properties, even if the properties aren't defined (!). If a property needs to be overwritten, or should be defined at a...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/1883
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Merging schema's with undefined properties doesn't work","articleBody":"oapi-codegen isn't able to merge schema's which have incompatible properties, even if the properties aren't defined (!).\r\n\r\n\r\nIf a property needs to be overwritten, or should be defined at a later moment depending on context, it's common to use `allOf` + `readOnly: true`. Example:\r\n\r\n```yaml\r\nallOf:\r\n - $ref '#/components/myField'\r\nreadOnly: true\r\n```\r\n\r\n\r\n## Reproduce\r\n\r\nIn this example, I defined `identityId`, which is shared across payloads, but should /sometimes/ be set to readOnly. \r\n\r\n### `config.yaml`\r\n```yaml\r\npackage: identities\r\ngenerate:\r\n client: true\r\n echo-server: true\r\n strict-server: true\r\n embedded-spec: false\r\n models: true\r\noutput: ./identities/identities.gen.go\r\n```\r\n\r\n### `identities.yaml`\r\n```\r\nopenapi: 3.0.0\r\ninfo:\r\n title: test\r\n version: 0.0.0\r\npaths:\r\n /identities/{identityId}:\r\n get:\r\n operationId: getIdentity\r\n parameters:\r\n - $ref: '#/components/parameters/PathIdentityId'\r\n responses:\r\n '200':\r\n description: get identity\r\n content:\r\n application/json:\r\n schema:\r\n $ref: '#/components/schemas/Identity'\r\n post:\r\n operationId: updateIdentity\r\n requestBody:\r\n required: true\r\n content:\r\n application/json:\r\n schema:\r\n allOf:\r\n - $ref: '#/components/schemas/Identity'\r\n - type: object\r\n properties:\r\n identity_id: # setting identityId to readOnly, so it can't be changed/upserted.\r\n readOnly: true\r\n parameters:\r\n - $ref: '#/components/parameters/PathIdentityId'\r\n responses:\r\n '200':\r\n description: get identity\r\n content:\r\n application/json:\r\n schema:\r\n $ref: '#/components/schemas/Identity'\r\ncomponents:\r\n parameters:\r\n PathIdentityId:\r\n in: path\r\n name: identityId\r\n schema:\r\n $ref: '#/components/schemas/IdentityId'\r\n required: true\r\n schemas:\r\n IdentityId:\r\n description: unique id of an identity\r\n type: string\r\n pattern: ^[ 0-9a-zA-Z- ]+$\r\n minLength: 1\r\n maxLength: 64\r\n example: 89e0ec57-4a7c-41a0-9433-970ba7709733\r\n Identity:\r\n type: object\r\n required:\r\n - identity_id\r\n - name\r\n properties:\r\n identity_id:\r\n $ref: '#/components/schemas/IdentityId'\r\n name:\r\n type: string\r\n```\r\n\r\n### cmd\r\n\r\n\r\n`go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@v2.4.1 --config=./config/rest-gen/identities.yml ./internal/rest/identities/identities.yaml`\r\n\r\nthrows the following error:\r\n\r\n```\r\nerror generating code: error generating type definitions: error generating Go types for component schemas: error converting Schema User to Go type: error generating Go schema for property 'identity_id': error merging schemas: error merging schemas for AllOf: merging two schemas with different ReadOnly\r\nexit status 1\r\n```\r\n\r\n\r\nLooking at the code, it seems that the reader is defaulting to `false`, and has no way to differentiate later if the property was _set_ to `false`, or defaulted to `false`.\r\nhttps://github.com/oapi-codegen/oapi-codegen/blob/main/pkg/codegen/merge_schemas.go#L176-L178","author":{"url":"https://github.com/thumbnail","@type":"Person","name":"thumbnail"},"datePublished":"2025-01-15T08:02:32Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/1883"}
| 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:e83a65cd-eb71-a9cc-f9bf-ec4567ce60d2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AE48:D23AD:272310:33FCC7:6AF9CC9F |
| html-safe-nonce | 0e5e04968a4004d8b5f3723d56340640f2649d656e5961702754b43242620350 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTQ4OkQyM0FEOjI3MjMxMDozM0ZDQzc6NkFGOUNDOUYiLCJ2aXNpdG9yX2lkIjoiMjY0NzUxNDYyNDgxNTEzNjMxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5d829b71dd2e1dc2d74a04aaa1f64c1fad5f8785ac7820d710af29409bf34155 |
| hovercard-subject-tag | issue:2789094111 |
| 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/1883/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e074504ebea54228f5d3472f0143279c6615b62fa98d3b1ed3f69673b8b44b6e/oapi-codegen/oapi-codegen/issues/1883 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e074504ebea54228f5d3472f0143279c6615b62fa98d3b1ed3f69673b8b44b6e/oapi-codegen/oapi-codegen/issues/1883 |
| og:image:alt | oapi-codegen isn't able to merge schema's which have incompatible properties, even if the properties aren't defined (!). If a property needs to be overwritten, or should be defined at a later momen... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | thumbnail |
| 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