Skip to content

Commit 82c0bdb

Browse files
authored
Merge pull request #6 from Barberrrry/master
Handle embedded struct fields and slice of any type
2 parents b230f13 + 5ba57f0 commit 82c0bdb

File tree

4 files changed

+101
-12
lines changed

4 files changed

+101
-12
lines changed

README.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ import (
2626
"github.com/mcuadros/go-jsonschema-generator"
2727
)
2828

29+
type EmbeddedType struct {
30+
Zoo string
31+
}
32+
33+
type Item struct {
34+
Value string
35+
}
36+
2937
type ExampleBasic struct {
3038
Foo bool `json:"foo"`
3139
Bar string `json:",omitempty"`
3240
Qux int8
3341
Baz []string
42+
EmbeddedType
43+
List []Item
3444
}
3545

3646
func main() {
@@ -54,17 +64,36 @@ func main() {
5464
"type": "string"
5565
}
5666
},
67+
"List": {
68+
"type": "array",
69+
"items": {
70+
"type": "object",
71+
"properties": {
72+
"Value": {
73+
"type": "string"
74+
}
75+
},
76+
"required": [
77+
"Value"
78+
]
79+
}
80+
},
5781
"Qux": {
5882
"type": "integer"
5983
},
84+
"Zoo": {
85+
"type": "string"
86+
},
6087
"foo": {
61-
"type": "bool"
88+
"type": "boolean"
6289
}
6390
},
6491
"required": [
6592
"foo",
6693
"Qux",
67-
"Baz"
94+
"Baz",
95+
"Zoo",
96+
"List"
6897
]
6998
}
7099
```

example/simple.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/mcuadros/go-jsonschema-generator"
5+
"github.com/Barberrrry/go-jsonschema-generator"
66
)
77

8+
type EmbeddedType struct {
9+
Zoo string
10+
}
11+
12+
type Item struct {
13+
Value string
14+
}
15+
816
type ExampleBasic struct {
917
Foo bool `json:"foo"`
1018
Bar string `json:",omitempty"`
1119
Qux int8
1220
Baz []string
21+
EmbeddedType
22+
List []Item
1323
}
1424

1525
func main() {

jsonschema.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,12 @@ func (d *Document) String() string {
4545
type property struct {
4646
Type string `json:"type,omitempty"`
4747
Format string `json:"format,omitempty"`
48-
Items *item `json:"items,omitempty"`
48+
Items *property `json:"items,omitempty"`
4949
Properties map[string]*property `json:"properties,omitempty"`
5050
Required []string `json:"required,omitempty"`
5151
AdditionalProperties bool `json:"additionalProperties,omitempty"`
5252
}
5353

54-
type item struct {
55-
Type string `json:"type,omitempty"`
56-
}
57-
5854
func (p *property) read(t reflect.Type, opts tagOptions) {
5955
jsType, format, kind := getTypeFromMapping(t)
6056
if jsType != "" {
@@ -81,7 +77,8 @@ func (p *property) readFromSlice(t reflect.Type) {
8177
if kind == reflect.Uint8 {
8278
p.Type = "string"
8379
} else if jsType != "" {
84-
p.Items = &item{Type: jsType}
80+
p.Items = &property{}
81+
p.Items.read(t.Elem(), tagOptions(""))
8582
}
8683
}
8784

@@ -114,6 +111,18 @@ func (p *property) readFromStruct(t reflect.Type) {
114111
continue
115112
}
116113

114+
if field.Anonymous {
115+
embeddedProperty := &property{}
116+
embeddedProperty.read(field.Type, opts)
117+
118+
for name, property := range embeddedProperty.Properties {
119+
p.Properties[name] = property
120+
}
121+
p.Required = append(p.Required, embeddedProperty.Required...)
122+
123+
continue
124+
}
125+
117126
p.Properties[name] = &property{}
118127
p.Properties[name].read(field.Type, opts)
119128

jsonschema_test.go

+44-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,14 @@ func (self *propertySuite) TestLoadWithTag(c *C) {
8686
})
8787
}
8888

89+
type SliceStruct struct {
90+
Value string
91+
}
92+
8993
type ExampleJSONBasicSlices struct {
9094
Slice []string `json:",foo,omitempty"`
9195
SliceOfInterface []interface{} `json:",foo"`
96+
SliceOfStruct []SliceStruct
9297
}
9398

9499
func (self *propertySuite) TestLoadSliceAndContains(c *C) {
@@ -102,14 +107,26 @@ func (self *propertySuite) TestLoadSliceAndContains(c *C) {
102107
Properties: map[string]*property{
103108
"Slice": &property{
104109
Type: "array",
105-
Items: &item{Type: "string"},
110+
Items: &property{Type: "string"},
106111
},
107112
"SliceOfInterface": &property{
108113
Type: "array",
109114
},
115+
"SliceOfStruct": &property{
116+
Type: "array",
117+
Items: &property{
118+
Type: "object",
119+
Required: []string{"Value"},
120+
Properties: map[string]*property{
121+
"Value": &property{
122+
Type: "string",
123+
},
124+
},
125+
},
126+
},
110127
},
111128

112-
Required: []string{"SliceOfInterface"},
129+
Required: []string{"SliceOfInterface", "SliceOfStruct"},
113130
},
114131
})
115132
}
@@ -142,6 +159,30 @@ func (self *propertySuite) TestLoadNested(c *C) {
142159
})
143160
}
144161

162+
type EmbeddedStruct struct {
163+
Foo string
164+
}
165+
166+
type ExampleJSONEmbeddedStruct struct {
167+
EmbeddedStruct
168+
}
169+
170+
func (self *propertySuite) TestLoadEmbedded(c *C) {
171+
j := &Document{}
172+
j.Read(&ExampleJSONEmbeddedStruct{})
173+
174+
c.Assert(*j, DeepEquals, Document{
175+
Schema: "http://json-schema.org/schema#",
176+
property: property{
177+
Type: "object",
178+
Properties: map[string]*property{
179+
"Foo": &property{Type: "string"},
180+
},
181+
Required: []string{"Foo"},
182+
},
183+
})
184+
}
185+
145186
type ExampleJSONBasicMaps struct {
146187
Maps map[string]string `json:",omitempty"`
147188
MapOfInterface map[string]interface{}
@@ -181,7 +222,7 @@ func (self *propertySuite) TestLoadNonStruct(c *C) {
181222
Schema: "http://json-schema.org/schema#",
182223
property: property{
183224
Type: "array",
184-
Items: &item{Type: "string"},
225+
Items: &property{Type: "string"},
185226
},
186227
})
187228
}

0 commit comments

Comments
 (0)