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

Assignment to entry in nil map with @Header all #1082

Closed
microphoneabuser opened this issue Dec 20, 2021 · 5 comments
Closed

Assignment to entry in nil map with @Header all #1082

microphoneabuser opened this issue Dec 20, 2021 · 5 comments

Comments

@microphoneabuser
Copy link

Describe the bug
When using @Header all instead of explicitly writing response codes, panic occurs.

Code used:

// @Header all {string} X-Request-ID "Some description"

Result:

panic: assignment to entry in nil map
                                     
goroutine 1 running:

github.com/swaggo/swag.(*Operation).ParseResponseHeaderComment(0xc00030e120, {0xc0001ca00b, 0x84}, 0x91b134)
     /go/pkg/mod/github.com/swaggo/swag@v1.7.6/operation.go:897 +0x789         
github.com/swaggo/swag.(*Operation).ParseComment(0xc00030e120, {0xc0001ca000, 0x1}, 0x49d9fa)
     /go/pkg/mod/github.com/swaggo/swag@v1.7.6/operation.go:120 +0x287                         
github.com/swaggo/swag.(*Parser).ParseRouterAPIInfo(0xc0001b35f0, {0xc00052be00, 0x2d}, 0xc00002d500)
     /go/pkg/mod/github.com/swaggo/swag@v1.7.6/parser.go:695 +0x1b9
github.com/swaggo/swag.(*PackagesDefinitions).RangeFiles(0xc00000e960, 0xc00092f918)                
     /go/pkg/mod/github.com/swaggo/swag@v1.7.6/packages.go:88 +0x1ff             
github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0xc0001b35f0, {0xc0001add20, 0x1, 0xc000072100}, {0x7ffe2ace2eea, 0x12}, 0x64)
     /go/pkg/mod/github.com/swaggo/swag@v1.7.6/parser.go:288 +0x325
github.com/swaggo/swag/gen.(*Gen).Build(0xc0001adc50, 0xc00092fc30)                                 
     /go/pkg/mod/github.com/swaggo/swag@v1.7.6/gen/gen.go:106 +0x2b0                              
main.initAction(0xc0001e21a0)                                                                       
     /go/pkg/mod/github.com/swaggo/swag@v1.7.6/cmd/swag/main.go:110 +0x39a
github.com/urfave/cli/v2.(*Command).Run(0xc0001937a0, 0xc0001d8380)
     /go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x64a
github.com/urfave/cli/v2.(*App).RunContext(0xc0001e2000, {0x9b56f0, 0xc000026038}, {0xc0000201e0, 0x6, 0x6})
     /go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0x81e
github.com/urfave/cli/v2.(*App).Run(...)
     /go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224
main.main()
     /go/pkg/mod/github.com/swaggo/swag@v1.7.6/cmd/swag/main.go:174 +0x55d

But if i use: @Header 200,400,404,500 {string} X-Request-ID "Some description" it works fine.

The problem is in operation.go (*Operation).ParseResponseHeaderComment:

...
if strings.EqualFold(matches[1], "all") {
		if operation.Responses.Default != nil {
			operation.Responses.Default.Headers[headerKey] = header
		}

		if operation.Responses.StatusCodeResponses != nil {
			for code, response := range operation.Responses.StatusCodeResponses {
				response.Headers[headerKey] = header
				operation.Responses.StatusCodeResponses[code] = response
			}
		}

		return nil
	}
...

To fix it, we just need to initialize response.Headers before response.Headers[headerKey] = header :

...
if strings.EqualFold(matches[1], "all") {
		if operation.Responses.Default != nil {
			operation.Responses.Default.Headers[headerKey] = header
		}

		if operation.Responses.StatusCodeResponses != nil {
			for code, response := range operation.Responses.StatusCodeResponses {
				response.Headers = make(map[string]spec.Header)
				response.Headers[headerKey] = header
				operation.Responses.StatusCodeResponses[code] = response
			}
		}

		return nil
	}
...

To Reproduce
Use @Header all

@ubogdan
Copy link
Contributor

ubogdan commented Dec 20, 2021

This should be fixed in here #1066, and it will go with the next release (a few days far away).

Please build swag using the latest version from master and let us know.

@ubogdan
Copy link
Contributor

ubogdan commented Dec 21, 2021

@microphoneabuser any updates here?

@microphoneabuser
Copy link
Author

@ubogdan No, panic still occurs.

@ubogdan
Copy link
Contributor

ubogdan commented Dec 24, 2021

@microphoneabuser this has been fixed and released with v1.7.7.
Please retest and let us know.

@microphoneabuser
Copy link
Author

@ubogdan It's working fine now, thank you! :)

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