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

encoding/json: custom unmarshal not called on empty value with ,string #50997

Open
aronatkins opened this issue Feb 3, 2022 · 0 comments
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@aronatkins
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.17.6 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/aron/Library/Caches/go-build"
GOENV="/Users/aron/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/aron/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/aron/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8j/pt4fjhkj11d3xd9344pdspmh0000gn/T/go-build3937592509=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

We have incoming JSON that receives a Boolean value as a quoted number. We have a custom UnmarshalJSON method that helps parse different types of Boolean values. When that incoming value comes in with an unexpected, empty string value, the unmarshal function is never called; the decoder decides that this situation is an error without giving the target type an opportunity to handle the value.

Perhaps this is an unexpected, unsupported situation? The ,string documentation indicates that it can be used for Boolean types, but we have added a custom UnmarshalJSON to the mix.

This same type has caused us struggles in the past; see #20651

package main

import (
	"encoding/json"
	"fmt"
	"strconv"
)

type parsedBool bool

func (p *parsedBool) UnmarshalJSON(data []byte) error {
	v, err := strconv.ParseBool(string(data))
	if err != nil {
		return fmt.Errorf("custom error: %w", err)
	}
	*p = parsedBool(v)
	return nil
}

type Target struct {
	Enabled parsedBool `json:"enabled,string"`
}

func main() {
	var err error
        empty := `{"enabled":""}`
        truth := `{"enabled":"t"}`
        unexpected := `{"enabled":"unexpected"}`

	target := Target{}
	err = json.Unmarshal([]byte(empty), &target)
	fmt.Printf("target: %#v; error: %s\n", target, err)
	err = json.Unmarshal([]byte(unexpected), &target)
	fmt.Printf("target: %#v; error: %s\n", target, err)
	err = json.Unmarshal([]byte(truth), &target)
	fmt.Printf("target: %#v; error: %s\n", target, err)
	
}

https://play.golang.com/p/bFAQ4Xu6mar

What did you expect to see?

Expected to see parsedBool#UnmarshalJSON attempt parse the incoming value, producing something like:

target: main.Target{Enabled:false}; error: custom error: strconv.ParseBool: parsing "": invalid syntax
target: main.Target{Enabled:false}; error: custom error: strconv.ParseBool: parsing "unexpected": invalid syntax
target: main.Target{Enabled:true}; error: %!s(<nil>)

What did you see instead?

An error about an invalid ,string struct tag, which prevents the call to UnmarshalJSON.

target: main.Target{Enabled:false}; error: json: invalid use of ,string struct tag, trying to unmarshal "" into main.parsedBool
target: main.Target{Enabled:false}; error: custom error: strconv.ParseBool: parsing "unexpected": invalid syntax
target: main.Target{Enabled:true}; error: %!s(<nil>)
@toothrot toothrot added this to the Backlog milestone Feb 4, 2022
@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants