swaggest / openapi-go

OpenAPI structures for Go
https://pkg.go.dev/github.com/swaggest/openapi-go/openapi3
MIT License
223 stars 18 forks source link

Issue with self-referencing entites #89

Closed Fifi31 closed 6 months ago

Fifi31 commented 7 months ago

Describe the bug When a sub-entity of a request has a field of its own type, the resulting schema doesn't reference itself. It works well if the request (the top-level type) references itself, but not for sub-entities.

To Reproduce Playground

type SubEntity struct {
    Self *SubEntity `json:"self"`
}
type Req struct {
    SubEntity *SubEntity `json:"subentity"`
    Self      *Req       `json:"self"`
}

func main() {
    reflector := openapi3.Reflector{}
    reflector.Spec = &openapi3.Spec{Openapi: "3.1.0"}

    putOp := openapi3.Operation{}

    reflector.SetRequest(&putOp, Req{}, http.MethodPut)
    reflector.Spec.AddOperation(http.MethodPut, "/things/{id}", putOp)

    schema, err := reflector.Spec.MarshalYAML()
    if err != nil {
        log.Fatal(err)
    }

    // Req schema references itself on self field, but SubEntity schema has no reference on self field
    fmt.Println(string(schema))
}

Expected behavior components.schemas.SubEntity.properties.self should have a $ref field with the value '#/components/schemas/SubEntity'.

Also SubEntity.self has nullable set as true but I am not sure if it is normal, as Req.self has not.

vearutop commented 6 months ago

Now should be fixed in master, thanks for reporting this!