Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Jun 24, 2024
1 parent 66ef2c9 commit d024535
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2516,22 +2516,37 @@ func TestReflector_Reflect_byteSlice(t *testing.T) {
r := jsonschema.Reflector{}

type S struct {
A []byte `json:"a" description:"Hello world!"`
A []byte `json:"a" description:"Hello world!"`
B json.RawMessage `json:"b" description:"I am a RawMessage."`
C *json.RawMessage `json:"c" description:"I am a RawMessage pointer."`
}

v, err := json.Marshal(S{A: []byte("hello world!")})
s1 := S{
A: []byte("hello world!"),
B: []byte(`{"foo":"bar"}`),
}
s1.C = &s1.B

v, err := json.Marshal(s1)
require.NoError(t, err)
assert.Equal(t, `{"a":"aGVsbG8gd29ybGQh"}`, string(v)) // []byte is marshaled to base64.
// []byte is marshaled to base64, RawMessage value and pointer are passed as is.
assert.Equal(t, `{"a":"aGVsbG8gd29ybGQh","b":{"foo":"bar"},"c":{"foo":"bar"}}`, string(v))

var s2 S

require.NoError(t, json.Unmarshal(v, &s2))
assert.Equal(t, "hello world!", string(s2.A)) // []byte is unmarshaled from base64.
assert.Equal(t, `{"foo":"bar"}`, string(s2.B))
assert.Equal(t, `{"foo":"bar"}`, string(*s2.C))

s, err := r.Reflect(S{})
require.NoError(t, err)
assertjson.EqMarshal(t, `{
"properties":{"a":{"description":"Hello world!","type":"string","format":"base64"}},
"properties":{
"a":{"description":"Hello world!","type":"string","format":"base64"},
"b":{"description":"I am a RawMessage."},
"c":{"description":"I am a RawMessage pointer."}
},
"type":"object"
}`, s)
}

0 comments on commit d024535

Please sign in to comment.