Skip to content

Commit

Permalink
Recognize []byte as base64 string (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jun 24, 2024
1 parent 9acc00c commit b8c2e24
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

var (
typeOfJSONRawMsg = reflect.TypeOf(json.RawMessage{})
typeOfByteSlice = reflect.TypeOf([]byte{})
typeOfTime = reflect.TypeOf(time.Time{})
typeOfDate = reflect.TypeOf(Date{})
typeOfTextUnmarshaler = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
Expand Down Expand Up @@ -731,6 +732,13 @@ func (r *Reflector) applySubSchemas(v reflect.Value, rc *ReflectContext, schema
}

func (r *Reflector) isWellKnownType(t reflect.Type, schema *Schema) bool {
if t == typeOfByteSlice {
schema.AddType(String)
schema.WithFormat("base64")

return true
}

if t == typeOfTime {
schema.AddType(String)
schema.WithFormat("date-time")
Expand Down
39 changes: 39 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2511,3 +2511,42 @@ func TestReflector_Reflect_ElseExposer(t *testing.T) {
require.NoError(t, err)
assertjson.EqMarshal(t, `{"type":"string","else":{"title":"test2","type":"string"}}`, s)
}

func TestReflector_Reflect_byteSlice(t *testing.T) {
r := jsonschema.Reflector{}

type S struct {
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."`
}

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

v, err := json.Marshal(s1)
require.NoError(t, err)
// []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"},
"b":{"description":"I am a RawMessage."},
"c":{"description":"I am a RawMessage pointer."}
},
"type":"object"
}`, s)
}

0 comments on commit b8c2e24

Please sign in to comment.