Skip to content

Panic when rendering a nullable example #301

Closed

Description

@ThomasRooney

Given a specification (e.g. stored under test_specs/nullable-examples.openapi.yaml:

openapi: 3.1.0
components:
  schemas:
    Thing:
      type: object
      description: A nullable example.
      properties:
        target:
          nullable: true
          type: string
          enum:
            - staging
            - production
          example:

Attempting to render this specification causes a panic.

func TestDocument_Nullable_Example(t *testing.T) {
	data, _ := os.ReadFile("../../../test_specs/nullable-examples.openapi.yaml")
	info, _ := datamodel.ExtractSpecInfo(data)

	lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())

	highDoc := NewDocument(lowDoc)
	rendered := highDoc.RenderWithIndention(2)

	if runtime.GOOS != "windows" {
		assert.Equal(t, string(data), strings.TrimSpace(string(rendered)))
	}

	rendered = highDoc.RenderWithIndention(4)
	if runtime.GOOS != "windows" {
		assert.NotEqual(t, string(data), strings.TrimSpace(string(rendered)))
	}
}
=== RUN   TestDocument_Nullable_Example
--- FAIL: TestDocument_Nullable_Example (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x10439b798]

goroutine 35 [running]:
testing.tRunner.func1.2({0x1048a6580, 0x104d2a520})
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1631 +0x3c0
testing.tRunner.func1()
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1634 +0x57c
panic({0x1048a6580?, 0x104d2a520?})
	/opt/homebrew/opt/go/libexec/src/runtime/panic.go:770 +0xf0
gopkg.in/yaml%2ev3.handleErr(0x1400028fc38)
	/Users/thomasrooney/go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/yaml.go:294 +0x120
panic({0x1048a6580?, 0x104d2a520?})
	/opt/homebrew/opt/go/libexec/src/runtime/panic.go:770 +0xf0
gopkg.in/yaml%2ev3.handleErr(0x1400028a228)
	/Users/thomasrooney/go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/yaml.go:294 +0x120
panic({0x1048a6580?, 0x104d2a520?})
	/opt/homebrew/opt/go/libexec/src/runtime/panic.go:770 +0xf0
gopkg.in/yaml%2ev3.(*Node).Encode(0x14000219220, {0x104905d60, 0x140001be780})
	/Users/thomasrooney/go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/yaml.go:269 +0x208
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).AddYAMLNode(0x14000012320, 0x14000218a00, 0x140002009c0)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:505 +0x1124
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).Render(0x14000012320)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:256 +0x2f0
github.com/pb33f/libopenapi/datamodel/high/base.(*SchemaProxy).MarshalYAML(0x140002081c0)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/base/schema_proxy.go:191 +0x190
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).AddYAMLNode(0x140000121e0, 0x140002188c0, 0x14000200840)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:449 +0xacc
github.com/pb33f/libopenapi/orderedmap.(*Map[...]).ToYamlNode(0x1049b88c0, {0x1049a87a0, 0x140000121e0}, {0x104969ce0, 0x14000208200})
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/orderedmap/builder.go:78 +0x70c
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).AddYAMLNode(0x140000121e0, 0x14000218500, 0x14000200780)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:427 +0x668
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).Render(0x140000121e0)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:256 +0x2f0
github.com/pb33f/libopenapi/datamodel/high/base.(*SchemaProxy).MarshalYAML(0x14000208080)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/base/schema_proxy.go:191 +0x190
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).AddYAMLNode(0x140000120f0, 0x140002183c0, 0x14000200660)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:449 +0xacc
github.com/pb33f/libopenapi/orderedmap.(*Map[...]).ToYamlNode(0x1049b88c0, {0x1049a87a0, 0x140000120f0}, {0x104969ce0, 0x14000208100})
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/orderedmap/builder.go:78 +0x70c
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).AddYAMLNode(0x140000120f0, 0x14000218280, 0x14000200600)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:427 +0x668
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).Render(0x140000120f0)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:256 +0x2f0
github.com/pb33f/libopenapi/datamodel/high/v3.(*Components).MarshalYAML(0x140000ae060)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/v3/components.go:158 +0x58
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).AddYAMLNode(0x14000012050, 0x14000218000, 0x140002005a0)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:449 +0xacc
github.com/pb33f/libopenapi/datamodel/high.(*NodeBuilder).Render(0x14000012050)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/node_builder.go:256 +0x2f0
github.com/pb33f/libopenapi/datamodel/high/v3.(*Document).MarshalYAML(0x140000da0b0)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/v3/document.go:189 +0x58
gopkg.in/yaml%2ev3.(*encoder).marshal(0x1400020c008, {0x0, 0x0}, {0x104905820, 0x140000da0b0, 0x16})
	/Users/thomasrooney/go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/encode.go:140 +0x5c8
gopkg.in/yaml%2ev3.(*encoder).marshalDoc(0x1400020c008, {0x0, 0x0}, {0x104905820, 0x140000da0b0, 0x16})
	/Users/thomasrooney/go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/encode.go:105 +0x174
gopkg.in/yaml%2ev3.(*Encoder).Encode(0x14000064010, {0x104905820, 0x140000da0b0})
	/Users/thomasrooney/go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/yaml.go:251 +0xdc
github.com/pb33f/libopenapi/datamodel/high/v3.(*Document).RenderWithIndention(0x140000da0b0, 0x2)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/v3/document.go:169 +0xa8
github.com/pb33f/libopenapi/datamodel/high/v3.TestDocument_Nullable_Example(0x1400012a9c0)
	/Users/thomasrooney/Code/speakeasy-api/libopenapi/datamodel/high/v3/document_test.go:645 +0x160
testing.tRunner(0x1400012a9c0, 0x1049a3350)
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1689 +0x1a8
created by testing.(*T).Run in goroutine 1
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1742 +0x668```

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions