grpc-ecosystem / grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec

Home Page:https://grpc-ecosystem.github.io/grpc-gateway/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSONPb marshaler doesn't use indent on non proto fields

gknw opened this issue Β· comments

commented

πŸ› Bug Report

The JSONPb marshaler does not use ident on non proto fields. Here the value of JSONPb.MarshalOptions.Ident is checked once, but not checked below

To Reproduce

package main

import (
	"fmt"

	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
	"google.golang.org/protobuf/encoding/protojson"
)

type S struct {
	A string
	B int64
	C map[string]int
}

func main() {
	m := &runtime.JSONPb{
		MarshalOptions: protojson.MarshalOptions{
			Indent: "    ",
		},
	}

	var err error
	var b []byte

	b, err = m.Marshal(&S{"GG", 3, map[string]int{"FOO": 0, "BAR": -1}})
	if err != nil {
		panic(err.Error)
	}
	fmt.Println(string(b))

	b, err = m.Marshal(map[string]int{"FOO": 1, "BAR": 2})
	if err != nil {
		panic(err.Error)
	}
	fmt.Println(string(b))
}

Expected behavior

{
    "A": "GG",
    "B": 3,
    "C": {
        "BAR": -1,
        "FOO": 0
    }
}
{
    "BAR": 2,
    "FOO": 1
}

Actual Behavior

{"A":"GG","B":3,"C":{"BAR":-1,"FOO":0}}
{
    "BAR": 2,
    "FOO": 1
}

Your Environment

go version go1.21.6 linux/amd64