Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Swag init fails for recursive cyclic references involving embedded structs #1939

Open
takanuva15 opened this issue Dec 9, 2024 · 0 comments

Comments

@takanuva15
Copy link

Describe the bug
When there are 2 "child" structs that embed the same struct S1, and S1 also has a pointer to one of the child structs, swag init is unable to parse it and errors out.

To Reproduce
Steps to reproduce the behavior:

  1. Create a new go file with the following MVE using the gin framework:

main.go

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type Parent struct {
	Child2 *Child2
}

type Child1 struct {
	Parent
	Prop1 string
}
type Child2 struct {
	Parent
	Prop2 string
}

func main() {
	r := gin.Default()
	r.GET("/ping", Get)
	_ = r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

// Get
// @Success      200  {object}  Child1
// @Router       /ping [get]
func Get(c *gin.Context) {
	c2 := Child2{Prop2: "c2"}
	c1 := Child1{Prop1: "c1", Parent: Parent{Child2: &c2}}
	c.JSON(http.StatusOK, c1)
}

The go.mod file can be generated as:

go mod init dummy
go get github.com/gin-gonic/gin
  1. Run swag init
  2. Note that the following error gets printed:
swag init
2024/12/09 16:58:59 Generate swagger docs....
2024/12/09 16:58:59 Generate general API Info, search dir:./
2024/12/09 16:59:00 Generating main.Child1
2024/12/09 16:59:00 Generating main.Parent
2024/12/09 16:59:00 Generating main.Child2
2024/12/09 16:59:00 Skipping 'main.Parent', recursion detected.
2024/12/09 16:59:00 Error parsing type definition 'main.Child2': Parent: recursively parsing struct
2024/12/09 16:59:00 Error parsing type definition 'main.Parent': [child2]: Child2: Parent: recursively parsing struct
2024/12/09 16:59:00 Error parsing type definition 'main.Child1': Parent: [child2]: Child2: Parent: recursively parsing struct
2024/12/09 16:59:00 ParseComment error in file /Users/takanuva15/Documents/Repositories/dummy/main.go :Child1: Parent: [child2]: Child2: Parent: recursively parsing struct
  1. Now run the actual gin server via go run main.go
  2. curl 'http://localhost:8080/ping'
  3. Note that the output works perfectly fine:
{
    "Child2": {
        "Child2": null,
        "Prop2": "c2"
    },
    "Prop1": "c1"
}

Expected behavior
The swag init should generate docs normally using definition references as it already does for normal cyclic references

Screenshots
N/A

Your swag version
1.16.4

Your go version
1.23.3

Desktop (please complete the following information):

  • OS: macOS
  • Browser: Chrome 131.0.6778.109
  • Version: Sonoma 14.7.1

Additional context
This example has been boiled down for simplicity, but we do have places in the actual Golang model we use in our company where 2 child structs embed the same parent struct, yet the parent struct has a pointer to one of the child structs as well. (It's not great, but we cannot get rid of it since it would break our existing production environment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant