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

render.Render() writes to writer unexpected data of aliased type #56

Open
Archirk opened this issue May 24, 2024 · 0 comments
Open

render.Render() writes to writer unexpected data of aliased type #56

Archirk opened this issue May 24, 2024 · 0 comments

Comments

@Archirk
Copy link

Archirk commented May 24, 2024

Hello,
During my work I faced unexpected behaviour on calling render.Render() when I was implemented Renderer interface on struct, which occured to be alias. Honestly, I have no clue why it's happening.
But it appears that render calls implicitly some default renderer IN ADDITION to my implementation.
It seems like it should exit here but it does not.

package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"github.com/go-chi/render"
)

type (
	PublicProfile Profile
	Profile       struct {
		Name string
		Age  int
	}
	ProfileResponse struct {
		Name string `json:"name"`
	}
)

func (p PublicProfile) Render(w http.ResponseWriter, rq *http.Request) error {
	render.Status(rq, http.StatusNoContent)
	render.JSON(w, rq, ProfileResponse{Name: p.Name})
	return nil
}

func main() {
	w := httptest.NewRecorder()
	rq := httptest.NewRequest(http.MethodGet, "/", nil)
	someProfile := Profile{Name: "John", Age: 42}
	wrappedProfile := PublicProfile(someProfile)
	render.Render(w, rq, wrappedProfile)
	responseBody, _ := io.ReadAll(w.Body)
	fmt.Println(w.Code, string(responseBody))
}

Output:

204 {"name":"John"}
{"Name":"John","Age":42}

On go-playground

The only way around I have found is setting default responder to empty func. Is this expected way to it?

render.Respond = func(w http.ResponseWriter, rq *http.Request, v interface{}) {}
@Archirk Archirk changed the title render.Render() writes to writer origin data on alias type render.Render() writes to writer unexpected data of aliased type May 24, 2024
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