Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default parsing #110

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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

Check notice on line 1137 in reflect.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) are not covered by tests.

Check warning on line 1137 in reflect.go

View check run for this annotation

Codecov / codecov/patch

reflect.go#L1136-L1137

Added lines #L1136 - L1137 were not covered by tests
case propertySchema.HasType(Integer) && i != nil:
val = *i
case propertySchema.HasType(Boolean) && b != nil:
val = *b

Check notice on line 1141 in reflect.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) are not covered by tests.

Check warning on line 1141 in reflect.go

View check run for this annotation

Codecov / codecov/patch

reflect.go#L1140-L1141

Added lines #L1140 - L1141 were not covered by tests
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() []interface{} {
return []interface{}{DiscoverAll, DiscoverNone}
}

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)
}
Loading