diff --git a/bundler/bundler_path_order_test.go b/bundler/bundler_path_order_test.go new file mode 100644 index 00000000..4c82d374 --- /dev/null +++ b/bundler/bundler_path_order_test.go @@ -0,0 +1,113 @@ +// Copyright 2026 Princess Beef Heavy Industries, LLC / Dave Shanley +// https://pb33f.io + +package bundler + +import ( + "os" + "path/filepath" + "testing" + + "github.com/pb33f/go-yaml" + "github.com/pb33f/libopenapi" + "github.com/pb33f/libopenapi/datamodel" + "github.com/pb33f/testify/assert" + "github.com/pb33f/testify/require" +) + +// https://github.com/pb33f/libopenapi/issues/733 +func TestBundleDocumentComposed_PreservesRootOrderOfReferencedItems(t *testing.T) { + dir := t.TempDir() + spec := []byte(`openapi: 3.0.3 +info: {title: t, version: '1'} +paths: + /c: + get: + responses: + '200': {description: ok} + callbacks: + onEvent: + '{$request.body#/c}': + post: + responses: + '200': {description: ok} + '{$request.body#/b}': + $ref: './refs.yaml#/items/b' + '{$request.body#/a}': + $ref: './refs.yaml#/items/a' + /b: + $ref: './refs.yaml#/items/b' + /a: + $ref: './refs.yaml#/items/a' + /d: + get: + responses: + '200': {description: ok} +`) + refs := []byte(`items: + a: + post: + responses: + '200': {description: ok} + b: + post: + responses: + '200': {description: ok} +`) + specPath := filepath.Join(dir, "openapi.yaml") + require.NoError(t, os.WriteFile(specPath, spec, 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(dir, "refs.yaml"), refs, 0o600)) + + doc, err := libopenapi.NewDocumentWithConfiguration(spec, &datamodel.DocumentConfiguration{ + SpecFilePath: specPath, + BasePath: dir, + AllowFileReferences: true, + }) + require.NoError(t, err) + model, err := doc.BuildV3Model() + require.NoError(t, err) + + paths := model.Model.Paths + callback := paths.PathItems.GetOrZero("/c").Get.Callbacks.GetOrZero("onEvent") + + wantPaths := []string{"/c", "/b", "/a", "/d"} + wantExpressions := []string{"{$request.body#/c}", "{$request.body#/b}", "{$request.body#/a}"} + + renderedKeys := func(v interface{ MarshalYAML() (interface{}, error) }, inline bool) []string { + var out interface{} + var err error + if inline { + out, err = v.(interface{ MarshalYAMLInline() (interface{}, error) }).MarshalYAMLInline() + } else { + out, err = v.MarshalYAML() + } + require.NoError(t, err) + var keys []string + content := out.(*yaml.Node).Content + for i := 0; i < len(content); i += 2 { + keys = append(keys, content[i].Value) + } + return keys + } + + for _, inline := range []bool{false, true} { + assert.Equal(t, wantPaths, renderedKeys(paths, inline), "paths, inline=%t", inline) + assert.Equal(t, wantExpressions, renderedKeys(callback, inline), "callback, inline=%t", inline) + } + + bundled, err := BundleDocumentComposed(&model.Model, nil) + require.NoError(t, err) + + var root yaml.Node + require.NoError(t, yaml.Unmarshal(bundled, &root)) + var got []string + top := root.Content[0].Content + for i := 0; i < len(top); i += 2 { + if top[i].Value == "paths" { + for j := 0; j < len(top[i+1].Content); j += 2 { + got = append(got, top[i+1].Content[j].Value) + } + } + } + assert.Equal(t, wantPaths, got) +} diff --git a/datamodel/high/v3/callback.go b/datamodel/high/v3/callback.go index 03df9906..227e1e5a 100644 --- a/datamodel/high/v3/callback.go +++ b/datamodel/high/v3/callback.go @@ -103,13 +103,10 @@ func (c *Callback) MarshalYAML() (interface{}, error) { ln := 9999 // default to a high value to weight new content to the bottom. var style yaml.Style if c.low != nil { - lpi := c.low.FindExpression(k) - if lpi != nil { - ln = lpi.ValueNode.Line - } - for lk := range c.low.Expression.KeysFromOldest() { if lk.Value == k { + // the key lives in this document, a $ref'd value may not. + ln = lk.KeyNode.Line style = lk.KeyNode.Style break } @@ -203,13 +200,10 @@ func (c *Callback) marshalYAMLInlineInternal(ctx any) (interface{}, error) { ln := 9999 // default to a high value to weight new content to the bottom. var style yaml.Style if c.low != nil { - lpi := c.low.FindExpression(k) - if lpi != nil { - ln = lpi.ValueNode.Line - } - for lk := range c.low.Expression.KeysFromOldest() { if lk.Value == k { + // the key lives in this document, a $ref'd value may not. + ln = lk.KeyNode.Line style = lk.KeyNode.Style break } diff --git a/datamodel/high/v3/paths.go b/datamodel/high/v3/paths.go index b84b14c6..39e82305 100644 --- a/datamodel/high/v3/paths.go +++ b/datamodel/high/v3/paths.go @@ -91,13 +91,10 @@ func (p *Paths) MarshalYAML() (interface{}, error) { ln := 9999 // default to a high value to weight new content to the bottom. var style yaml.Style if p.low != nil { - lpi := p.low.FindPath(k) - if lpi != nil { - ln = lpi.ValueNode.Line - } - for lk := range p.low.PathItems.KeysFromOldest() { if lk.Value == k { + // the key lives in this document, a $ref'd value may not. + ln = lk.KeyNode.Line style = lk.KeyNode.Style break } @@ -169,13 +166,10 @@ func (p *Paths) marshalYAMLInlineWithContext(ctx any) (interface{}, error) { ln := 9999 // default to a high value to weight new content to the bottom. var style yaml.Style if p.low != nil { - lpi := p.low.FindPath(k) - if lpi != nil { - ln = lpi.ValueNode.Line - } - for lk := range p.low.PathItems.KeysFromOldest() { if lk.Value == k { + // the key lives in this document, a $ref'd value may not. + ln = lk.KeyNode.Line style = lk.KeyNode.Style break }