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
36 changes: 36 additions & 0 deletions datamodel/high/v3/path_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ func TestPathItem(t *testing.T) {
assert.Equal(t, 1, r.GoLow().Servers.KeyNode.Line)
}

func TestPathItem_NullOperations(t *testing.T) {
yml := `get: null
post:
description: create
put:
additionalOperations:
FOO: null
BAR:
description: bar`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n lowV3.PathItem
_ = low.BuildModel(&idxNode, &n)
require.NoError(t, n.Build(context.Background(), nil, idxNode.Content[0], idx))

r := NewPathItem(&n)
assert.Nil(t, r.Get)
assert.Nil(t, r.Put)
require.NotNil(t, r.Post)
assert.Equal(t, "create", r.Post.Description)

ops := r.GetOperations()
assert.Equal(t, 2, ops.Len())
_, ok := ops.Get("get")
assert.False(t, ok)
_, ok = ops.Get("FOO")
assert.False(t, ok)

require.NotNil(t, r.AdditionalOperations)
assert.Equal(t, 1, r.AdditionalOperations.Len())
assert.Equal(t, "bar", r.AdditionalOperations.GetOrZero("BAR").Description)
}

func TestPathItem_WithAdditionalOperations(t *testing.T) {
// Test for lines 132-133 and 204-210: Additional operations support in OpenAPI 3.2+
// Create a proper low-level PathItem with AdditionalOperations
Expand Down
6 changes: 6 additions & 0 deletions datamodel/low/v2/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ func (p *PathItem) Build(ctx context.Context, _, root *yaml.Node, idx *index.Spe
continue // ignore everything else.
}

// an operation is a mapping. `get:`, `get: null` or any other non-mapping value
// holds no operation, so don't build an empty one for it.
if !utils.IsNodeMap(pathNode) {
continue
}

var op Operation
if err := buildPathItemOperationModel(pathNode, &op); err != nil {
return err
Expand Down
29 changes: 29 additions & 0 deletions datamodel/low/v2/path_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ func TestPathItem_Build_MethodModelBuildFail(t *testing.T) {
assert.ErrorContains(t, err, "model boom")
}

func TestPathItem_Build_NonMappingOperations(t *testing.T) {
yml := `get: null
put:
post: ~
delete: nope
head: []
options: ''
patch: {}`

var idxNode yaml.Node
require.NoError(t, yaml.Unmarshal([]byte(yml), &idxNode))
idx := index.NewSpecIndex(&idxNode)

var n PathItem
require.NoError(t, low.BuildModel(idxNode.Content[0], &n))
require.NoError(t, n.Build(context.Background(), nil, idxNode.Content[0], idx))

assert.True(t, n.Get.IsEmpty())
assert.True(t, n.Put.IsEmpty())
assert.True(t, n.Post.IsEmpty())
assert.True(t, n.Delete.IsEmpty())
assert.True(t, n.Head.IsEmpty())
assert.True(t, n.Options.IsEmpty())

// an empty mapping is an operation, just an empty one.
require.False(t, n.Patch.IsEmpty())
assert.NotNil(t, n.Patch.Value)
}

func TestPathItem_Hash(t *testing.T) {
yml := `get:
description: get me up
Expand Down
8 changes: 8 additions & 0 deletions datamodel/low/v3/path_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ func (p *PathItem) Build(ctx context.Context, keyNode, root *yaml.Node, idx *ind
if err != nil {
return err
}
// an operation is a mapping. `get:`, `get: null` or any other non-mapping value
// holds no operation, so don't build an empty one for it.
if !utils.IsNodeMap(pathNode) {
continue
}
var op Operation
if err := low.BuildModel(pathNode, &op); err != nil {
return err
Expand Down Expand Up @@ -380,6 +385,9 @@ func (p *PathItem) Build(ctx context.Context, keyNode, root *yaml.Node, idx *ind
if err != nil {
return err
}
if !utils.IsNodeMap(opValueNode) {
continue
}
var addOp Operation
if err := low.BuildModel(opValueNode, &addOp); err != nil {
return err
Expand Down
84 changes: 84 additions & 0 deletions datamodel/low/v3/path_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,90 @@ func TestPathItem_Build_StandardOperationUnknownYAMLKey(t *testing.T) {
assert.NoError(t, err)
}

func TestPathItem_Build_NonMappingOperations(t *testing.T) {
yml := `x-nothing: null
get: null
put:
post: ~
delete: nope
head: []
options:
$ref: '#/x-nothing'
trace: null
query: ''
patch: {}
ROOT:
$ref: '#/x-nothing'
additionalOperations:
FOO: null
BAR: {}
BAZ: nope
QUX:
REF:
$ref: '#/x-nothing'`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n PathItem
_ = low.BuildModel(idxNode.Content[0], &n)
err := n.Build(context.Background(), nil, idxNode.Content[0], idx)
require.NoError(t, err)

assert.True(t, n.Get.IsEmpty())
assert.True(t, n.Put.IsEmpty())
assert.True(t, n.Post.IsEmpty())
assert.True(t, n.Delete.IsEmpty())
assert.True(t, n.Head.IsEmpty())
assert.True(t, n.Options.IsEmpty())
assert.True(t, n.Trace.IsEmpty())
assert.True(t, n.Query.IsEmpty())

// an empty mapping is an operation, just an empty one.
require.False(t, n.Patch.IsEmpty())
assert.NotNil(t, n.Patch.Value)

require.NotNil(t, n.AdditionalOperations.Value)
var keys []string
for k := range n.AdditionalOperations.Value.FromOldest() {
keys = append(keys, k.Value)
}
assert.Equal(t, []string{"BAR"}, keys)
}

func TestPathItem_Build_OnlyNullAdditionalOperations(t *testing.T) {
yml := `additionalOperations:
FOO: null`

var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)

var n PathItem
_ = low.BuildModel(idxNode.Content[0], &n)
require.NoError(t, n.Build(context.Background(), nil, idxNode.Content[0], idx))
assert.True(t, n.AdditionalOperations.IsEmpty())
}

func TestPathItem_Hash_NullOperation(t *testing.T) {
build := func(yml string) *PathItem {
var idxNode yaml.Node
require.NoError(t, yaml.Unmarshal([]byte(yml), &idxNode))
idx := index.NewSpecIndex(&idxNode)
var n PathItem
require.NoError(t, low.BuildModel(idxNode.Content[0], &n))
require.NoError(t, n.Build(context.Background(), nil, idxNode.Content[0], idx))
return &n
}

absent := build("summary: s")
null := build("summary: s\nget: null")
empty := build("summary: s\nget: {}")
assert.Equal(t, absent.Hash(), null.Hash())
assert.NotEqual(t, absent.Hash(), empty.Hash())
}

func TestPathItem_Build_AdditionalOperationsUnknownYAMLKey(t *testing.T) {
// YAML keys matching unexported fields (e.g., "context") are silently ignored
// by BuildModel; the build succeeds since the key is simply unrecognized.
Expand Down
Loading