Handle byte slices as type alias
David Halls authored 3 years ago
Alec Thomas committed 3 years ago
97 | 97 | "binaryEncoding": "base64" |
98 | 98 | } |
99 | 99 | }, |
100 | "photo2": { | |
101 | "type": "string", | |
102 | "media": { | |
103 | "binaryEncoding": "base64" | |
104 | } | |
105 | }, | |
100 | 106 | "feeling": { |
101 | 107 | "oneOf": [ |
102 | 108 | { |
97 | 97 | "binaryEncoding": "base64" |
98 | 98 | } |
99 | 99 | }, |
100 | "photo2": { | |
101 | "type": "string", | |
102 | "media": { | |
103 | "binaryEncoding": "base64" | |
104 | } | |
105 | }, | |
100 | 106 | "feeling": { |
101 | 107 | "oneOf": [ |
102 | 108 | { |
82 | 82 | "binaryEncoding": "base64" |
83 | 83 | } |
84 | 84 | }, |
85 | "photo2": { | |
86 | "type": "string", | |
87 | "media": { | |
88 | "binaryEncoding": "base64" | |
89 | } | |
90 | }, | |
85 | 91 | "feeling": { |
86 | 92 | "oneOf": [ |
87 | 93 | { |
97 | 97 | "binaryEncoding": "base64" |
98 | 98 | } |
99 | 99 | }, |
100 | "photo2": { | |
101 | "type": "string", | |
102 | "media": { | |
103 | "binaryEncoding": "base64" | |
104 | } | |
105 | }, | |
100 | 106 | "feeling": { |
101 | 107 | "oneOf": [ |
102 | 108 | { |
90 | 90 | "binaryEncoding": "base64" |
91 | 91 | } |
92 | 92 | }, |
93 | "photo2": { | |
94 | "type": "string", | |
95 | "media": { | |
96 | "binaryEncoding": "base64" | |
97 | } | |
98 | }, | |
93 | 99 | "feeling": { |
94 | 100 | "oneOf": [ |
95 | 101 | { |
89 | 89 | "binaryEncoding": "base64" |
90 | 90 | } |
91 | 91 | }, |
92 | "photo2": { | |
93 | "type": "string", | |
94 | "media": { | |
95 | "binaryEncoding": "base64" | |
96 | } | |
97 | }, | |
92 | 98 | "feeling": { |
93 | 99 | "oneOf": [ |
94 | 100 | { |
250 | 256 | "binaryEncoding": "base64" |
251 | 257 | } |
252 | 258 | }, |
259 | "photo2": { | |
260 | "type": "string", | |
261 | "media": { | |
262 | "binaryEncoding": "base64" | |
263 | } | |
264 | }, | |
253 | 265 | "feeling": { |
254 | 266 | "oneOf": [ |
255 | 267 | { |
89 | 89 | "binaryEncoding": "base64" |
90 | 90 | } |
91 | 91 | }, |
92 | "photo2": { | |
93 | "type": "string", | |
94 | "media": { | |
95 | "binaryEncoding": "base64" | |
96 | } | |
97 | }, | |
92 | 98 | "feeling": { |
93 | 99 | "oneOf": [ |
94 | 100 | { |
250 | 256 | "binaryEncoding": "base64" |
251 | 257 | } |
252 | 258 | }, |
259 | "photo2": { | |
260 | "type": "string", | |
261 | "media": { | |
262 | "binaryEncoding": "base64" | |
263 | } | |
264 | }, | |
253 | 265 | "feeling": { |
254 | 266 | "oneOf": [ |
255 | 267 | { |
18 | 18 | "SomeUntaggedBaseProperty", |
19 | 19 | "id", |
20 | 20 | "name", |
21 | "photo" | |
21 | "photo", | |
22 | "photo2" | |
22 | 23 | ], |
23 | 24 | "properties": { |
24 | 25 | "some_base_property": { |
89 | 90 | "binaryEncoding": "base64" |
90 | 91 | } |
91 | 92 | }, |
93 | "photo2": { | |
94 | "type": "string", | |
95 | "media": { | |
96 | "binaryEncoding": "base64" | |
97 | } | |
98 | }, | |
92 | 99 | "feeling": { |
93 | 100 | "oneOf": [ |
94 | 101 | { |
238 | 238 | returnType.MinItems = t.Len() |
239 | 239 | returnType.MaxItems = returnType.MinItems |
240 | 240 | } |
241 | switch t { | |
242 | case byteSliceType: | |
241 | if t.Kind() == reflect.Slice && t.Elem() == byteSliceType.Elem() { | |
243 | 242 | returnType.Type = "string" |
244 | 243 | returnType.Media = &Type{BinaryEncoding: "base64"} |
245 | 244 | return returnType |
246 | default: | |
247 | returnType.Type = "array" | |
248 | returnType.Items = r.reflectTypeToSchema(definitions, t.Elem()) | |
249 | return returnType | |
250 | } | |
245 | } | |
246 | returnType.Type = "array" | |
247 | returnType.Items = r.reflectTypeToSchema(definitions, t.Elem()) | |
248 | return returnType | |
251 | 249 | |
252 | 250 | case reflect.Interface: |
253 | 251 | return &Type{ |
68 | 68 | |
69 | 69 | // Tests for RFC draft-wright-json-schema-hyperschema-00, section 4 |
70 | 70 | Photo []byte `json:"photo,omitempty" jsonschema:"required"` |
71 | Photo2 Bytes `json:"photo2,omitempty" jsonschema:"required"` | |
71 | 72 | |
72 | 73 | // Tests for jsonpb enum support |
73 | 74 | Feeling ProtoEnum `json:"feeling,omitempty"` |
103 | 104 | Child3 interface{} `json:"child3" jsonschema:"oneof_required=group2,oneof_type=string;array"` |
104 | 105 | Child4 string `json:"child4" jsonschema:"oneof_required=group1"` |
105 | 106 | } |
107 | ||
108 | type Bytes []byte | |
106 | 109 | |
107 | 110 | func TestSchemaGeneration(t *testing.T) { |
108 | 111 | tests := []struct { |