|
| 1 | +package oq_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/speakeasy-api/openapi/graph" |
| 8 | + "github.com/speakeasy-api/openapi/openapi" |
| 9 | + "github.com/speakeasy-api/openapi/oq" |
| 10 | + "github.com/speakeasy-api/openapi/references" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +// The trailing table row is indented one space further than the rows above it, which |
| 17 | +// makes yaml.v3 emit an extra line break before it on every encode. The break lands |
| 18 | +// inside the scalar, so it changes the decoded value rather than just the layout. |
| 19 | +const foldedScalarSpec = `openapi: 3.1.0 |
| 20 | +info: |
| 21 | + title: Test |
| 22 | + version: 1.0.0 |
| 23 | +paths: {} |
| 24 | +components: |
| 25 | + schemas: |
| 26 | + Widget: |
| 27 | + type: object |
| 28 | + description: >- |
| 29 | + ### Widgets |
| 30 | +
|
| 31 | + | Name | Kind | |
| 32 | + | ---- | ---- | |
| 33 | + | acme | ` + "`petstore`" + ` | |
| 34 | +` |
| 35 | + |
| 36 | +func loadFoldedScalarGraph(t *testing.T) *graph.SchemaGraph { |
| 37 | + t.Helper() |
| 38 | + |
| 39 | + ctx := t.Context() |
| 40 | + |
| 41 | + doc, _, err := openapi.Unmarshal(ctx, strings.NewReader(foldedScalarSpec), openapi.WithSkipValidation()) |
| 42 | + require.NoError(t, err) |
| 43 | + require.NotNil(t, doc) |
| 44 | + |
| 45 | + idx := openapi.BuildIndex(ctx, doc, references.ResolveOptions{ |
| 46 | + RootDocument: doc, |
| 47 | + TargetDocument: doc, |
| 48 | + TargetLocation: "spec.yaml", |
| 49 | + }) |
| 50 | + |
| 51 | + return graph.Build(ctx, idx) |
| 52 | +} |
| 53 | + |
| 54 | +// formattedDescription pulls the description back out of a `key:\n <schema>` wrapper. |
| 55 | +func formattedDescription(t *testing.T, formatted string) string { |
| 56 | + t.Helper() |
| 57 | + |
| 58 | + var decoded map[string]struct { |
| 59 | + Description string `yaml:"description"` |
| 60 | + } |
| 61 | + require.NoError(t, yaml.Unmarshal([]byte(formatted), &decoded)) |
| 62 | + require.Len(t, decoded, 1) |
| 63 | + |
| 64 | + for _, schema := range decoded { |
| 65 | + return schema.Description |
| 66 | + } |
| 67 | + |
| 68 | + return "" |
| 69 | +} |
| 70 | + |
| 71 | +// `openapi spec query --format yaml` marshals graph nodes with its own encoder, so it |
| 72 | +// needs the same stabilization as every other encode boundary. |
| 73 | +func TestFormatYAML_FoldedScalarKeepsItsValue(t *testing.T) { |
| 74 | + t.Parallel() |
| 75 | + |
| 76 | + var want struct { |
| 77 | + Components struct { |
| 78 | + Schemas map[string]struct { |
| 79 | + Description string `yaml:"description"` |
| 80 | + } `yaml:"schemas"` |
| 81 | + } `yaml:"components"` |
| 82 | + } |
| 83 | + require.NoError(t, yaml.Unmarshal([]byte(foldedScalarSpec), &want)) |
| 84 | + wantDescription := want.Components.Schemas["Widget"].Description |
| 85 | + require.Contains(t, wantDescription, "| acme |") |
| 86 | + |
| 87 | + g := loadFoldedScalarGraph(t) |
| 88 | + |
| 89 | + result, err := oq.Execute(`schemas | where(name == "Widget")`, g) |
| 90 | + require.NoError(t, err) |
| 91 | + require.Len(t, result.Rows, 1) |
| 92 | + |
| 93 | + formatted := oq.FormatYAML(result, g) |
| 94 | + require.NotEmpty(t, formatted) |
| 95 | + |
| 96 | + assert.Equal(t, wantDescription, formattedDescription(t, formatted)) |
| 97 | + assert.NotContains(t, formatted, ">-", "affected folded scalars should be emitted as literal blocks") |
| 98 | +} |
| 99 | + |
| 100 | +// Formatting the same result twice must not drift, since FormatYAML restyles graph |
| 101 | +// nodes in place. |
| 102 | +func TestFormatYAML_FoldedScalarIsAFixedPoint(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + |
| 105 | + g := loadFoldedScalarGraph(t) |
| 106 | + |
| 107 | + result, err := oq.Execute(`schemas | where(name == "Widget")`, g) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + first := oq.FormatYAML(result, g) |
| 111 | + for i := range 3 { |
| 112 | + assert.Equal(t, first, oq.FormatYAML(result, g), "output changed on format %d", i+2) |
| 113 | + } |
| 114 | +} |
0 commit comments