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

オープン
#56 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

@team-humaki がすでに取り組んでいます。

2026年9月16日 から。

  • #66 @team-humaki による — オープン

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
go
領域
api, backend

調査の方向性

リンク先の69行目にある render.go から始め、提供されているエイリアス型と httptest の例を使って問題を再現します。render.Render がカスタム Render メソッドとデフォルトの responder をどのように処理するかを追跡し、完了した動作によって意図した response body と status だけが生成され、重複した出力がないことを確認します。

索引モデルが issue の本文から書いたものです。

説明

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{}) {}
主要言語
Go
スター
340
フォーク
69
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

go-chi/render のほかの issue

go-chi/render の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。