pb33f / libopenapi

libopenapi is a fully featured, high performance OpenAPI 3.1, 3.0 and Swagger parser, library, validator and toolkit for golang applications.
https://pb33f.io/libopenapi/
Other
362 stars 46 forks source link

Circular reference does not return an error #286

Closed oliviernguyenquoc closed 3 weeks ago

oliviernguyenquoc commented 4 weeks ago

When I use this YAML:

openapi: 3.1.0
info:
  title: Circular References Test
  version: 1.0.0
components:
  schemas:
    Node:
      type: object
      properties:
        next:
          $ref: '#/components/schemas/Node'
package main

import (
    "fmt"
    "os"

    "github.com/pb33f/libopenapi"
)

func main() {
    openAPISpec, err := os.ReadFile("circular_references.yaml")
    if err != nil {
        fmt.Printf("Failed to read OpenAPI spec: %v\n", err)
        os.Exit(1)
    }

    document, err := libopenapi.NewDocument(openAPISpec)
    if err != nil {
        panic(fmt.Sprintf("cannot create new document: %e", err))
    }

    // because we know this is a v3 spec, we can build a ready to go model from it.
    _, errors := document.BuildV3Model()
    if len(errors) > 0 {
        for i := range errors {
            fmt.Printf("error: %e\n", errors[i])
        }
        panic(fmt.Sprintf("cannot create v3 model from document: %d errors reported",
            len(errors)))
    }

    fmt.Println("Successfully parsed OpenAPI document with circular references")
}

Output: "Successfully parsed OpenAPI document with circular references"

I should have an error

khart-twilio commented 3 weeks ago

Have you read https://pb33f.io/libopenapi/circular-references/ ?

Maybe I'm missing something in your intent, sorry if I am, but the benefit of this library is that it doesn't error out when circular references are present (which can be a valid use-case).

So it is working as expected, right?

daveshanley commented 3 weeks ago

Have you read https://pb33f.io/libopenapi/circular-references/ ?

Maybe I'm missing something in your intent, sorry if I am, but the benefit of this library is that it doesn't error out when circular references are present (which can be a valid use-case).

So it is working as expected, right?

This is correct. Circular reference management and detection is a feature of the library, it's not a showstopper, which is why there are configuration options available when building documents.

The docs here: https://pb33f.io/libopenapi/circular-references/ explain this all in detail.

Thanks.

oliviernguyenquoc commented 2 weeks ago

Ok I missed this phrase: "A circular reference isn’t considered something that’s broken, unless required is set to true"