Skip to content

Commit

Permalink
Fix default parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Mar 6, 2024
1 parent 57ddbd6 commit 108ff6a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
67 changes: 23 additions & 44 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,57 +1113,36 @@ func (r *Reflector) walkProperties(v reflect.Value, parent *Schema, rc *ReflectC
}

func checkInlineValue(propertySchema *Schema, field reflect.StructField, tag string, setter func(interface{}) *Schema) error {
var val interface{}
var (
val interface{}
t SimpleType

i *int64
f *float64
s *string
b *bool
)

var t SimpleType
if propertySchema.Type != nil && propertySchema.Type.SimpleTypes != nil {
t = *propertySchema.Type.SimpleTypes
}

switch t { //nolint:exhaustive // Covered by default case.
case Integer:
var v *int64

if err := refl.ReadIntPtrTag(field.Tag, tag, &v); err != nil {
return fmt.Errorf("parsing %s for %s: %w", tag, t, err)
}

if v != nil {
val = *v
}
case Number:
var v *float64

if err := refl.ReadFloatPtrTag(field.Tag, tag, &v); err != nil {
return fmt.Errorf("parsing %s for %s: %w", tag, t, err)
}

if v != nil {
val = *v
}

case String:
var v *string

refl.ReadStringPtrTag(field.Tag, tag, &v)

if v != nil {
val = *v
}

case Boolean:
var v *bool
_ = refl.ReadIntPtrTag(field.Tag, tag, &i) //nolint:errcheck
_ = refl.ReadFloatPtrTag(field.Tag, tag, &f) //nolint:errcheck
_ = refl.ReadBoolPtrTag(field.Tag, tag, &b) //nolint:errcheck
refl.ReadStringPtrTag(field.Tag, tag, &s)

if err := refl.ReadBoolPtrTag(field.Tag, tag, &v); err != nil {
return fmt.Errorf("parsing %s for %s: %w", tag, t, err)
}

if v != nil {
val = *v
}
case Null:
switch {
case propertySchema.HasType(Number) && f != nil:
val = *f
case propertySchema.HasType(Integer) && i != nil:
val = *i
case propertySchema.HasType(Boolean) && b != nil:
val = *b
case propertySchema.HasType(String) && s != nil:
val = *s
case t == Null:
// No default for type null.

default:
var v string

Expand Down
31 changes: 31 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1918,3 +1918,34 @@ func TestReflector_AddOperation_rawSchema(t *testing.T) {
"type":"object"
}`, s)
}

type Discover string

const (
DiscoverAll Discover = "all"
DiscoverNone Discover = "none"
)

func (d *Discover) Enum() []any {

Check failure on line 1929 in reflect_test.go

View workflow job for this annotation

GitHub Actions / test (1.13.x)

undefined: any
return []any{DiscoverAll, DiscoverNone}

Check failure on line 1930 in reflect_test.go

View workflow job for this annotation

GitHub Actions / test (1.13.x)

undefined: any
}

func TestReflector_Reflect_ptrDefault(t *testing.T) {
type NewThing struct {
DiscoverMode *Discover `json:"discover,omitempty" default:"all"`
}

r := jsonschema.Reflector{}

s, err := r.Reflect(NewThing{})
require.NoError(t, err)
assertjson.EqMarshal(t, `{
"definitions":{
"JsonschemaGoTestDiscover":{"enum":["all","none"],"type":["null","string"]}
},
"properties":{
"discover":{"$ref":"#/definitions/JsonschemaGoTestDiscover","default":"all"}
},
"type":"object"
}`, s)
}

0 comments on commit 108ff6a

Please sign in to comment.