Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Panic on circular reference @v0.21.5 #380

Open
1000p opened this issue Feb 19, 2025 · 2 comments
Open

Panic on circular reference @v0.21.5 #380

1000p opened this issue Feb 19, 2025 · 2 comments
Labels
bug Something isn't working need better example needs more information, explanation or a better example.

Comments

@1000p
Copy link

1000p commented Feb 19, 2025

Bug: The helper function (GetCircularReferenceResult) returns nil for this self-referential case (schema below), and the subsequent unguarded call to .GenerateJourneyPath() on nil leads to a panic (invalid memory address dereference).

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x102f086c0]

goroutine 93 [running]:
github.com/pb33f/libopenapi/index.(*CircularReferenceResult).GenerateJourneyPath(0x0)
	/Users/mileniliev/go/pkg/mod/github.com/pb33f/libopenapi@v0.21.5/index/circular_reference_result.go:24 +0x30
github.com/pb33f/libopenapi/datamodel/low.LocateRefNodeWithContext({0x103d5de50, 0x140003c2d50}, 0x1400019e780, 0x140002b2008)
	/Users/mileniliev/go/pkg/mod/github.com/pb33f/libopenapi@v0.21.5/datamodel/low/extraction_functions.go:100 +0x3e0
github.com/pb33f/libopenapi/datamodel/low/base.buildSchema({0x103d5de50, 0x140003c2d50}, 0x14000328bd0, 0x1400019e6e0, 0x1400019e780, 0x14000328a10, 0x140002b2008)
	/Users/mileniliev/go/pkg/mod/github.com/pb33f/libopenapi@v0.21.5/datamodel/low/base/schema.go:1201 +0x13c
created by github.com/pb33f/libopenapi/datamodel/low/base.(*Schema).Build in goroutine 280
	/Users/mileniliev/go/pkg/mod/github.com/pb33f/libopenapi@v0.21.5/datamodel/low/base/schema.go:891 +0x4274
Reproducible schema
openapi: "3.0.3"
info:
  title: "Circular Reference Panic Example"
  version: "1.0.0"
paths:
  /test:
    get:
      summary: "Returns an object with an array of actions"
      responses:
        "200":
          description: "Successful response with actions"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ActionList"
components:
  schemas:
    # ActionList is a valid object schema that contains an array property.
    # Each element in the array should conform to the ActionComponent schema.
    ActionList:
      type: object
      properties:
        actions:
          type: array
          items:
            $ref: "#/components/schemas/ActionComponent"

    # ActionComponent is defined as a self-referencing schema.
    # This intentional circular reference is used to reproduce the bug.
    ActionComponent:
      $ref: "#/components/schemas/ActionComponent"
@daveshanley daveshanley added the bug Something isn't working label Feb 22, 2025
@daveshanley
Copy link
Member

Oh dear, willfix.

@daveshanley
Copy link
Member

daveshanley commented Feb 22, 2025

Can you give me a snippet of the code you are using to create this because I built the following lower level test and it works just fine. I can see you're creating a model - to save me digging around trying to discover which schema you're building, a snippet of how you're calling Schema() would be very helpful

func TestSpecIndex_CheckCircularRefIssue380(t *testing.T) {

	d := `openapi: "3.0.3"
info:
  title: "Circular Reference Panic Example"
  version: "1.0.0"
paths:
  /test:
    get:
      summary: "Returns an object with an array of actions"
      responses:
        "200":
          description: "Successful response with actions"
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ActionList"
components:
  schemas:
    # ActionList is a valid object schema that contains an array property.
    # Each element in the array should conform to the ActionComponent schema.
    ActionList:
      type: object
      properties:
        actions:
          type: array
          items:
            $ref: "#/components/schemas/ActionComponent"

    # ActionComponent is defined as a self-referencing schema.
    # This intentional circular reference is used to reproduce the bug.
    ActionComponent:
      $ref: "#/components/schemas/ActionComponent"
`

	var rootNode yaml.Node
	_ = yaml.Unmarshal([]byte(d), &rootNode)

	config := CreateClosedAPIIndexConfig()
	idx := NewSpecIndexWithConfig(&rootNode, config)

	resolver := NewResolver(idx)
	assert.NotNil(t, resolver)

	circ := resolver.Resolve()
	assert.Len(t, circ, 1)
	assert.True(t, circ[0].CircularReference.IsInfiniteLoop)
	assert.Equal(t, "infinite circular reference detected: #/components/schemas/ActionComponent", circ[0].ErrorRef.Error())
	assert.Equal(t, "ActionList -> ActionComponent -> ActionComponent", circ[0].CircularReference.GenerateJourneyPath())

}

@daveshanley daveshanley added the need better example needs more information, explanation or a better example. label Feb 22, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working need better example needs more information, explanation or a better example.
Projects
None yet
Development

No branches or pull requests

2 participants