Skip to content

Fix duplicate map keys when a type embeds two structs sharing a field - #631

Open
HarnageaGabriel wants to merge 1 commit into
kubernetes:masterfrom
HarnageaGabriel:fix-129-duplicate-embedded-type
Open

Fix duplicate map keys when a type embeds two structs sharing a field#631
HarnageaGabriel wants to merge 1 commit into
kubernetes:masterfrom
HarnageaGabriel:fix-129-duplicate-embedded-type

Conversation

@HarnageaGabriel

Copy link
Copy Markdown

Summary

generateMembers recursed into embedded struct members without tracking which field names it had already emitted. When a type embeds two different structs that (directly or transitively) provide a field with the same name — e.g. both indirectly embed metav1.TypeMeta, both exposing kind/apiVersiongenerateProperty was called twice for that name. That produced a Go map literal in the generated zz_generated.openapi.go with a duplicate key, which fails to compile:

pkg/openapi/zz_generated.openapi.go:24107:6: duplicate key "kind" in map literal
pkg/openapi/zz_generated.openapi.go:24114:6: duplicate key "apiVersion" in map literal

This mirrors the case reported in #129 (ServiceServingCertSignerConfig embedding two types that both embedded metav1.TypeMeta).

Fix

generateMembers now resolves embedded fields breadth-first by depth, matching Go's own field-promotion rules:

  • a field at a shallower embedding depth wins over the same-named field at a deeper depth
  • two fields tying at the same shallowest depth are ambiguous and are excluded entirely (mirroring Go's unpromoted/ambiguous-selector behavior), instead of silently emitting a duplicate map key
  • the required field list is deduplicated the same way

Test plan

  • go build ./...
  • go test ./pkg/generators/... (255 tests passed)
  • Added regression tests covering: two same-depth embeds with a shared field name (ambiguous, excluded from both Properties and Required), and a shallower embed's field winning over a deeper one with the same name

Fixes #129

generateMembers recursed into embedded members without tracking which
field names had already been emitted. When two different embedded
types provided a field with the same name (e.g. both indirectly embed
metav1.TypeMeta, both exposing "kind"/"apiVersion"), generateProperty
was invoked twice for that name, producing a Go map literal with a
duplicate key in the generated zz_generated.openapi.go, which fails to
compile.

Resolve embedded fields breadth-first by depth, matching Go's own
embedding rules: a field at a shallower depth wins over one deeper,
and two fields tying at the same shallowest depth are ambiguous and
excluded entirely (mirroring Go's unpromoted-ambiguous-selector
behavior) instead of silently duplicating a key.

Fixes kubernetes#129
@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 10, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: HarnageaGabriel / name: HarnageaGabriel (1d3ff01)

@kubernetes-prow kubernetes-prow Bot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Aug 10, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @HarnageaGabriel!

It looks like this is your first PR to kubernetes/kube-openapi 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kube-openapi has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: HarnageaGabriel
Once this PR has been reviewed and has the lgtm label, please assign sttts for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow
kubernetes-prow Bot requested review from Jefftree and sttts August 10, 2026 20:53
@kubernetes-prow kubernetes-prow Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Having the same type embedded more than once breaks generation

1 participant