Skip to content

Commit

Permalink
fix required list for embedded struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Aug 30, 2023
1 parent af53f8c commit 824fb79
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
28 changes: 19 additions & 9 deletions schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func (s Schemas) add(r Ref, scm *Schema) {
}
}

func (s Schemas) del(r Ref) {
if r.Unique() {
delete(s.types, r.ID)
}
}

// DefineT converts the t to Schema recursively and append newly meet schemas to the schema list s.
func (s Schemas) DefineT(t reflect.Type) *Schema { //nolint: cyclop,gocyclo
r := s.RefT(t)
Expand Down Expand Up @@ -173,6 +179,16 @@ func (s Schemas) DefineT(t reflect.Type) *Schema { //nolint: cyclop,gocyclo

p := s.DefineT(f.Type)

ps := s.PeakSchema(p)
if f.Anonymous && (tag == nil || tag.Name == "") && indirectType(f.Type).Kind() == reflect.Struct {
for k, v := range ps.Properties {
scm.Properties[k] = v
}
s.del(s.RefT(f.Type))
scm.Required = append(scm.Required, ps.Required...)
continue
}

desc := f.Tag.Get("description")
if desc != "" {
p.Description = desc
Expand All @@ -196,18 +212,12 @@ func (s Schemas) DefineT(t reflect.Type) *Schema { //nolint: cyclop,gocyclo
p.Type = TypeString
}
}

scm.Properties[n] = p

if tag == nil || !tag.Omitempty {
scm.Required = append(scm.Required, n)
}

ps := s.PeakSchema(p)
if f.Anonymous && (tag == nil || tag.Name == "") && len(ps.Properties) > 0 {
for k, v := range ps.Properties {
scm.Properties[k] = v
}
} else {
scm.Properties[n] = p
}
}

case reflect.Ptr:
Expand Down
20 changes: 4 additions & 16 deletions schemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ func TestRef(t *testing.T) {
func TestEmbeddedStruct(t *testing.T) {
g := got.T(t)

type A struct{ Val int }
type A struct {
Val int
}

type B struct {
A
Expand All @@ -324,20 +326,6 @@ func TestEmbeddedStruct(t *testing.T) {
c.Define(B{})

g.Eq(g.JSON(c.String()), map[string]interface{} /* len=2 */ {
"A": map[string]interface{} /* len=6 */ {
`additionalProperties` /* len=20 */ : false,
"description": `github.com/NaturalSelectionLabs/jschema_test.A`, /* len=46 */
"properties": map[string]interface{}{
"Val": map[string]interface{}{
"type": "number",
},
},
"required": []interface{} /* len=1 cap=1 */ {
"Val",
},
"title": "A",
"type": "object",
},
"B": map[string]interface{} /* len=6 */ {
`additionalProperties` /* len=20 */ : false,
"description": `github.com/NaturalSelectionLabs/jschema_test.B`, /* len=46 */
Expand All @@ -347,7 +335,7 @@ func TestEmbeddedStruct(t *testing.T) {
},
},
"required": []interface{} /* len=1 cap=1 */ {
"A",
"Val",
},
"title": "B",
"type": "object",
Expand Down
7 changes: 7 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,10 @@ func ToJValList[T any](list ...T) []JVal {

return to
}

func indirectType(t reflect.Type) reflect.Type {
if t.Kind() == reflect.Ptr {
return t.Elem()
}
return t
}

0 comments on commit 824fb79

Please sign in to comment.