oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Merge properties when combining schemas with `allOf`

Open skomorokh opened this issue 4 months ago • 0 comments

Currently oapi-codegen doesn't merge property definitions when combining schemas with allOf and instead takes the complete property from the last one it sees: https://github.com/oapi-codegen/oapi-codegen/blob/0d94f2f2015e5fec2cd66273eeb8eff7d1f86bc1/pkg/codegen/merge_schemas.go#L203

This means we can't create a schema for a JSON Merge Patch endpoint that inherits types and everything else from a base schema and just makes some of them nullable (as sending null is how one clears a value in RFC 7396). This approach is described here: https://blog.stoplight.io/reuse-openapi-descriptions Instead we have to maintain redundant types/formats/descriptions/etc. for the properties.

From this doc in the spec it does seem like properties are expected to be inherited: https://swagger.io/docs/specification/v3_0/data-models/inheritance-and-polymorphism/

And other implementations seem to merge them: https://github.com/Redocly/redoc/blob/34623575ce4741280df4c960f926c3751022b026/src/services/OpenAPIParser.ts#L276

Understandably a full merge would be an undertaking, reflection is not ideal, and something like mergo would be a somewhat heavy extra dep. However, even a shallow merge of a subset of Schema properties would enable this PATCH use case and presumably many others.

skomorokh avatar Oct 17 '25 20:10 skomorokh