diff --git a/processor/processor.go b/processor/processor.go index 5decc94..09caa79 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -61,7 +61,7 @@ func Process(config *config.Config) ([]types.GroupVersionDetails, error) { return nil, fmt.Errorf("failed to find API types in directory %s:%w", config.SourcePath, err) } - p.types.InlineTypes() + p.types.InlineTypes(p.propagateReference) // collect references between types for typeName, refs := range p.references { @@ -490,6 +490,18 @@ func (p *processor) loadAliasType(typeDef *types.Type, pkg *loader.Package, unde return p.processType(tPkg, tInfo, depth+1) } +// Every child that has a reference to 'originalType', will also get a reference to 'additionalType'. +func (p *processor) propagateReference(originalType *types.Type, additionalType *types.Type) { + originalTypeKey := types.Key(originalType) + additionalTypeKey := types.Key(additionalType) + + for _, parentRefs := range p.references { + if _, ok := parentRefs[originalTypeKey]; ok { + parentRefs[additionalTypeKey] = struct{}{} + } + } +} + func (p *processor) addReference(parent *types.Type, child *types.Type) { if child == nil || child.Kind == types.BasicKind { return diff --git a/test/api/v1/guestbook_types.go b/test/api/v1/guestbook_types.go index a64274d..1b825ad 100644 --- a/test/api/v1/guestbook_types.go +++ b/test/api/v1/guestbook_types.go @@ -40,7 +40,11 @@ type Embedded3 struct { D string `json:"d,omitempty"` } type Embedded4 struct { - C string `json:"c,omitempty"` + C string `json:"c,omitempty"` + EmbeddedX `json:",inline"` +} +type EmbeddedX struct { + X string `json:"x,omitempty"` } // GuestbookSpec defines the desired state of Guestbook. diff --git a/test/expected.asciidoc b/test/expected.asciidoc index 2076a27..fbb4414 100644 --- a/test/expected.asciidoc +++ b/test/expected.asciidoc @@ -35,11 +35,33 @@ Package v1 contains API Schema definitions for the webapp v1 API group | *`a`* __string__ | | *`b`* __string__ | | *`c`* __string__ | +| *`x`* __string__ | | *`d`* __string__ | | *`e`* __string__ | |=== +[id="{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embeddedx"] +==== EmbeddedX + + + +.Appears In: +**** +- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded[$$Embedded$$] +- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded1[$$Embedded1$$] +- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded2[$$Embedded2$$] +- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded3[$$Embedded3$$] +- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded4[$$Embedded4$$] +**** + +[cols="25a,75a", options="header"] +|=== +| Field | Description +| *`x`* __string__ | +|=== + + [id="{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-guestbook"] ==== Guestbook diff --git a/test/expected.md b/test/expected.md index 1471acb..ced7303 100644 --- a/test/expected.md +++ b/test/expected.md @@ -30,10 +30,29 @@ Package v1 contains API Schema definitions for the webapp v1 API group | `a` _string_ | | | `b` _string_ | | | `c` _string_ | | +| `x` _string_ | | | `d` _string_ | | | `e` _string_ | | +#### EmbeddedX + + + + + +_Appears in:_ +- [Embedded](#embedded) +- [Embedded1](#embedded1) +- [Embedded2](#embedded2) +- [Embedded3](#embedded3) +- [Embedded4](#embedded4) + +| Field | Description | +| --- | --- | +| `x` _string_ | | + + #### Guestbook diff --git a/types/types.go b/types/types.go index f1e4300..b25e664 100644 --- a/types/types.go +++ b/types/types.go @@ -223,7 +223,7 @@ func (t *Type) ContainsInlinedTypes() bool { // TypeMap is a map of Type elements type TypeMap map[string]*Type -func (types TypeMap) InlineTypes() { +func (types TypeMap) InlineTypes(propagateReference func(original *Type, additional *Type)) { // If C is inlined in B, and B is inlined in A; the fields of C are copied // into B before the fields of B is copied into A. The ideal order of // iterating and inlining fields is NOT known. Worst-case, only one type's @@ -254,6 +254,7 @@ func (types TypeMap) InlineTypes() { zap.S().Debugw("Inlining embedded type", "type", t, "embeddedType", t.Fields[i].Type) t.Fields.inlineType(i, embeddedType) + propagateReference(embeddedType, t) } } }