GeezerVison is showing github.com — Allow Struct Embedding with `allOf` by clarkjohnd · Pull Request #2086 · oapi-codegen/oapi-codegen
Conversation
clarkjohnd
commented
Sep 12, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
A rebase of 2 year old PR (#1295) (main code courtesy of Lahabana!) to the latest main branch with a very small tweak to error handling. This is a feature I find very useful. Also addresses #1622 (comment), where the original draft PR was linked.
Using one of my own app examples, adding x-go-allof-embed-refs: true to allOf components:
WorkItemView:
x-go-allof-embed-refs: true
allOf:
- $ref: '#/components/schemas/WorkItem'
- type: object
properties:
project:
$ref: '#/components/schemas/ProjectSummary'
organisation:
$ref: '#/components/schemas/OrganisationSummary'
type WorkItemView struct {
// all fields duplicated from root component WorkItem
// ...
// ...
Organisation *OrganisationSummary `json:"organisation,omitempty"`
Project *ProjectSummary `json:"project,omitempty"`
}
to:
type WorkItemView struct {
// Embedded struct due to allOf(#/components/schemas/WorkItem)
WorkItem `yaml:",inline"`
// Embedded fields due to inline allOf schema
Organisation *OrganisationSummary `json:"organisation,omitempty"`
Project *ProjectSummary `json:"project,omitempty"`
}
This helps build reusable functions for "extendable" types - e.g. I have a function to populate WorkItem - now if I extend work item to add other fields for other API paths (like above example), I can reuse the same function to manage the WorkItem nested struct, rather than rebuilding an entire new function for the extended type.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
LarsFronius, lahabana, ngrash, oliwer, marcoabreu, and stendler-mlpa reacted with thumbs up emoji
This is useful when working with extending APIs
Signed-off-by: Charly Molter <[email protected]>
|

Kusari Analysis Results:

✅ No Flagged Issues Detected
All values appear to be within acceptable risk parameters.
No pinned version dependency changes, code issues or exposed secrets detected!
@kusari-inspector rerun - Trigger a re-analysis of this PR
@kusari-inspector feedback [your message] - Send feedback to our AI and team
See Kusari's documentation for setup and configuration.
Commit: 5389038, performed at: 2025-09-12T14:29:19Z
Found this helpful? Give it a 👍 or 👎 reaction!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Would love to see this merged, good job!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
greptile-apps[bot] reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Greptile Summary
This PR adds support for struct embedding in allOf compositions via the x-go-allof-embed-refs: true extension. The implementation allows referenced schemas to be embedded as structs rather than having their fields duplicated, enabling better code reuse for extendable types.
Key Changes:
- Added
x-go-allof-embed-refs extension support in extension.go with a helper function to read the boolean value
- Modified schema merging logic in
merge_schemas.go to conditionally generate embedded structs when the extension is enabled
- Added validation to prevent overlapping fields between sibling schemas and modifications to required fields outside their own schema when using embedding
- Added comprehensive test cases to verify the feature works correctly
Potential Issue:
- The test case in
embed-openapi.yaml appears to have overlapping FirstName fields between PersonProperties and FirstName schemas in the Person allOf, which should trigger validation errors according to the implementation
Confidence Score: 3/5
- This PR adds a useful feature but has a test case that may not align with validation logic
- The core implementation is solid with proper error handling and validation checks. However, the test schema appears to have overlapping fields that should fail validation based on the implemented checks. This needs verification that either the test is correct or the validation logic needs adjustment.
internal/test/all_of/embed-openapi.yaml needs verification - check if the overlapping FirstName field is intentional or if it should be removed from one of the schemas
Important Files Changed
| Filename |
Overview |
| pkg/codegen/extension.go |
Added new extension constant and helper function for x-go-allof-embed-refs feature |
| pkg/codegen/merge_schemas.go |
Core implementation of embedding feature with validation for overlapping fields and required field modifications |
| pkg/codegen/schema.go |
Added call to ReadExtGoAllOfEmbedRefs to enable the feature when processing allOf schemas |
| internal/test/all_of/embed-openapi.yaml |
Test schema with potential overlapping FirstName field in Person allOf that may violate validation rules |
Last reviewed commit: b109546
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Comment on lines
+33
to
+36
|
|
allOf:
|
|
|
- $ref: "#/components/schemas/PersonProperties"
|
|
|
- required: [ FirstName, LastName ]
|
|
|
- $ref: "#/components/schemas/FirstName"
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential overlapping field issue: both PersonProperties (line 34) and FirstName (line 36) define a FirstName property. The validation logic in merge_schemas.go:215-219 checks for overlapping fields and should reject this when x-go-allof-embed-refs: true is set. Verify that this test case generates correctly or if it needs to be updated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
This is a very tricky change, despite seeming simple, and I think we need lots more testing to ensure it doesn't break behavior.
When you embed structs, you also embed their methods.
In your example, say, WorkItem is a complex schema with a complex structure that requires implementing the json.Marshaler and json.Unmarshaler interfaces, which we do automatically.
Due to this embedding, when the json library is doing its unarshaling, it will find that WorkItemView has JSON marshaling specializations, and it will call those, so it will attempt to unmarshal WorkItemView using WorkItem.UnmarshalJSON.
I've hit these problems before.
Dynamic typing in a stricly typed language is quite hard, and allOf/anyOf/oneOf has been the most difficult thing to support in oapi-codegen.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
At least 1 approving review is required to merge this pull request.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|