|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package embedallof |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/go-openapi/codescan" |
| 13 | + "github.com/go-openapi/spec" |
| 14 | + "github.com/go-openapi/testify/v2/assert" |
| 15 | + "github.com/go-openapi/testify/v2/require" |
| 16 | +) |
| 17 | + |
| 18 | +func examplesRoot(t *testing.T) string { |
| 19 | + t.Helper() |
| 20 | + _, thisFile, _, ok := runtime.Caller(0) |
| 21 | + require.True(t, ok) |
| 22 | + return filepath.Clean(filepath.Join(filepath.Dir(thisFile), "..", "..")) |
| 23 | +} |
| 24 | + |
| 25 | +// scanEmbedAllOf scans the witness tree with DefaultAllOfForEmbeds off or on. |
| 26 | +func scanEmbedAllOf(t *testing.T, on bool) *spec.Swagger { |
| 27 | + t.Helper() |
| 28 | + doc, err := codescan.Run(&codescan.Options{ |
| 29 | + WorkDir: examplesRoot(t), |
| 30 | + Packages: []string{"./shaping/embedallof/..."}, |
| 31 | + ScanModels: true, |
| 32 | + DefaultAllOfForEmbeds: on, |
| 33 | + }) |
| 34 | + require.NoError(t, err) |
| 35 | + require.NotNil(t, doc) |
| 36 | + return doc |
| 37 | +} |
| 38 | + |
| 39 | +// goldenRaw marshals an arbitrary value into testdata/<feature>.json, |
| 40 | +// honouring UPDATE_GOLDEN. |
| 41 | +// |
| 42 | +// Regenerate with: UPDATE_GOLDEN=1 go test ./... |
| 43 | +func goldenRaw(t *testing.T, feature string, v any) { |
| 44 | + t.Helper() |
| 45 | + got, err := json.MarshalIndent(v, "", " ") |
| 46 | + require.NoError(t, err) |
| 47 | + got = append(got, '\n') |
| 48 | + |
| 49 | + golden := filepath.Join("testdata", feature+".json") |
| 50 | + if os.Getenv("UPDATE_GOLDEN") != "" { |
| 51 | + require.NoError(t, os.WriteFile(golden, got, 0o600)) |
| 52 | + } |
| 53 | + want, err := os.ReadFile(golden) |
| 54 | + require.NoError(t, err) |
| 55 | + assert.JSONEq(t, string(want), string(got)) |
| 56 | +} |
| 57 | + |
| 58 | +// allOfHasRef reports whether any allOf member of sch is a $ref to ref. |
| 59 | +func allOfHasRef(sch spec.Schema, ref string) bool { |
| 60 | + for _, m := range sch.AllOf { |
| 61 | + if m.Ref.String() == ref { |
| 62 | + return true |
| 63 | + } |
| 64 | + } |
| 65 | + return false |
| 66 | +} |
| 67 | + |
| 68 | +// allOfHasProp reports whether any allOf member of sch carries property name. |
| 69 | +func allOfHasProp(sch spec.Schema, name string) bool { |
| 70 | + for _, m := range sch.AllOf { |
| 71 | + if _, ok := m.Properties[name]; ok { |
| 72 | + return true |
| 73 | + } |
| 74 | + } |
| 75 | + return false |
| 76 | +} |
| 77 | + |
| 78 | +// TestEmbedAllOf_Off is the control: with the flag off a plain embed inlines |
| 79 | +// the embedded type's properties (byte-identical to the historic behaviour). |
| 80 | +func TestEmbedAllOf_Off(t *testing.T) { |
| 81 | + doc := scanEmbedAllOf(t, false) |
| 82 | + pe, ok := doc.Definitions["PlainEmbed"] |
| 83 | + require.True(t, ok, "PlainEmbed definition missing") |
| 84 | + goldenRaw(t, "plainembed_off", pe) |
| 85 | + |
| 86 | + // Inlined flat: every embedded + own field lands as a top-level property. |
| 87 | + assert.Empty(t, pe.AllOf, "plain embed must not compose when off") |
| 88 | + for _, p := range []string{"id", "name", "note", "color"} { |
| 89 | + _, has := pe.Properties[p] |
| 90 | + assert.True(t, has, "field %q inlined when off", p) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// TestEmbedAllOf_On exercises the feature: a plain model embed becomes an allOf |
| 95 | +// $ref member, a plain non-model embed an inline allOf member, and the embedding |
| 96 | +// struct's own fields a sibling allOf member. A json-named embed and an explicit |
| 97 | +// swagger:allOf embed are unaffected. |
| 98 | +func TestEmbedAllOf_On(t *testing.T) { |
| 99 | + doc := scanEmbedAllOf(t, true) |
| 100 | + pe, ok := doc.Definitions["PlainEmbed"] |
| 101 | + require.True(t, ok, "PlainEmbed definition missing") |
| 102 | + goldenRaw(t, "plainembed_on", pe) |
| 103 | + |
| 104 | + // PlainEmbed composes; no embedded/own field is inlined at the top level. |
| 105 | + require.NotEmpty(t, pe.AllOf, "plain embed must compose into allOf when on") |
| 106 | + _, hasID := pe.Properties["id"] |
| 107 | + assert.False(t, hasID, "model embed must not inline when composing") |
| 108 | + assert.True(t, allOfHasRef(pe, "#/definitions/Base"), "model embed → $ref allOf member") |
| 109 | + assert.True(t, allOfHasProp(pe, "note"), "non-model embed → inline allOf member") |
| 110 | + assert.True(t, allOfHasProp(pe, "color"), "own field → sibling allOf member") |
| 111 | + |
| 112 | + // PointerEmbed: *Base is peeled to the same $ref composition. |
| 113 | + ptr := doc.Definitions["PointerEmbed"] |
| 114 | + require.NotEmpty(t, ptr.AllOf) |
| 115 | + assert.True(t, allOfHasRef(ptr, "#/definitions/Base"), "pointer embed peels to a $ref member") |
| 116 | + assert.True(t, allOfHasProp(ptr, "tag"), "own field in sibling member") |
| 117 | + |
| 118 | + // NamedEmbed: a json-named embed stays a single nested property, unaffected. |
| 119 | + named := doc.Definitions["NamedEmbed"] |
| 120 | + assert.Empty(t, named.AllOf, "a json-named embed must not compose") |
| 121 | + _, hasBase := named.Properties["base"] |
| 122 | + assert.True(t, hasBase, "named embed nests under its json name") |
| 123 | + |
| 124 | + // TaggedEmbed: an explicit swagger:allOf embed is already composition. |
| 125 | + tagged := doc.Definitions["TaggedEmbed"] |
| 126 | + assert.True(t, allOfHasRef(tagged, "#/definitions/Base"), "swagger:allOf embed already composes") |
| 127 | +} |
0 commit comments