Skip to content

Commit

Permalink
Read deprecated flag from field tag (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jul 29, 2021
1 parent 4fb3b7f commit 7d49323
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
19 changes: 11 additions & 8 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,7 @@ func (r *Reflector) walkProperties(v reflect.Value, parent *Schema, rc *ReflectC
}

if tag == "" && field.Anonymous && field.Type.Kind() == reflect.Struct {
err := r.walkProperties(v.Field(i), parent, rc)
if err != nil {
if err := r.walkProperties(v.Field(i), parent, rc); err != nil {
return err
}

Expand All @@ -568,8 +567,7 @@ func (r *Reflector) walkProperties(v reflect.Value, parent *Schema, rc *ReflectC
omitEmpty := strings.Contains(tag, ",omitempty")
required := false

err := refl.ReadBoolTag(field.Tag, "required", &required)
if err != nil {
if err := refl.ReadBoolTag(field.Tag, "required", &required); err != nil {
return err
}

Expand Down Expand Up @@ -606,13 +604,18 @@ func (r *Reflector) walkProperties(v reflect.Value, parent *Schema, rc *ReflectC
}
}

err = refl.PopulateFieldsFromTags(&propertySchema, field.Tag)
if err != nil {
if err := refl.PopulateFieldsFromTags(&propertySchema, field.Tag); err != nil {
return err
}

err = reflectExample(&propertySchema, field)
if err != nil {
deprecated := false
if err := refl.ReadBoolTag(field.Tag, "deprecated", &deprecated); err != nil {
return err
} else if deprecated {
propertySchema.WithExtraPropertiesItem("deprecated", true)
}

if err := reflectExample(&propertySchema, field); err != nil {
return err
}

Expand Down
5 changes: 3 additions & 2 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ func (ISOCountry) JSONSchema() (jsonschema.Schema, error) {
func TestExposer(t *testing.T) {
type Some struct {
Week ISOWeek `json:"week"`
Country ISOCountry `json:"country"`
Country ISOCountry `json:"country" deprecated:"true"`
}

s, err := (&jsonschema.Reflector{}).Reflect(Some{})
Expand Down Expand Up @@ -626,7 +626,8 @@ func TestExposer(t *testing.T) {
},
"properties": {
"country": {
"$ref": "#/definitions/JsonschemaGoTestISOCountry"
"$ref": "#/definitions/JsonschemaGoTestISOCountry",
"deprecated": true
},
"week": {
"$ref": "#/definitions/JsonschemaGoTestISOWeek"
Expand Down

0 comments on commit 7d49323

Please sign in to comment.