From 606e9d74fd66e93defd3dde2fb293ced7c39f56b Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Sat, 21 Oct 2023 22:01:16 -0700 Subject: [PATCH 1/2] fix: prevent panic on examples for pointer field types --- schema.go | 14 +++++++++++--- schema_test.go | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/schema.go b/schema.go index ebb93d6e..6480af6c 100644 --- a/schema.go +++ b/schema.go @@ -228,7 +228,7 @@ func floatTag(f reflect.StructField, tag string) *float64 { func jsonTagValue(f reflect.StructField, t reflect.Type, value string) any { // Special case: strings don't need quotes. - if t.Kind() == reflect.String { + if t.Kind() == reflect.String || (t.Kind() == reflect.Pointer && t.Elem().Kind() == reflect.String) { return value } @@ -262,11 +262,19 @@ func jsonTagValue(f reflect.StructField, t reflect.Type, value string) any { tmp = reflect.Append(tmp, vv.Index(i).Elem().Convert(t.Elem())) } v = tmp.Interface() - } else if !tv.ConvertibleTo(t) { + } else if !tv.ConvertibleTo(deref(t)) { panic(fmt.Errorf("unable to convert %v to %v: %w", tv, t, ErrSchemaInvalid)) } - v = reflect.ValueOf(v).Convert(t).Interface() + converted := reflect.ValueOf(v).Convert(deref(t)) + if t.Kind() == reflect.Ptr { + // Special case: if the field is a pointer, we need to get a pointer + // to the converted value. + tmp := reflect.New(t.Elem()) + tmp.Elem().Set(converted) + converted = tmp + } + v = converted.Interface() } return v diff --git a/schema_test.go b/schema_test.go index 6b0368e5..887fc53a 100644 --- a/schema_test.go +++ b/schema_test.go @@ -369,6 +369,29 @@ func TestSchema(t *testing.T) { } }`, }, + { + name: "fiel-pointer-example", + input: struct { + Int *int64 `json:"int" example:"123"` + Str *string `json:"str" example:"foo"` + }{}, + expected: `{ + "type": "object", + "additionalProperties": false, + "required": ["int", "str"], + "properties": { + "int": { + "type": "integer", + "format": "int64", + "examples": [123] + }, + "str": { + "type": "string", + "examples": ["foo"] + } + } + }`, + }, { name: "panic-bool", input: struct { From 7902577cccd96ec352f0861f34ae4674d46c7403 Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Sat, 21 Oct 2023 22:19:39 -0700 Subject: [PATCH 2/2] fix: consistent error handling --- schema.go | 12 ++++++------ schema_test.go | 11 +++++++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/schema.go b/schema.go index 6480af6c..04512cea 100644 --- a/schema.go +++ b/schema.go @@ -198,7 +198,7 @@ func boolTag(f reflect.StructField, tag string) bool { } else if v == "false" { return false } else { - panic("invalid bool tag '" + tag + "' for field '" + f.Name + "': " + v) + panic(fmt.Errorf("invalid bool tag '%s' for field '%s': %v", tag, f.Name, v)) } } return false @@ -209,7 +209,7 @@ func intTag(f reflect.StructField, tag string) *int { if i, err := strconv.Atoi(v); err == nil { return &i } else { - panic("invalid int tag '" + tag + "' for field '" + f.Name + "': " + v + " (" + err.Error() + ")") + panic(fmt.Errorf("invalid int tag '%s' for field '%s': %v (%w)", tag, f.Name, v, err)) } } return nil @@ -220,7 +220,7 @@ func floatTag(f reflect.StructField, tag string) *float64 { if i, err := strconv.ParseFloat(v, 64); err == nil { return &i } else { - panic("invalid float tag '" + tag + "' for field '" + f.Name + "': " + v + " (" + err.Error() + ")") + panic(fmt.Errorf("invalid float tag '%s' for field '%s': %v (%w)", tag, f.Name, v, err)) } } return nil @@ -243,7 +243,7 @@ func jsonTagValue(f reflect.StructField, t reflect.Type, value string) any { var v any if err := json.Unmarshal([]byte(value), &v); err != nil { - panic("invalid tag for field '" + f.Name + "': " + err.Error()) + panic(fmt.Errorf("invalid tag for field '%s': %w", f.Name, err)) } vv := reflect.ValueOf(v) @@ -256,14 +256,14 @@ func jsonTagValue(f reflect.StructField, t reflect.Type, value string) any { tmp := reflect.MakeSlice(t, 0, vv.Len()) for i := 0; i < vv.Len(); i++ { if !vv.Index(i).Elem().Type().ConvertibleTo(t.Elem()) { - panic(fmt.Errorf("unable to convert %v to %v: %w", vv.Index(i).Interface(), t.Elem(), ErrSchemaInvalid)) + panic(fmt.Errorf("unable to convert %v to %v for field '%s': %w", vv.Index(i).Interface(), t.Elem(), f.Name, ErrSchemaInvalid)) } tmp = reflect.Append(tmp, vv.Index(i).Elem().Convert(t.Elem())) } v = tmp.Interface() } else if !tv.ConvertibleTo(deref(t)) { - panic(fmt.Errorf("unable to convert %v to %v: %w", tv, t, ErrSchemaInvalid)) + panic(fmt.Errorf("unable to convert %v to %v for field '%s': %w", tv, t, f.Name, ErrSchemaInvalid)) } converted := reflect.ValueOf(v).Convert(deref(t)) diff --git a/schema_test.go b/schema_test.go index 887fc53a..693d4914 100644 --- a/schema_test.go +++ b/schema_test.go @@ -370,7 +370,7 @@ func TestSchema(t *testing.T) { }`, }, { - name: "fiel-pointer-example", + name: "field-pointer-example", input: struct { Int *int64 `json:"int" example:"123"` Str *string `json:"str" example:"foo"` @@ -420,6 +420,13 @@ func TestSchema(t *testing.T) { }{}, panics: `invalid tag for field 'Value': invalid character 'b' looking for beginning of value`, }, + { + name: "panic-json-type", + input: struct { + Value int `json:"value" example:"true"` + }{}, + panics: `unable to convert bool to int for field 'Value': schema is invalid`, + }, } for _, c := range cases { @@ -427,7 +434,7 @@ func TestSchema(t *testing.T) { r := NewMapRegistry("#/components/schemas/", DefaultSchemaNamer) if c.panics != "" { - assert.PanicsWithValue(t, c.panics, func() { + assert.PanicsWithError(t, c.panics, func() { r.Schema(reflect.TypeOf(c.input), false, "") }) } else {