Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions bundler/bundler_path_order_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 4 additions & 10 deletions datamodel/high/v3/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 4 additions & 10 deletions datamodel/high/v3/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
Loading