forked from invopop/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect.go
1072 lines (950 loc) · 30.7 KB
/
reflect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package jsonschema uses reflection to generate JSON Schemas from Go types [1].
//
// If json tags are present on struct fields, they will be used to infer
// property names and if a property is required (omitempty is present).
//
// [1] http://json-schema.org/latest/json-schema-validation.html
package jsonschema
import (
"bytes"
"encoding/json"
"net"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"github.com/iancoleman/orderedmap"
)
// Version is the JSON Schema version.
var Version = "http://json-schema.org/draft/2020-12/schema"
// Schema represents a JSON Schema object type.
// RFC draft-bhutton-json-schema-00 section 4.3
type Schema struct {
// RFC draft-bhutton-json-schema-00
Version string `json:"$schema,omitempty"` // section 8.1.1
ID ID `json:"$id,omitempty"` // section 8.2.1
Anchor string `json:"$anchor,omitempty"` // section 8.2.2
Ref string `json:"$ref,omitempty"` // section 8.2.3.1
DynamicRef string `json:"$dynamicRef,omitempty"` // section 8.2.3.2
Definitions Definitions `json:"$defs,omitempty"` // section 8.2.4
Comments string `json:"$comment,omitempty"` // section 8.3
// RFC draft-bhutton-json-schema-00 section 10.2.1 (Sub-schemas with logic)
AllOf []*Schema `json:"allOf,omitempty"` // section 10.2.1.1
AnyOf []*Schema `json:"anyOf,omitempty"` // section 10.2.1.2
OneOf []*Schema `json:"oneOf,omitempty"` // section 10.2.1.3
Not *Schema `json:"not,omitempty"` // section 10.2.1.4
// RFC draft-bhutton-json-schema-00 section 10.2.2 (Apply sub-schemas conditionally)
If *Schema `json:"if,omitempty"` // section 10.2.2.1
Then *Schema `json:"then,omitempty"` // section 10.2.2.2
Else *Schema `json:"else,omitempty"` // section 10.2.2.3
DependentSchemas map[string]*Schema `json:"dependentSchemas,omitempty"` // section 10.2.2.4
// RFC draft-bhutton-json-schema-00 section 10.3.1 (arrays)
PrefixItems []*Schema `json:"prefixItems,omitempty"` // section 10.3.1.1
Items *Schema `json:"items,omitempty"` // section 10.3.1.2 (replaces additionalItems)
Contains *Schema `json:"contains,omitempty"` // section 10.3.1.3
// RFC draft-bhutton-json-schema-00 section 10.3.2 (sub-schemas)
Properties *orderedmap.OrderedMap `json:"properties,omitempty"` // section 10.3.2.1
PatternProperties map[string]*Schema `json:"patternProperties,omitempty"` // section 10.3.2.2
AdditionalProperties *Schema `json:"additionalProperties,omitempty"` // section 10.3.2.3
PropertyNames *Schema `json:"propertyNames,omitempty"` // section 10.3.2.4
// RFC draft-bhutton-json-schema-validation-00, section 6
Type string `json:"type,omitempty"` // section 6.1.1
Enum []interface{} `json:"enum,omitempty"` // section 6.1.2
Const interface{} `json:"const,omitempty"` // section 6.1.3
MultipleOf int `json:"multipleOf,omitempty"` // section 6.2.1
Maximum int `json:"maximum,omitempty"` // section 6.2.2
ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"` // section 6.2.3
Minimum int `json:"minimum,omitempty"` // section 6.2.4
ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"` // section 6.2.5
MaxLength int `json:"maxLength,omitempty"` // section 6.3.1
MinLength int `json:"minLength,omitempty"` // section 6.3.2
Pattern string `json:"pattern,omitempty"` // section 6.3.3
MaxItems int `json:"maxItems,omitempty"` // section 6.4.1
MinItems int `json:"minItems,omitempty"` // section 6.4.2
UniqueItems bool `json:"uniqueItems,omitempty"` // section 6.4.3
MaxContains uint `json:"maxContains,omitempty"` // section 6.4.4
MinContains uint `json:"minContains,omitempty"` // section 6.4.5
MaxProperties int `json:"maxProperties,omitempty"` // section 6.5.1
MinProperties int `json:"minProperties,omitempty"` // section 6.5.2
Required []string `json:"required,omitempty"` // section 6.5.3
DependentRequired map[string][]string `json:"dependentRequired,omitempty"` // section 6.5.4
// RFC draft-bhutton-json-schema-validation-00, section 7
Format string `json:"format,omitempty"`
// RFC draft-bhutton-json-schema-validation-00, section 8
ContentEncoding string `json:"contentEncoding,omitempty"` // section 8.3
ContentMediaType string `json:"contentMediaType,omitempty"` // section 8.4
ContentSchema *Schema `json:"contentSchema,omitempty"` // section 8.5
// RFC draft-bhutton-json-schema-validation-00, section 9
Title string `json:"title,omitempty"` // section 9.1
Description string `json:"description,omitempty"` // section 9.1
Default interface{} `json:"default,omitempty"` // section 9.2
Deprecated bool `json:"deprecated,omitempty"` // section 9.3
ReadOnly bool `json:"readOnly,omitempty"` // section 9.4
WriteOnly bool `json:"writeOnly,omitempty"` // section 9.4
Examples []interface{} `json:"examples,omitempty"` // section 9.5
Extras map[string]interface{} `json:"-"`
// Special boolean representation of the Schema - section 4.3.2
boolean *bool
}
var (
// TrueSchema defines a schema with a true value
TrueSchema = &Schema{boolean: &[]bool{true}[0]}
// FalseSchema defines a schema with a false value
FalseSchema = &Schema{boolean: &[]bool{false}[0]}
)
// customSchemaImpl is used to detect if the type provides it's own
// custom Schema Type definition to use instead. Very useful for situations
// where there are custom JSON Marshal and Unmarshal methods.
type customSchemaImpl interface {
JSONSchema() (*Schema, Definitions)
}
var customType = reflect.TypeOf((*customSchemaImpl)(nil)).Elem()
// customSchemaGetFieldDocString
type customSchemaGetFieldDocString interface {
GetFieldDocString(fieldName string) string
}
type customGetFieldDocString func(fieldName string) string
var customStructGetFieldDocString = reflect.TypeOf((*customSchemaGetFieldDocString)(nil)).Elem()
// Reflect reflects to Schema from a value using the default Reflector
func Reflect(v interface{}) *Schema {
return ReflectFromType(reflect.TypeOf(v))
}
// ReflectFromType generates root schema using the default Reflector
func ReflectFromType(t reflect.Type) *Schema {
r := &Reflector{}
return r.ReflectFromType(t)
}
// A Reflector reflects values into a Schema.
type Reflector struct {
// BaseSchemaID defines the URI that will be used as a base to determine Schema
// IDs for models. For example, a base Schema ID of `https://invopop.com/schemas`
// when defined with a struct called `User{}`, will result in a schema with an
// ID set to `https://invopop.com/schemas/user`.
//
// If no `BaseSchemaID` is provided, we'll take the type's complete package path
// and use that as a base instead. Set `Anonymous` to try if you do not want to
// include a schema ID.
BaseSchemaID ID
// Anonymous when true will hide the auto-generated Schema ID and provide what is
// known as an "anonymous schema". As a rule, this is not recommended.
Anonymous bool
// AssignAnchor when true will use the original struct's name as an anchor inside
// every definition, including the root schema. These can be useful for having a
// reference to the original struct's name in CamelCase instead of the snake-case used
// by default for URI compatibility.
//
// Anchors do not appear to be widely used out in the wild, so at this time the
// anchors themselves will not be used inside generated schema.
AssignAnchor bool
// AllowAdditionalProperties will cause the Reflector to generate a schema
// without additionalProperties set to 'false' for all struct types. This means
// the presence of additional keys in JSON objects will not cause validation
// to fail. Note said additional keys will simply be dropped when the
// validated JSON is unmarshaled.
AllowAdditionalProperties bool
// RequiredFromJSONSchemaTags will cause the Reflector to generate a schema
// that requires any key tagged with `jsonschema:required`, overriding the
// default of requiring any key *not* tagged with `json:,omitempty`.
RequiredFromJSONSchemaTags bool
// YAMLEmbeddedStructs will cause the Reflector to generate a schema that does
// not inline embedded structs. This should be enabled if the JSON schemas are
// used with yaml.Marshal/Unmarshal.
YAMLEmbeddedStructs bool
// Prefer yaml: tags over json: tags to generate the schema even if json: tags
// are present
PreferYAMLSchema bool
// Do not reference definitions. This will remove the top-level $defs map and
// instead cause the entire structure of types to be output in one tree. The
// list of type definitions (`$defs`) will not be included.
DoNotReference bool
// ExpandedStruct when true will include the reflected type's definition in the
// root as opposed to a definition with a reference. Using a reference in the root
// is useful as it allows us to maintain the struct's original name, but it is
// not common practice.
ExpandedStruct bool
// IgnoredTypes defines a slice of types that should be ignored in the schema,
// switching to just allowing additional properties instead.
IgnoredTypes []interface{}
// Lookup allows a function to be defined that will provide a custom mapping of
// types to Schema IDs. This allows existing schema documents to be referenced
// by their ID instead of being embedded into the current schema definitions.
// Reflected types will never be pointers, only underlying elements.
Lookup func(reflect.Type) ID
// Mapper is a function that can be used to map custom Go types to jsonschema schemas.
Mapper func(reflect.Type) *Schema
// Namer allows customizing of type names. The default is to use the type's name
// provided by the reflect package.
Namer func(reflect.Type) string
// KeyNamer allows customizing of key names.
// The default is to use the key's name as is, or the json (or yaml) tag if present.
// If a json or yaml tag is present, KeyNamer will receive the tag's name as an argument, not the original key name.
KeyNamer func(string) string
// AdditionalFields allows adding structfields for a given type
AdditionalFields func(reflect.Type) []reflect.StructField
// CommentMap is a dictionary of fully qualified go types and fields to comment
// strings that will be used if a description has not already been provided in
// the tags. Types and fields are added to the package path using "." as a
// separator.
//
// Type descriptions should be defined like:
//
// map[string]string{"github.com/invopop/jsonschema.Reflector": "A Reflector reflects values into a Schema."}
//
// And Fields defined as:
//
// map[string]string{"github.com/invopop/jsonschema.Reflector.DoNotReference": "Do not reference definitions."}
//
// See also: AddGoComments
CommentMap map[string]string
}
// Reflect reflects to Schema from a value.
func (r *Reflector) Reflect(v interface{}) *Schema {
return r.ReflectFromType(reflect.TypeOf(v))
}
// ReflectFromType generates root schema
func (r *Reflector) ReflectFromType(t reflect.Type) *Schema {
if t.Kind() == reflect.Ptr {
t = t.Elem() // re-assign from pointer
}
name := r.typeName(t)
s := new(Schema)
definitions := Definitions{}
s.Definitions = definitions
bs := r.reflectTypeToSchemaWithID(definitions, t)
if r.ExpandedStruct {
*s = *definitions[name]
delete(definitions, name)
} else {
*s = *bs
}
// Attempt to set the schema ID
if !r.Anonymous && s.ID == EmptyID {
baseSchemaID := r.BaseSchemaID
if baseSchemaID == EmptyID {
id := ID("https://" + t.PkgPath())
if err := id.Validate(); err == nil {
// it's okay to silently ignore URL errors
baseSchemaID = id
}
}
if baseSchemaID != EmptyID {
s.ID = baseSchemaID.Add(ToSnakeCase(name))
}
}
s.Version = Version
if !r.DoNotReference {
s.Definitions = definitions
}
return s
}
// Definitions hold schema definitions.
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.26
// RFC draft-wright-json-schema-validation-00, section 5.26
type Definitions map[string]*Schema
// Available Go defined types for JSON Schema Validation.
// RFC draft-wright-json-schema-validation-00, section 7.3
var (
timeType = reflect.TypeOf(time.Time{}) // date-time RFC section 7.3.1
ipType = reflect.TypeOf(net.IP{}) // ipv4 and ipv6 RFC section 7.3.4, 7.3.5
uriType = reflect.TypeOf(url.URL{}) // uri RFC section 7.3.6
)
// Byte slices will be encoded as base64
var byteSliceType = reflect.TypeOf([]byte(nil))
// Except for json.RawMessage
var rawMessageType = reflect.TypeOf(json.RawMessage{})
// Go code generated from protobuf enum types should fulfil this interface.
type protoEnum interface {
EnumDescriptor() ([]byte, []int)
}
var protoEnumType = reflect.TypeOf((*protoEnum)(nil)).Elem()
// SetBaseSchemaID is a helper use to be able to set the reflectors base
// schema ID from a string as opposed to then ID instance.
func (r *Reflector) SetBaseSchemaID(id string) {
r.BaseSchemaID = ID(id)
}
func (r *Reflector) refOrReflectTypeToSchema(definitions Definitions, t reflect.Type) *Schema {
id := r.lookupID(t)
if id != EmptyID {
return &Schema{
Ref: id.String(),
}
}
// Already added to definitions?
if _, ok := definitions[r.typeName(t)]; ok && !r.DoNotReference {
return r.refDefinition(definitions, t)
}
return r.reflectTypeToSchemaWithID(definitions, t)
}
func (r *Reflector) reflectTypeToSchemaWithID(defs Definitions, t reflect.Type) *Schema {
s := r.reflectTypeToSchema(defs, t)
if s != nil {
if r.Lookup != nil {
id := r.Lookup(t)
if id != EmptyID {
s.ID = id
}
}
}
return s
}
func (r *Reflector) reflectTypeToSchema(definitions Definitions, t reflect.Type) *Schema {
if r.Mapper != nil {
if t := r.Mapper(t); t != nil {
return t
}
}
if rt := r.reflectCustomSchema(definitions, t); rt != nil {
return rt
}
// jsonpb will marshal protobuf enum options as either strings or integers.
// It will unmarshal either.
if t.Implements(protoEnumType) {
return &Schema{OneOf: []*Schema{
{Type: "string"},
{Type: "integer"},
}}
}
// Defined format types for JSON Schema Validation
// RFC draft-wright-json-schema-validation-00, section 7.3
// TODO email RFC section 7.3.2, hostname RFC section 7.3.3, uriref RFC section 7.3.7
if t == ipType {
// TODO differentiate ipv4 and ipv6 RFC section 7.3.4, 7.3.5
return &Schema{Type: "string", Format: "ipv4"} // ipv4 RFC section 7.3.4
}
switch t.Kind() {
case reflect.Struct:
switch t {
case timeType: // date-time RFC section 7.3.1
return &Schema{Type: "string", Format: "date-time"}
case uriType: // uri RFC section 7.3.6
return &Schema{Type: "string", Format: "uri"}
default:
return r.reflectOrRefStruct(definitions, t)
}
case reflect.Map:
switch t.Key().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
rt := &Schema{
Type: "object",
PatternProperties: map[string]*Schema{
"^[0-9]+$": r.refOrReflectTypeToSchema(definitions, t.Elem()),
},
AdditionalProperties: FalseSchema,
}
return rt
}
var rt *Schema
if t.Elem().Kind() == reflect.Interface {
rt = &Schema{
Type: "object",
}
} else {
rt = &Schema{
Type: "object",
PatternProperties: map[string]*Schema{
".*": r.refOrReflectTypeToSchema(definitions, t.Elem()),
},
}
}
return rt
case reflect.Slice, reflect.Array:
returnType := &Schema{}
if t == rawMessageType {
return &Schema{}
}
if t.Kind() == reflect.Array {
returnType.MinItems = t.Len()
returnType.MaxItems = returnType.MinItems
}
if t.Kind() == reflect.Slice && t.Elem() == byteSliceType.Elem() {
returnType.Type = "string"
// NOTE: ContentMediaType is not set here
returnType.ContentEncoding = "base64"
return returnType
}
returnType.Type = "array"
returnType.Items = r.refOrReflectTypeToSchema(definitions, t.Elem())
return returnType
case reflect.Interface:
return &Schema{} // empty
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return &Schema{Type: "integer"}
case reflect.Float32, reflect.Float64:
return &Schema{Type: "number"}
case reflect.Bool:
return &Schema{Type: "boolean"}
case reflect.String:
return &Schema{Type: "string"}
case reflect.Ptr:
return r.refOrReflectTypeToSchema(definitions, t.Elem())
}
panic("unsupported type " + t.String())
}
func (r *Reflector) reflectCustomSchema(definitions Definitions, t reflect.Type) *Schema {
if t.Kind() == reflect.Ptr {
return r.reflectCustomSchema(definitions, t.Elem())
}
if t.Implements(customType) {
v := reflect.New(t)
o := v.Interface().(customSchemaImpl)
st, newDefs := o.JSONSchema()
for name, def := range newDefs {
definitions[name] = def
}
r.addDefinition(definitions, t, st)
if r.DoNotReference {
return st
} else {
return r.refDefinition(definitions, t)
}
}
return nil
}
func (r *Reflector) reflectOrRefStruct(definitions Definitions, t reflect.Type) *Schema {
st := new(Schema)
r.addDefinition(definitions, t, st) // makes sure we have a re-usable reference already
r.reflectStruct(definitions, t, st)
if r.DoNotReference {
return st
} else {
return r.refDefinition(definitions, t)
}
}
// Reflects a struct to a JSON Schema type.
func (r *Reflector) reflectStruct(definitions Definitions, t reflect.Type, s *Schema) {
s.Type = "object"
s.Properties = orderedmap.New()
s.Description = r.lookupComment(t, "")
if r.AssignAnchor {
s.Anchor = t.Name()
}
if !r.AllowAdditionalProperties {
s.AdditionalProperties = FalseSchema
}
ignored := false
for _, it := range r.IgnoredTypes {
if reflect.TypeOf(it) == t {
ignored = true
break
}
}
if !ignored {
r.reflectStructFields(s, definitions, t)
}
}
func (r *Reflector) reflectStructFields(st *Schema, definitions Definitions, t reflect.Type) {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return
}
var getFieldDocString customGetFieldDocString
if t.Implements(customStructGetFieldDocString) {
v := reflect.New(t)
o := v.Interface().(customSchemaGetFieldDocString)
getFieldDocString = o.GetFieldDocString
}
handleField := func(f reflect.StructField) {
name, shouldEmbed, required, nullable := r.reflectFieldName(f)
// if anonymous and exported type should be processed recursively
// current type should inherit properties of anonymous one
if name == "" {
if shouldEmbed {
r.reflectStructFields(st, definitions, f.Type)
}
return
}
property := r.refOrReflectTypeToSchema(definitions, f.Type)
property.structKeywordsFromTags(f, st, name)
if property.Description == "" {
property.Description = r.lookupComment(t, f.Name)
}
if getFieldDocString != nil {
property.Description = getFieldDocString(f.Name)
}
if nullable {
property = &Schema{
OneOf: []*Schema{
property,
{
Type: "null",
},
},
}
}
st.Properties.Set(name, property)
if required {
st.Required = append(st.Required, name)
}
}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
handleField(f)
}
if r.AdditionalFields != nil {
if af := r.AdditionalFields(t); af != nil {
for _, sf := range af {
handleField(sf)
}
}
}
}
func (r *Reflector) lookupComment(t reflect.Type, name string) string {
if r.CommentMap == nil {
return ""
}
n := fullyQualifiedTypeName(t)
if name != "" {
n = n + "." + name
}
return r.CommentMap[n]
}
// addDefinition will append the provided schema. If needed, an ID and anchor will also be added.
func (r *Reflector) addDefinition(definitions Definitions, t reflect.Type, s *Schema) {
name := r.typeName(t)
definitions[name] = s
}
// refDefinition will provide a schema with a reference to an existing definition.
func (r *Reflector) refDefinition(_ Definitions, t reflect.Type) *Schema {
name := r.typeName(t)
return &Schema{
Ref: "#/$defs/" + name,
}
}
func (r *Reflector) lookupID(t reflect.Type) ID {
if r.Lookup != nil {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return r.Lookup(t)
}
return EmptyID
}
func (t *Schema) structKeywordsFromTags(f reflect.StructField, parent *Schema, propertyName string) {
t.Description = f.Tag.Get("jsonschema_description")
tags := splitOnUnescapedCommas(f.Tag.Get("jsonschema"))
t.genericKeywords(tags, parent, propertyName)
switch t.Type {
case "string":
t.stringKeywords(tags)
case "number":
t.numbericKeywords(tags)
case "integer":
t.numbericKeywords(tags)
case "array":
t.arrayKeywords(tags)
case "boolean":
t.booleanKeywords(tags)
}
extras := strings.Split(f.Tag.Get("jsonschema_extras"), ",")
t.extraKeywords(extras)
}
// read struct tags for generic keyworks
func (t *Schema) genericKeywords(tags []string, parent *Schema, propertyName string) {
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
if len(nameValue) == 2 {
name, val := nameValue[0], nameValue[1]
switch name {
case "title":
t.Title = val
case "description":
t.Description = val
case "type":
t.Type = val
case "anchor":
t.Anchor = val
case "oneof_required":
var typeFound *Schema
for i := range parent.OneOf {
if parent.OneOf[i].Title == nameValue[1] {
typeFound = parent.OneOf[i]
}
}
if typeFound == nil {
typeFound = &Schema{
Title: nameValue[1],
Required: []string{},
}
parent.OneOf = append(parent.OneOf, typeFound)
}
typeFound.Required = append(typeFound.Required, propertyName)
case "oneof_type":
if t.OneOf == nil {
t.OneOf = make([]*Schema, 0, 1)
}
t.Type = ""
types := strings.Split(nameValue[1], ";")
for _, ty := range types {
t.OneOf = append(t.OneOf, &Schema{
Type: ty,
})
}
case "enum":
switch t.Type {
case "string":
t.Enum = append(t.Enum, val)
case "integer":
i, _ := strconv.Atoi(val)
t.Enum = append(t.Enum, i)
case "number":
f, _ := strconv.ParseFloat(val, 64)
t.Enum = append(t.Enum, f)
}
}
}
}
}
// read struct tags for boolean type keyworks
func (t *Schema) booleanKeywords(tags []string) {
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
if len(nameValue) != 2 {
continue
}
name, val := nameValue[0], nameValue[1]
if name == "default" {
if val == "true" {
t.Default = true
} else if val == "false" {
t.Default = false
}
}
}
}
// read struct tags for string type keyworks
func (t *Schema) stringKeywords(tags []string) {
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
if len(nameValue) == 2 {
name, val := nameValue[0], nameValue[1]
switch name {
case "minLength":
i, _ := strconv.Atoi(val)
t.MinLength = i
case "maxLength":
i, _ := strconv.Atoi(val)
t.MaxLength = i
case "pattern":
t.Pattern = val
case "format":
switch val {
case "date-time", "email", "hostname", "ipv4", "ipv6", "uri", "uuid":
t.Format = val
break
}
case "readOnly":
i, _ := strconv.ParseBool(val)
t.ReadOnly = i
case "writeOnly":
i, _ := strconv.ParseBool(val)
t.WriteOnly = i
case "default":
t.Default = val
case "example":
t.Examples = append(t.Examples, val)
}
}
}
}
// read struct tags for numberic type keyworks
func (t *Schema) numbericKeywords(tags []string) {
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
if len(nameValue) == 2 {
name, val := nameValue[0], nameValue[1]
switch name {
case "multipleOf":
i, _ := strconv.Atoi(val)
t.MultipleOf = i
case "minimum":
i, _ := strconv.Atoi(val)
t.Minimum = i
case "maximum":
i, _ := strconv.Atoi(val)
t.Maximum = i
case "exclusiveMaximum":
b, _ := strconv.ParseBool(val)
t.ExclusiveMaximum = b
case "exclusiveMinimum":
b, _ := strconv.ParseBool(val)
t.ExclusiveMinimum = b
case "default":
i, _ := strconv.Atoi(val)
t.Default = i
case "example":
if i, err := strconv.Atoi(val); err == nil {
t.Examples = append(t.Examples, i)
}
}
}
}
}
// read struct tags for object type keyworks
// func (t *Type) objectKeywords(tags []string) {
// for _, tag := range tags{
// nameValue := strings.Split(tag, "=")
// name, val := nameValue[0], nameValue[1]
// switch name{
// case "dependencies":
// t.Dependencies = val
// break;
// case "patternProperties":
// t.PatternProperties = val
// break;
// }
// }
// }
// read struct tags for array type keyworks
func (t *Schema) arrayKeywords(tags []string) {
var defaultValues []interface{}
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
if len(nameValue) == 2 {
name, val := nameValue[0], nameValue[1]
switch name {
case "minItems":
i, _ := strconv.Atoi(val)
t.MinItems = i
case "maxItems":
i, _ := strconv.Atoi(val)
t.MaxItems = i
case "uniqueItems":
t.UniqueItems = true
case "default":
defaultValues = append(defaultValues, val)
case "enum":
switch t.Items.Type {
case "string":
t.Items.Enum = append(t.Items.Enum, val)
case "integer":
i, _ := strconv.Atoi(val)
t.Items.Enum = append(t.Items.Enum, i)
case "number":
f, _ := strconv.ParseFloat(val, 64)
t.Items.Enum = append(t.Items.Enum, f)
}
}
}
}
if len(defaultValues) > 0 {
t.Default = defaultValues
}
}
func (t *Schema) extraKeywords(tags []string) {
for _, tag := range tags {
nameValue := strings.Split(tag, "=")
if len(nameValue) == 2 {
t.setExtra(nameValue[0], nameValue[1])
}
}
}
func (t *Schema) setExtra(key, val string) {
if t.Extras == nil {
t.Extras = map[string]interface{}{}
}
if existingVal, ok := t.Extras[key]; ok {
switch existingVal := existingVal.(type) {
case string:
t.Extras[key] = []string{existingVal, val}
case []string:
t.Extras[key] = append(existingVal, val)
case int:
t.Extras[key], _ = strconv.Atoi(val)
}
} else {
switch key {
case "minimum":
t.Extras[key], _ = strconv.Atoi(val)
default:
t.Extras[key] = val
}
}
}
func requiredFromJSONTags(tags []string) bool {
if ignoredByJSONTags(tags) {
return false
}
for _, tag := range tags[1:] {
if tag == "omitempty" {
return false
}
}
return true
}
func requiredFromJSONSchemaTags(tags []string) bool {
if ignoredByJSONSchemaTags(tags) {
return false
}
for _, tag := range tags {
if tag == "required" {
return true
}
}
return false
}
func nullableFromJSONSchemaTags(tags []string) bool {
if ignoredByJSONSchemaTags(tags) {
return false
}
for _, tag := range tags {
if tag == "nullable" {
return true
}
}
return false
}
func inlineYAMLTags(tags []string) bool {
for _, tag := range tags {
if tag == "inline" {
return true
}
}
return false
}
func ignoredByJSONTags(tags []string) bool {
return tags[0] == "-"
}
func ignoredByJSONSchemaTags(tags []string) bool {
return tags[0] == "-"
}
func (r *Reflector) reflectFieldName(f reflect.StructField) (string, bool, bool, bool) {
jsonTags, exist := f.Tag.Lookup("json")
yamlTags, yamlExist := f.Tag.Lookup("yaml")
if !exist || r.PreferYAMLSchema {
jsonTags = yamlTags
exist = yamlExist
}
jsonTagsList := strings.Split(jsonTags, ",")
yamlTagsList := strings.Split(yamlTags, ",")
if ignoredByJSONTags(jsonTagsList) {
return "", false, false, false
}
jsonSchemaTags := strings.Split(f.Tag.Get("jsonschema"), ",")
if ignoredByJSONSchemaTags(jsonSchemaTags) {
return "", false, false, false
}
name := f.Name
required := requiredFromJSONTags(jsonTagsList)
if r.RequiredFromJSONSchemaTags {
required = requiredFromJSONSchemaTags(jsonSchemaTags)
}
nullable := nullableFromJSONSchemaTags(jsonSchemaTags)
if jsonTagsList[0] != "" {
name = jsonTagsList[0]
}
// field not anonymous and not export has no export name
if !f.Anonymous && f.PkgPath != "" {
name = ""
}
embed := false
// field anonymous but without json tag should be inherited by current type
if f.Anonymous && !exist {
if !r.YAMLEmbeddedStructs {
name = ""
embed = true
} else {
name = strings.ToLower(name)
}
}
if yamlExist && inlineYAMLTags(yamlTagsList) {
name = ""
embed = true
}
if r.KeyNamer != nil {
name = r.KeyNamer(name)
}
return name, embed, required, nullable
}
// UnmarshalJSON is used to parse a schema object or boolean.
func (t *Schema) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("true")) {
*t = *TrueSchema
return nil
} else if bytes.Equal(data, []byte("false")) {
*t = *FalseSchema
return nil
}
type Schema_ Schema
aux := &struct {
*Schema_
}{
Schema_: (*Schema_)(t),
}
return json.Unmarshal(data, aux)
}
func (t *Schema) MarshalJSON() ([]byte, error) {
if t.boolean != nil {
if *t.boolean {
return []byte("true"), nil
} else {
return []byte("false"), nil