diff --git a/datamodel/high/v3/path_item_test.go b/datamodel/high/v3/path_item_test.go index 21669794..42e2e697 100644 --- a/datamodel/high/v3/path_item_test.go +++ b/datamodel/high/v3/path_item_test.go @@ -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 diff --git a/datamodel/low/v2/path_item.go b/datamodel/low/v2/path_item.go index ca2a1969..3801cf89 100644 --- a/datamodel/low/v2/path_item.go +++ b/datamodel/low/v2/path_item.go @@ -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 diff --git a/datamodel/low/v2/path_item_test.go b/datamodel/low/v2/path_item_test.go index 846dd2c2..501fcdf4 100644 --- a/datamodel/low/v2/path_item_test.go +++ b/datamodel/low/v2/path_item_test.go @@ -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 diff --git a/datamodel/low/v3/path_item.go b/datamodel/low/v3/path_item.go index 7f8d40fa..1aeca6c6 100644 --- a/datamodel/low/v3/path_item.go +++ b/datamodel/low/v3/path_item.go @@ -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 @@ -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 diff --git a/datamodel/low/v3/path_item_test.go b/datamodel/low/v3/path_item_test.go index 8fd01582..fc1712cc 100644 --- a/datamodel/low/v3/path_item_test.go +++ b/datamodel/low/v3/path_item_test.go @@ -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.