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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 [Bug]: agent.Struct fails to unmarshal response since 2.33.0 #2134

Closed
3 tasks done
teemukataja opened this issue Oct 3, 2022 · 4 comments 路 Fixed by #2137
Closed
3 tasks done

馃悰 [Bug]: agent.Struct fails to unmarshal response since 2.33.0 #2134

teemukataja opened this issue Oct 3, 2022 · 4 comments 路 Fixed by #2137

Comments

@teemukataja
Copy link

teemukataja commented Oct 3, 2022

Bug Description

  • agent.Struct() raises a null pointer exception.
  • agent.Struct() works in 2.32.0, but stopped working after 2.33.0, including the latest release 2.38.1.

How to Reproduce

Working version with 2.32.0

  1. set fiber version to 2.32.0 in go.mod
  2. run go mod tidy
  3. run script
  4. response is successfully parsed

Broken version 2.33.0-2.38.1

  1. set fiber version to anything after 2.33.0 in go.mod
  2. run go mod tidy
  3. run script
  4. null pointer exception is raised

Expected Behavior

JSON response is unmarshalled into struct.

Fiber Version

2.33.0-2.38.1

Code Snippet (optional)

package main

import (
	"fmt"

	"github.com/gofiber/fiber/v2"
)

type response struct {
	Title string `json:"title,omitempty"`
}

func main() {

	agent := fiber.AcquireAgent()
	defer agent.ConnectionClose()
	request := agent.Request()
	request.Header.SetMethod("GET")
	request.SetRequestURI("https://jsonplaceholder.typicode.com/todos/1")

	_ = agent.Parse()

	var r response
	_, _, _ = agent.Struct(&r)  // exception raised here

	fmt.Println(r)
}
$ go run cmd/main.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x668e38]

goroutine 1 [running]:
github.com/gofiber/fiber/v2.(*Agent).Struct(0xc0000fa000, {0x680720, 0xc000013180})
	/home/go/1.18.6/pkg/mod/github.com/gofiber/fiber/v2@v2.38.1/client.go:829 +0xf8
main.main()
	/home/go/1.18.6/src/request-test/cmd/main.go:24 +0x1c5

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.
@welcome
Copy link

welcome bot commented Oct 3, 2022

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@teemukataja
Copy link
Author

teemukataja commented Oct 3, 2022

I have a workaround solution by using agent.Bytes instead and unmarshalling manually:

var r response
_, body, _ := agent.Bytes()
_ = json.Unmarshal(body, &r)

@li-jin-gou
Copy link
Contributor

use a.JSONDecoder(json.Unmarshal).Struct(&r)

@ReneWerner87
Copy link
Member

@teemukataja thanks for the hint, fixed it in the pull request #2137

will be in the next release

until then you have to set the default decoder by hand
#2134 (comment)

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

Successfully merging a pull request may close this issue.

3 participants