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

JSONPb marshaler doesn't use indent on non proto fields #4026

Closed
gknw opened this issue Feb 22, 2024 · 0 comments · Fixed by #4027
Closed

JSONPb marshaler doesn't use indent on non proto fields #4026

gknw opened this issue Feb 22, 2024 · 0 comments · Fixed by #4027

Comments

@gknw
Copy link
Contributor

gknw commented Feb 22, 2024

🐛 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

@johanbrandhorst johanbrandhorst changed the title JSONPb marshaler doesn't use ident on non proto fields JSONPb marshaler doesn't use indent on non proto fields Feb 22, 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

Successfully merging a pull request may close this issue.

1 participant