diff --git a/datamodel/low/node_map.go b/datamodel/low/node_map.go index b1ace9c8..d9433ca4 100644 --- a/datamodel/low/node_map.go +++ b/datamodel/low/node_map.go @@ -28,7 +28,8 @@ type AddNodes interface { // // Node maps are filled for every object built but read only by tools that map lines back to objects, so // writes are recorded as they happen and the line index is built when the nodes are first read. -// NodeLines is safe for concurrent use, and its zero value is ready to use. +// NodeLines is safe for concurrent use, and its zero value is ready to use. A nil *NodeLines reads as +// empty and ignores writes. type NodeLines struct { mu sync.Mutex pending []nodeLineWrite @@ -48,6 +49,9 @@ func (n *NodeLines) Store(line int, value any) { // Load returns the value held for a line, and whether the line holds one. func (n *NodeLines) Load(line int) (any, bool) { + if n == nil { + return nil, false + } n.mu.Lock() defer n.mu.Unlock() value, ok := n.index()[line] @@ -57,6 +61,9 @@ func (n *NodeLines) Load(line int) (any, bool) { // Range calls f for each line and its value, in line order, until f returns false. The lines are read // before f is first called, so f may write to the nodes. func (n *NodeLines) Range(f func(line int, value any) bool) { + if n == nil { + return + } n.mu.Lock() index := n.index() lines := make([]int, 0, len(index)) @@ -82,6 +89,11 @@ func (n *NodeLines) add(line int, node *yaml.Node) { } func (n *NodeLines) write(w nodeLineWrite) { + if n == nil { + // Objects that were never built (for example placeholders for external references skipped + // by SkipExternalRefResolution) have no node store; there is nothing to record for them. + return + } n.mu.Lock() defer n.mu.Unlock() if n.lines != nil { diff --git a/datamodel/low/node_map_test.go b/datamodel/low/node_map_test.go index 5d148cfe..49c2ec23 100644 --- a/datamodel/low/node_map_test.go +++ b/datamodel/low/node_map_test.go @@ -10,6 +10,30 @@ import ( "github.com/pb33f/testify/assert" ) +// Placeholders for skipped external references are never built, so their node store is nil. Writing +// to or reading from it must be a no-op, not a nil pointer dereference. +func TestNodeLines_NilReceiver(t *testing.T) { + var n *NodeLines + node := &yaml.Node{Line: 3} + + assert.NotPanics(t, func() { n.Store(3, node) }) + assert.NotPanics(t, func() { n.add(3, node) }) + + v, ok := n.Load(3) + assert.Nil(t, v) + assert.False(t, ok) + + called := false + n.Range(func(int, any) bool { called = true; return true }) + assert.False(t, called) + + nm := &NodeMap{} + assert.NotPanics(t, func() { nm.AddNode(3, node) }) + assert.False(t, nm.ContainsLine(3)) + assert.Equal(t, []int{0}, nm.GetNodeLines()) + assert.Len(t, nm.GetNodes()[0], 0) +} + func Test_NodeMapExtractNodes(t *testing.T) { yml := `one: hello two: there diff --git a/document_skip_external_refs_test.go b/document_skip_external_refs_test.go new file mode 100644 index 00000000..ee6228e6 --- /dev/null +++ b/document_skip_external_refs_test.go @@ -0,0 +1,145 @@ +// Copyright 2026 Dave Shanley / Quobix +// SPDX-License-Identifier: MIT + +package libopenapi + +import ( + "io" + "log/slog" + "testing" + + "github.com/pb33f/libopenapi/datamodel" + "github.com/pb33f/testify/assert" + "github.com/pb33f/testify/require" +) + +// With SkipExternalRefResolution enabled, an external $ref used as a map entry (a media type, header, +// link, example, encoding, callback or webhook) is replaced by a placeholder that is never built, so its +// node store is nil. The parent's Build then recorded the entry's key line on that nil store and +// panicked on a goroutine started by TranslateSliceParallel, which callers cannot recover. +func TestSkipExternalRefResolution_ExternalRefMapEntriesDoNotPanic(t *testing.T) { + const h30 = "openapi: 3.0.3\ninfo: {title: t, version: '1'}\n" + cases := map[string]string{ + "request body content": h30 + `paths: + /x: + post: + requestBody: + content: + application/json: {$ref: 'other.yaml#/mt'} + responses: {} +`, + "response content": h30 + `paths: + /x: + get: + responses: + '200': + description: ok + content: + application/json: {$ref: 'other.yaml#/mt'} +`, + "response headers": h30 + `paths: + /x: + get: + responses: + '200': + description: ok + headers: + X-A: {$ref: 'other.yaml#/h'} +`, + "response links": h30 + `paths: + /x: + get: + responses: + '200': + description: ok + links: + l: {$ref: 'other.yaml#/l'} +`, + "parameter content": h30 + `paths: + /x: + get: + parameters: + - name: a + in: query + content: + application/json: {$ref: 'other.yaml#/mt'} + responses: {} +`, + "parameter examples": h30 + `paths: + /x: + get: + parameters: + - name: a + in: query + schema: {type: string} + examples: + e: {$ref: 'other.yaml#/e'} + responses: {} +`, + "media type examples": h30 + `paths: + /x: + post: + requestBody: + content: + application/json: + examples: + e: {$ref: 'other.yaml#/e'} + responses: {} +`, + "media type encoding": h30 + `paths: + /x: + post: + requestBody: + content: + multipart/form-data: + encoding: + f: {$ref: 'other.yaml#/enc'} + responses: {} +`, + "encoding headers": h30 + `paths: + /x: + post: + requestBody: + content: + multipart/form-data: + encoding: + f: + headers: + X-A: {$ref: 'other.yaml#/h'} + responses: {} +`, + "operation callbacks": h30 + `paths: + /x: + get: + callbacks: + cb: {$ref: 'other.yaml#/cb'} + responses: {} +`, + "webhooks": "openapi: 3.1.0\ninfo: {title: t, version: '1'}\npaths: {}\n" + `webhooks: + w: {$ref: 'other.yaml#/pi'} +`, + "media type item encoding": "openapi: 3.2.0\ninfo: {title: t, version: '1'}\n" + `paths: + /x: + post: + requestBody: + content: + multipart/mixed: + itemEncoding: + f: {$ref: 'other.yaml#/enc'} + responses: {} +`, + } + for name, spec := range cases { + t.Run(name, func(t *testing.T) { + cfg := datamodel.NewDocumentConfiguration() + cfg.SkipExternalRefResolution = true + cfg.Logger = slog.New(slog.NewTextHandler(io.Discard, nil)) + + doc, err := NewDocumentWithConfiguration([]byte(spec), cfg) + require.NoError(t, err) + model, err := doc.BuildV3Model() + assert.NoError(t, err) + assert.NotNil(t, model) + }) + } +}