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: panic: recursively parsing struct #1662

Closed
jeffwalter-rum opened this issue Aug 28, 2023 · 8 comments
Closed

swag init: panic: recursively parsing struct #1662

jeffwalter-rum opened this issue Aug 28, 2023 · 8 comments

Comments

@jeffwalter-rum
Copy link

jeffwalter-rum commented Aug 28, 2023

Describe the bug
I have the following data strutures:

import "github.com/my/local/openrtb"

type Response struct {
	RTB        *openrtb.BidResponse
}

and the following package (github.com/my/local/openrtb):

package openrtb

import "github.com/bsm/openrtb"

type BidResponse = openrtb.BidResponse

I get the following panic:

2023/08/28 12:41:11 Generating openrtb.BidResponse
2023/08/28 12:41:11 Skipping 'openrtb.BidResponse', recursion detected.
2023/08/28 12:41:11 Error parsing type definition 'openrtb.BidResponse': recursively parsing struct
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x100c769fc]

goroutine 1 [running]:
github.com/swaggo/swag.(*Parser).getRefTypeSchema(0x140000afd40, 0x14000c3e0a0, 0x0)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1073 +0x7c
github.com/swaggo/swag.(*Parser).getTypeSchema(0x140000afd40, {0x14000c77380, 0x13}, 0xd?, 0x1)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1052 +0xac8
github.com/swaggo/swag.(*Parser).parseStructField(0x140000afd40, 0x2d?, 0x1400067b5c0)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1373 +0x33c
github.com/swaggo/swag.(*Parser).parseStruct(0x14000814160?, 0x10?, 0x1400035bf20)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1286 +0xac
github.com/swaggo/swag.(*Parser).parseTypeExpr(0x14000436750?, 0x100c9fcf4?, {0x100e636d8?, 0x14000671218?}, 0x88?)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1239 +0x70
github.com/swaggo/swag.(*Parser).ParseDefinition(0x140000afd40, 0x14000c3fa40)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1124 +0x394
github.com/swaggo/swag.(*Parser).getTypeSchema(0x140000afd40, {0x14000436730, 0xd}, 0x10092cdc4?, 0x1)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1049 +0xa14
github.com/swaggo/swag.(*Parser).parseStructField(0x140000afd40, 0x1400088e510?, 0x140005d8240)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1373 +0x33c
github.com/swaggo/swag.(*Parser).parseStruct(0x14000b02260?, 0x10?, 0x140005c9590)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1286 +0xac
github.com/swaggo/swag.(*Parser).parseTypeExpr(0x14000c76228?, 0x100c9fcf4?, {0x100e636d8?, 0x140005da0a8?}, 0xc0?)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1239 +0x70
github.com/swaggo/swag.(*Parser).ParseDefinition(0x140000afd40, 0x14000afa140)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1124 +0x394
github.com/swaggo/swag.(*Parser).getTypeSchema(0x140000afd40, {0x140003ee25e, 0x12}, 0x1008578ec?, 0x1)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:1049 +0xa14
github.com/swaggo/swag.parseObjectSchema(0x140000afd40, {0x140003ee25e, 0x12}, 0x2800000000000000?)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/operation.go:859 +0xde8
github.com/swaggo/swag.(*Operation).parseObjectSchema(...)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/operation.go:813
github.com/swaggo/swag.(*Operation).parseAPIObjectSchema(0x14000ceb200, {0x140003ee24e, 0x22}, {0x140003ee253, 0x6}, {0x140003ee25e, 0x12}, 0x0?)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/operation.go:944 +0x38c
github.com/swaggo/swag.(*Operation).ParseResponseComment(0x14000ceb200, {0x140003ee24e, 0x22}, 0x2?)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/operation.go:976 +0x114
github.com/swaggo/swag.(*Operation).ParseComment(0x14000ceb200, {0x140003ee240?, 0x1?}, 0x14000ad6f60?)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/operation.go:143 +0x38c
github.com/swaggo/swag.(*Parser).ParseRouterAPIInfo(0x140000afd40, 0x14000400600)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:917 +0x19c
github.com/swaggo/swag.(*PackagesDefinitions).RangeFiles(0x140001db560, 0x14000d07768)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/packages.go:107 +0x27c
github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0x140000afd40, {0x140000edb40, 0x2, 0x0?}, {0x16f5b79b3, 0x15}, 0x64)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/parser.go:415 +0x4d4
github.com/swaggo/swag/gen.(*Gen).Build(0x140001db380, 0x140001e8dc0)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/gen/gen.go:207 +0x650
main.initAction(0x140001fe000?)
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/cmd/swag/main.go:201 +0x840
github.com/urfave/cli/v2.(*Command).Run(0x140000eec60, 0x140000e3b80)
        /Users/user/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x4a8
github.com/urfave/cli/v2.(*App).RunContext(0x140001e41a0, {0x100e646f8?, 0x1011b5f60}, {0x140000bc120, 0x6, 0x6})
        /Users/user/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0x808
github.com/urfave/cli/v2.(*App).Run(...)
        /Users/user/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224
main.main()
        /Users/user/go/pkg/mod/github.com/swaggo/swag@v1.16.1/cmd/swag/main.go:277 +0x540

To Reproduce
Steps to reproduce the behavior:

  1. Run swag init

Expected behavior
A valid swagger file should be generated without a panic.

Your swag version
swag version v1.8.12

Your go version
go version go1.21.0 darwin/arm64

@jeffwalter-rum
Copy link
Author

As far as I can tell, there's no recursion in the openrtb.BidResponse struct, so it appears the parser thinks the local package and the imported package of the same name, is some sort of recursion. I did try aliasing the imported openrtb package in the local openrtb package. Also tried renaming the local openrtb pacakge and tried aliasing both packages throughout the code. None of that helped.

@jeffwalter-rum
Copy link
Author

Anybody? Would really appreciate a little help with this.

sdghchj added a commit that referenced this issue Sep 6, 2023
Signed-off-by: sdghchj <sdghchj@qq.com>
@jeffwalter-rum
Copy link
Author

@sdghchj just tested with your commit and my problem is resolved! Thank you!

@jeffwalter-rum
Copy link
Author

@sdghchj actually, think I spoke too soon. Now I'm getting this and no docs are being generated:

2023/09/06 10:27:11 Generating openrtb.BidResponse
2023/09/06 10:27:11 Skipping 'openrtb.BidResponse', recursion detected.
2023/09/06 10:27:11 Error parsing type definition 'openrtb.BidResponse': openrtb.BidResponse: recursively parsing struct
2023/09/06 10:27:11 Error parsing type definition 'ssps.Response': rtb: openrtb.BidResponse: openrtb.BidResponse: recursively parsing struct
2023/09/06 10:27:11 Error parsing type definition 'responses.Response': ssp: ssps.Response: rtb: openrtb.BidResponse: openrtb.BidResponse: recursively parsing struct
2023/09/06 10:27:11 ParseComment error in file /Users/user/go/repos/ads/pkg/http.v2/routes/v0/serving/display.go :responses.Response: ssp: ssps.Response: rtb: openrtb.BidResponse: openrtb.BidResponse: recursively parsing struct

@sdghchj
Copy link
Member

sdghchj commented Sep 7, 2023

Can you please provide your simplified project?

@jeffwalter-rum
Copy link
Author

@sdghchj I just sent you an invite to a private repo that demonstrates the issue: https://github.com/jeffwalter-rum/swag-bug

To reproduce:

user@Users-MacBook-Pro repos % git clone https://github.com/jeffwalter-rum/swag-bug.git
Cloning into 'swag-bug'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 7 (delta 1), reused 7 (delta 1), pack-reused 0
Receiving objects: 100% (7/7), 9.75 KiB | 1.22 MiB/s, done.
Resolving deltas: 100% (1/1), done.
user@Users-MacBook-Pro repos % cd swag-bug 
user@Users-MacBook-Pro swag-bug %  swag i --dir=.                                         
2023/09/07 14:39:55 Generate swagger docs....
2023/09/07 14:39:55 Generate general API Info, search dir:.
2023/09/07 14:39:55 Generating main.Response
2023/09/07 14:39:55 Generating openrtb.BidResponse
2023/09/07 14:39:55 Skipping 'openrtb.BidResponse', recursion detected.
2023/09/07 14:39:55 Error parsing type definition 'openrtb.BidResponse': openrtb.BidResponse: recursively parsing struct
2023/09/07 14:39:55 Error parsing type definition 'main.Response': rtb: openrtb.BidResponse: openrtb.BidResponse: recursively parsing struct
2023/09/07 14:39:55 ParseComment error in file /Users/user/go/repos/swag-bug/main.go :Response: rtb: openrtb.BidResponse: openrtb.BidResponse: recursively parsing struct

@sdghchj
Copy link
Member

sdghchj commented Sep 8, 2023

I have tested on your repo, it'ok actually.

swag init -pd
2023/09/08 09:39:01 Generate swagger docs....
2023/09/08 09:39:01 Generate general API Info, search dir:./
2023/09/08 09:39:03 Generating main.Response
2023/09/08 09:39:03 Generating github_com_jeffwalter-rum_swag-bug_openrtb.BidResponse
2023/09/08 09:39:03 Generating github_com_bsm_openrtb.BidResponse
2023/09/08 09:39:03 Generating openrtb.SeatBid
2023/09/08 09:39:03 Generating openrtb.Bid
2023/09/08 09:39:03 Generating openrtb.Extension
2023/09/08 09:39:03 create docs.go at docs/docs.go
2023/09/08 09:39:03 create swagger.json at docs/swagger.json
2023/09/08 09:39:03 create swagger.yaml at docs/swagger.yaml

Check whether you have updated your swag to my latest commit, please.

@jeffwalter-rum
Copy link
Author

Ok, it works with the -pd flag. Might be good to provide a detailed example for the -pd flag in the README. I don't see it specifically called out there and would never have guessed that was the fix. Thanks for your help!

sdghchj added a commit that referenced this issue Nov 24, 2023
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

2 participants