GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow Struct Embedding with `allOf` by clarkjohnd · Pull Request #2086 · oapi-codegen/oapi-codegen

Conversation

@clarkjohnd

@clarkjohnd clarkjohnd commented Sep 12, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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
All reactions
  • 👍 6 reactions
lahabana and others added 3 commits September 11, 2025 22:49
@lahabana @clarkjohnd
add option x-go-allof-embed-refs
7dc35d7
This is useful when working with extending APIs

Signed-off-by: Charly Molter <[email protected]>
@clarkjohnd
identify overlapping field in error
7e63712
@clarkjohnd
Fix base error change
5389038
@clarkjohnd
clarkjohnd requested a review from a team as a code owner September 12, 2025 14:29
@kusari-inspector

kusari-inspector Bot commented Sep 12, 2025

Copy link
Copy Markdown

Kusari Inspector

Kusari Analysis Results:

Proceed with these changes

✅ No Flagged Issues Detected
All values appear to be within acceptable risk parameters.

No pinned version dependency changes, code issues or exposed secrets detected!

Note

View full detailed analysis result for more information on the output and the checks that were run.


@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!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@marcoabreu

marcoabreu commented Dec 10, 2025

Copy link
Copy Markdown

Would love to see this merged, good job!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@clarkjohnd
Merge branch 'main' into xEmbedRef
b109546
@mromaszewicz

mromaszewicz commented Feb 24, 2026

Copy link
Copy Markdown
Member

@greptileai

greptile-apps[bot] reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@greptile-apps

greptile-apps Bot commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

greptile-apps[bot]
greptile-apps Bot reviewed Feb 24, 2026
View reviewed changes

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

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.

10 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread internal/test/all_of/embed-openapi.yaml
Comment on lines +33 to +36
allOf:
- $ref: "#/components/schemas/PersonProperties"
- required: [ FirstName, LastName ]
- $ref: "#/components/schemas/FirstName"

@greptile-apps greptile-apps Bot Feb 24, 2026

Copy link
Copy Markdown
Contributor

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.

All reactions
@mromaszewicz

mromaszewicz commented Feb 24, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@greptile-apps greptile-apps[bot] greptile-apps[bot] left review comments

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

Labels

None yet

Projects

None yet

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.

4 participants

@clarkjohnd @marcoabreu @mromaszewicz @lahabana
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.