forked from chrusty/protoc-gen-jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
582 lines (517 loc) · 17.8 KB
/
main.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
// protoc plugin which converts .proto to JSON schema
// It is spawned by protoc and generates JSON-schema files.
// "Heavily influenced" by Google's "protog-gen-bq-schema"
//
// usage:
// $ bin/protoc --jsonschema_out=path/to/outdir foo.proto
//
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/alecthomas/jsonschema"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/xeipuuv/gojsonschema"
)
const (
LOG_DEBUG = 0
LOG_INFO = 1
LOG_WARN = 2
LOG_ERROR = 3
LOG_FATAL = 4
LOG_PANIC = 5
)
var (
allowNullValues bool = false
disallowAdditionalProperties bool = false
disallowBigIntsAsStrings bool = false
debugLogging bool = false
globalPkg = &ProtoPackage{
name: "",
parent: nil,
children: make(map[string]*ProtoPackage),
types: make(map[string]*descriptor.DescriptorProto),
}
logLevels = map[LogLevel]string{
0: "DEBUG",
1: "INFO",
2: "WARN",
3: "ERROR",
4: "FATAL",
5: "PANIC",
}
)
// ProtoPackage describes a package of Protobuf, which is an container of message types.
type ProtoPackage struct {
name string
parent *ProtoPackage
children map[string]*ProtoPackage
types map[string]*descriptor.DescriptorProto
}
type LogLevel int
func init() {
flag.BoolVar(&allowNullValues, "allow_null_values", false, "Allow NULL values to be validated")
flag.BoolVar(&disallowAdditionalProperties, "disallow_additional_properties", false, "Disallow additional properties")
flag.BoolVar(&disallowBigIntsAsStrings, "disallow_bigints_as_strings", false, "Disallow bigints to be strings (eg scientific notation)")
flag.BoolVar(&debugLogging, "debug", false, "Log debug messages")
}
func logWithLevel(logLevel LogLevel, logFormat string, logParams ...interface{}) {
// If we're not doing debug logging then just return:
if logLevel <= LOG_INFO && !debugLogging {
return
}
// Otherwise log:
logMessage := fmt.Sprintf(logFormat, logParams...)
log.Printf(fmt.Sprintf("[%v] %v", logLevels[logLevel], logMessage))
}
func registerType(pkgName *string, msg *descriptor.DescriptorProto) {
pkg := globalPkg
if pkgName != nil {
for _, node := range strings.Split(*pkgName, ".") {
if pkg == globalPkg && node == "" {
// Skips leading "."
continue
}
child, ok := pkg.children[node]
if !ok {
child = &ProtoPackage{
name: pkg.name + "." + node,
parent: pkg,
children: make(map[string]*ProtoPackage),
types: make(map[string]*descriptor.DescriptorProto),
}
pkg.children[node] = child
}
pkg = child
}
}
pkg.types[msg.GetName()] = msg
}
func (pkg *ProtoPackage) lookupType(name string) (*descriptor.DescriptorProto, bool) {
if strings.HasPrefix(name, ".") {
return globalPkg.relativelyLookupType(name[1:len(name)])
}
for ; pkg != nil; pkg = pkg.parent {
if desc, ok := pkg.relativelyLookupType(name); ok {
return desc, ok
}
}
return nil, false
}
func relativelyLookupNestedType(desc *descriptor.DescriptorProto, name string) (*descriptor.DescriptorProto, bool) {
components := strings.Split(name, ".")
componentLoop:
for _, component := range components {
for _, nested := range desc.GetNestedType() {
if nested.GetName() == component {
desc = nested
continue componentLoop
}
}
logWithLevel(LOG_INFO, "no such nested message %s in %s", component, desc.GetName())
return nil, false
}
return desc, true
}
func (pkg *ProtoPackage) relativelyLookupType(name string) (*descriptor.DescriptorProto, bool) {
components := strings.SplitN(name, ".", 2)
switch len(components) {
case 0:
logWithLevel(LOG_DEBUG, "empty message name")
return nil, false
case 1:
found, ok := pkg.types[components[0]]
return found, ok
case 2:
logWithLevel(LOG_DEBUG, "looking for %s in %s at %s (%v)", components[1], components[0], pkg.name, pkg)
if child, ok := pkg.children[components[0]]; ok {
found, ok := child.relativelyLookupType(components[1])
return found, ok
}
if msg, ok := pkg.types[components[0]]; ok {
found, ok := relativelyLookupNestedType(msg, components[1])
return found, ok
}
logWithLevel(LOG_INFO, "no such package nor message %s in %s", components[0], pkg.name)
return nil, false
default:
logWithLevel(LOG_FATAL, "not reached")
return nil, false
}
}
func (pkg *ProtoPackage) relativelyLookupPackage(name string) (*ProtoPackage, bool) {
components := strings.Split(name, ".")
for _, c := range components {
var ok bool
pkg, ok = pkg.children[c]
if !ok {
return nil, false
}
}
return pkg, true
}
// Convert a proto "field" (essentially a type-switch with some recursion):
func convertField(curPkg *ProtoPackage, desc *descriptor.FieldDescriptorProto, msg *descriptor.DescriptorProto) (*jsonschema.Type, error) {
// Prepare a new jsonschema.Type for our eventual return value:
jsonSchemaType := &jsonschema.Type{
Properties: make(map[string]*jsonschema.Type),
}
// Switch the types, and pick a JSONSchema equivalent:
switch desc.GetType() {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
descriptor.FieldDescriptorProto_TYPE_FLOAT:
if allowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_NULL},
{Type: gojsonschema.TYPE_NUMBER},
}
} else {
jsonSchemaType.Type = gojsonschema.TYPE_NUMBER
}
case descriptor.FieldDescriptorProto_TYPE_INT32,
descriptor.FieldDescriptorProto_TYPE_UINT32,
descriptor.FieldDescriptorProto_TYPE_FIXED32,
descriptor.FieldDescriptorProto_TYPE_SFIXED32,
descriptor.FieldDescriptorProto_TYPE_SINT32:
if allowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_NULL},
{Type: gojsonschema.TYPE_INTEGER},
}
} else {
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
}
case descriptor.FieldDescriptorProto_TYPE_INT64,
descriptor.FieldDescriptorProto_TYPE_UINT64,
descriptor.FieldDescriptorProto_TYPE_FIXED64,
descriptor.FieldDescriptorProto_TYPE_SFIXED64,
descriptor.FieldDescriptorProto_TYPE_SINT64:
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: gojsonschema.TYPE_INTEGER})
if !disallowBigIntsAsStrings {
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: gojsonschema.TYPE_STRING})
}
if allowNullValues {
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: gojsonschema.TYPE_NULL})
}
case descriptor.FieldDescriptorProto_TYPE_STRING,
descriptor.FieldDescriptorProto_TYPE_BYTES:
if allowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_NULL},
{Type: gojsonschema.TYPE_STRING},
}
} else {
jsonSchemaType.Type = gojsonschema.TYPE_STRING
}
case descriptor.FieldDescriptorProto_TYPE_ENUM:
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: gojsonschema.TYPE_STRING})
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: gojsonschema.TYPE_INTEGER})
if allowNullValues {
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: gojsonschema.TYPE_NULL})
}
// Go through all the enums we have, see if we can match any to this field by name:
for _, enumDescriptor := range msg.GetEnumType() {
// Each one has several values:
for _, enumValue := range enumDescriptor.Value {
// Figure out the entire name of this field:
fullFieldName := fmt.Sprintf(".%v.%v", *msg.Name, *enumDescriptor.Name)
// If we find ENUM values for this field then put them into the JSONSchema list of allowed ENUM values:
if strings.HasSuffix(desc.GetTypeName(), fullFieldName) {
jsonSchemaType.Enum = append(jsonSchemaType.Enum, enumValue.Name)
jsonSchemaType.Enum = append(jsonSchemaType.Enum, enumValue.Number)
}
}
}
case descriptor.FieldDescriptorProto_TYPE_BOOL:
if allowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_NULL},
{Type: gojsonschema.TYPE_BOOLEAN},
}
} else {
jsonSchemaType.Type = gojsonschema.TYPE_BOOLEAN
}
case descriptor.FieldDescriptorProto_TYPE_GROUP,
descriptor.FieldDescriptorProto_TYPE_MESSAGE:
switch desc.GetTypeName() {
case ".google.protobuf.Timestamp":
jsonSchemaType.Type = gojsonschema.TYPE_STRING
jsonSchemaType.Format = "date-time"
default:
jsonSchemaType.Type = gojsonschema.TYPE_OBJECT
if desc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_OPTIONAL {
jsonSchemaType.AdditionalProperties = []byte("true")
}
if desc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REQUIRED {
jsonSchemaType.AdditionalProperties = []byte("false")
}
}
default:
return nil, fmt.Errorf("unrecognized field type: %s", desc.GetType().String())
}
// Recurse array of primitive types:
if desc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REPEATED && jsonSchemaType.Type != gojsonschema.TYPE_OBJECT {
jsonSchemaType.Items = &jsonschema.Type{}
if len(jsonSchemaType.Enum) > 0 {
jsonSchemaType.Items.Enum = jsonSchemaType.Enum
jsonSchemaType.Enum = nil
jsonSchemaType.Items.OneOf = nil
} else {
jsonSchemaType.Items.Type = jsonSchemaType.Type
jsonSchemaType.Items.OneOf = jsonSchemaType.OneOf
}
if allowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_NULL},
{Type: gojsonschema.TYPE_ARRAY},
}
} else {
jsonSchemaType.Type = gojsonschema.TYPE_ARRAY
jsonSchemaType.OneOf = []*jsonschema.Type{}
}
return jsonSchemaType, nil
}
// Recurse nested objects / arrays of objects (if necessary):
if jsonSchemaType.Type == gojsonschema.TYPE_OBJECT {
recordType, ok := curPkg.lookupType(desc.GetTypeName())
if !ok {
return nil, fmt.Errorf("no such message type named %s", desc.GetTypeName())
}
// Recurse:
recursedJSONSchemaType, err := convertMessageType(curPkg, recordType)
if err != nil {
return nil, err
}
// The result is stored differently for arrays of objects (they become "items"):
if desc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REPEATED {
jsonSchemaType.Items = &recursedJSONSchemaType
jsonSchemaType.Type = gojsonschema.TYPE_ARRAY
} else {
// Nested objects are more straight-forward:
jsonSchemaType.Properties = recursedJSONSchemaType.Properties
}
// Optionally allow NULL values:
if allowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_NULL},
{Type: jsonSchemaType.Type},
}
jsonSchemaType.Type = ""
}
}
return jsonSchemaType, nil
}
// Converts a proto "MESSAGE" into a JSON-Schema:
func convertMessageType(curPkg *ProtoPackage, msg *descriptor.DescriptorProto) (jsonschema.Type, error) {
// Prepare a new jsonschema:
jsonSchemaType := jsonschema.Type{
Properties: make(map[string]*jsonschema.Type),
Version: jsonschema.Version,
}
// Optionally allow NULL values:
if allowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_NULL},
{Type: gojsonschema.TYPE_OBJECT},
}
} else {
jsonSchemaType.Type = gojsonschema.TYPE_OBJECT
}
// disallowAdditionalProperties will prevent validation where extra fields are found (outside of the schema):
if disallowAdditionalProperties {
jsonSchemaType.AdditionalProperties = []byte("false")
} else {
jsonSchemaType.AdditionalProperties = []byte("true")
}
logWithLevel(LOG_DEBUG, "Converting message: %s", proto.MarshalTextString(msg))
for _, fieldDesc := range msg.GetField() {
recursedJSONSchemaType, err := convertField(curPkg, fieldDesc, msg)
if err != nil {
logWithLevel(LOG_ERROR, "Failed to convert field %s in %s: %v", fieldDesc.GetName(), msg.GetName(), err)
return jsonSchemaType, err
}
jsonSchemaType.Properties[fieldDesc.GetName()] = recursedJSONSchemaType
}
return jsonSchemaType, nil
}
// Converts a proto "ENUM" into a JSON-Schema:
func convertEnumType(enum *descriptor.EnumDescriptorProto) (jsonschema.Type, error) {
// Prepare a new jsonschema.Type for our eventual return value:
jsonSchemaType := jsonschema.Type{
Version: jsonschema.Version,
}
// Allow both strings and integers:
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: "string"})
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: "integer"})
// Add the allowed values:
for _, enumValue := range enum.Value {
jsonSchemaType.Enum = append(jsonSchemaType.Enum, enumValue.Name)
jsonSchemaType.Enum = append(jsonSchemaType.Enum, enumValue.Number)
}
return jsonSchemaType, nil
}
// Converts a proto file into a JSON-Schema:
func convertFile(file *descriptor.FileDescriptorProto) ([]*plugin.CodeGeneratorResponse_File, error) {
// Input filename:
protoFileName := path.Base(file.GetName())
// Prepare a list of responses:
response := []*plugin.CodeGeneratorResponse_File{}
// Warn about multiple messages / enums in files:
if len(file.GetMessageType()) > 1 {
logWithLevel(LOG_WARN, "protoc-gen-jsonschema will create multiple MESSAGE schemas (%d) from one proto file (%v)", len(file.GetMessageType()), protoFileName)
}
if len(file.GetEnumType()) > 1 {
logWithLevel(LOG_WARN, "protoc-gen-jsonschema will create multiple ENUM schemas (%d) from one proto file (%v)", len(file.GetEnumType()), protoFileName)
}
// Generate standalone ENUMs:
if len(file.GetMessageType()) == 0 {
for _, enum := range file.GetEnumType() {
jsonSchemaFileName := fmt.Sprintf("%s.jsonschema", enum.GetName())
logWithLevel(LOG_INFO, "Generating JSON-schema for stand-alone ENUM (%v) in file [%v] => %v", enum.GetName(), protoFileName, jsonSchemaFileName)
enumJsonSchema, err := convertEnumType(enum)
if err != nil {
logWithLevel(LOG_ERROR, "Failed to convert %s: %v", protoFileName, err)
return nil, err
} else {
// Marshal the JSON-Schema into JSON:
jsonSchemaJSON, err := json.MarshalIndent(enumJsonSchema, "", " ")
if err != nil {
logWithLevel(LOG_ERROR, "Failed to encode jsonSchema: %v", err)
return nil, err
} else {
// Add a response:
resFile := &plugin.CodeGeneratorResponse_File{
Name: proto.String(jsonSchemaFileName),
Content: proto.String(string(jsonSchemaJSON)),
}
response = append(response, resFile)
}
}
}
} else {
// Otherwise process MESSAGES (packages):
pkg, ok := globalPkg.relativelyLookupPackage(file.GetPackage())
if !ok {
return nil, fmt.Errorf("no such package found: %s", file.GetPackage())
}
for _, msg := range file.GetMessageType() {
jsonSchemaFileName := fmt.Sprintf("%s.jsonschema", msg.GetName())
logWithLevel(LOG_INFO, "Generating JSON-schema for MESSAGE (%v) in file [%v] => %v", msg.GetName(), protoFileName, jsonSchemaFileName)
messageJSONSchema, err := convertMessageType(pkg, msg)
if err != nil {
logWithLevel(LOG_ERROR, "Failed to convert %s: %v", protoFileName, err)
return nil, err
} else {
// Marshal the JSON-Schema into JSON:
jsonSchemaJSON, err := json.MarshalIndent(messageJSONSchema, "", " ")
if err != nil {
logWithLevel(LOG_ERROR, "Failed to encode jsonSchema: %v", err)
return nil, err
} else {
// Add a response:
resFile := &plugin.CodeGeneratorResponse_File{
Name: proto.String(jsonSchemaFileName),
Content: proto.String(string(jsonSchemaJSON)),
}
response = append(response, resFile)
}
}
}
}
return response, nil
}
func convert(req *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, error) {
generateTargets := make(map[string]bool)
for _, file := range req.GetFileToGenerate() {
generateTargets[file] = true
}
res := &plugin.CodeGeneratorResponse{}
for _, file := range req.GetProtoFile() {
for _, msg := range file.GetMessageType() {
logWithLevel(LOG_DEBUG, "Loading a message type %s from package %s", msg.GetName(), file.GetPackage())
registerType(file.Package, msg)
}
}
for _, file := range req.GetProtoFile() {
if _, ok := generateTargets[file.GetName()]; ok {
logWithLevel(LOG_DEBUG, "Converting file (%v)", file.GetName())
converted, err := convertFile(file)
if err != nil {
res.Error = proto.String(fmt.Sprintf("Failed to convert %s: %v", file.GetName(), err))
return res, err
}
res.File = append(res.File, converted...)
}
}
return res, nil
}
func convertFrom(rd io.Reader) (*plugin.CodeGeneratorResponse, error) {
logWithLevel(LOG_DEBUG, "Reading code generation request")
input, err := ioutil.ReadAll(rd)
if err != nil {
logWithLevel(LOG_ERROR, "Failed to read request: %v", err)
return nil, err
}
req := &plugin.CodeGeneratorRequest{}
err = proto.Unmarshal(input, req)
if err != nil {
logWithLevel(LOG_ERROR, "Can't unmarshal input: %v", err)
return nil, err
}
commandLineParameter(req.GetParameter())
logWithLevel(LOG_DEBUG, "Converting input")
return convert(req)
}
func commandLineParameter(parameters string) {
for _, parameter := range strings.Split(parameters, ",") {
switch parameter {
case "allow_null_values":
allowNullValues = true
case "debug":
debugLogging = true
case "disallow_additional_properties":
disallowAdditionalProperties = true
case "disallow_bigints_as_strings":
disallowBigIntsAsStrings = true
}
}
}
func main() {
flag.Parse()
ok := true
logWithLevel(LOG_DEBUG, "Processing code generator request")
res, err := convertFrom(os.Stdin)
if err != nil {
ok = false
if res == nil {
message := fmt.Sprintf("Failed to read input: %v", err)
res = &plugin.CodeGeneratorResponse{
Error: &message,
}
}
}
logWithLevel(LOG_DEBUG, "Serializing code generator response")
data, err := proto.Marshal(res)
if err != nil {
logWithLevel(LOG_FATAL, "Cannot marshal response: %v", err)
}
_, err = os.Stdout.Write(data)
if err != nil {
logWithLevel(LOG_FATAL, "Failed to write response: %v", err)
}
if ok {
logWithLevel(LOG_DEBUG, "Succeeded to process code generator request")
} else {
logWithLevel(LOG_WARN, "Failed to process code generator but successfully sent the error to protoc")
os.Exit(1)
}
}