From 77fe42535e5307a2682c79917a974ecb150bed35 Mon Sep 17 00:00:00 2001 From: Yuval Goldberg Date: Fri, 1 Oct 2021 15:51:00 +0300 Subject: [PATCH] Support marshalling a null URI Currently in case trying to marshal a null URI struct, it tries to call `u.String()` function and panics. The marshal behavior should match the unmarshal behavior and return a `null` bytes array. --- scalar.go | 3 +++ scalar_test.go | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/scalar.go b/scalar.go index c91574c..42bac75 100644 --- a/scalar.go +++ b/scalar.go @@ -74,6 +74,9 @@ type ( // MarshalJSON implements the json.Marshaler interface. // The URI is a quoted string. func (u URI) MarshalJSON() ([]byte, error) { + if u.URL == nil { + return []byte("null"), nil + } return json.Marshal(u.String()) } diff --git a/scalar_test.go b/scalar_test.go index c2b34f0..c7bb450 100644 --- a/scalar_test.go +++ b/scalar_test.go @@ -16,6 +16,11 @@ func TestURI_MarshalJSON(t *testing.T) { in githubv4.URI want string }{ + { + name: "null", + in: githubv4.URI{}, + want: `null`, + }, { in: githubv4.URI{URL: &url.URL{Scheme: "https", Host: "example.org", Path: "/foo/bar"}}, want: `"https://example.org/foo/bar"`,