-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
626 lines (541 loc) · 19.3 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
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
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
av_opts "github.com/infobloxopen/protoc-gen-atlas-validate/options"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/pluginpb"
)
const (
metadataPkgPath = "google.golang.org/grpc/metadata"
gwruntimePkgPath = "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
runtimePkgPath = "github.com/infobloxopen/protoc-gen-atlas-validate/runtime"
)
type validateBuilder struct {
plugin *protogen.Plugin
methods map[string][]*methodDescriptor
packageName string
renderOnce bool
}
func main() {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
var request pluginpb.CodeGeneratorRequest
err = proto.Unmarshal(input, &request)
if err != nil {
panic(err)
}
opts := protogen.Options{}
plugin, err := opts.New(&request)
if err != nil {
panic(err)
}
builder := &validateBuilder{
methods: make(map[string][]*methodDescriptor),
plugin: plugin,
}
for _, protoFile := range plugin.Files {
methods := builder.gatherMethods(protoFile)
protoName := *protoFile.Proto.Name
builder.methods[protoName] = methods
}
resp := builder.generate(plugin)
out, err := proto.Marshal(resp)
if err != nil {
panic(err)
}
fmt.Fprint(os.Stdout, string(out))
}
func (b *validateBuilder) generate(plugin *protogen.Plugin) *pluginpb.CodeGeneratorResponse {
var lastG *protogen.GeneratedFile
for _, protoFile := range plugin.Files {
if !protoFile.Generate {
continue
}
fileName := protoFile.GeneratedFilenamePrefix + ".pb.atlas.validate.go"
g := b.plugin.NewGeneratedFile(fileName, ".")
lastG = g
g.P("package ", protoFile.GoPackageName)
b.packageName = string(protoFile.Desc.Package())
b.renderValidatorMethods(protoFile, g)
b.renderValidatorObjectMethods(protoFile, g)
if !b.renderOnce && sameName(b.packageName, *protoFile.Proto.Name) {
b.renderOnce = true
b.renderMethodDescriptors(g)
b.renderAnnotator(g)
}
}
if !b.renderOnce {
b.renderMethodDescriptors(lastG)
b.renderAnnotator(lastG)
}
return plugin.Response()
}
func sameName(packageName string, fileName string) bool {
fs := strings.Split(fileName, "/")
return strings.HasPrefix(fs[len(fs)-1], packageName+".")
}
func (b *validateBuilder) gatherMethods(file *protogen.File) []*methodDescriptor {
var methods []*methodDescriptor
for _, service := range file.Services {
for _, method := range service.Methods {
for i, opt := range extractHTTPOpts(method) {
methods = append(methods, &methodDescriptor{
inputTypeMessage: method.Input,
svc: string(service.Desc.Name()),
method: string(method.Desc.Name()),
idx: i,
httpBody: opt.body,
httpMethod: opt.method,
gwPattern: fmt.Sprintf("%s_%s_%d", service.Desc.Name(), method.Desc.Name(), i),
allowUnknown: b.getAllowUnknown(file.Desc.Options(), service.Desc.Options(), method.Desc.Options()),
})
}
}
}
return methods
}
type methodDescriptor struct {
inputTypeMessage *protogen.Message
svc string
method string
httpBody string
httpMethod string
gwPattern string
idx int
allowUnknown bool
}
type httpOpt struct {
body string
method string
}
func getHttpMethod(r *annotations.HttpRule) string {
switch r.GetPattern().(type) {
case *annotations.HttpRule_Get:
return "GET"
case *annotations.HttpRule_Post:
return "POST"
case *annotations.HttpRule_Put:
return "PUT"
case *annotations.HttpRule_Delete:
return "DELETE"
case *annotations.HttpRule_Patch:
return "PATCH"
}
return ""
}
func extractHTTPOpts(m *protogen.Method) []httpOpt {
r := []httpOpt{}
options := m.Desc.Options()
if options == nil {
return nil
}
if !proto.HasExtension(options, annotations.E_Http) {
return nil
}
ext := proto.GetExtension(options, annotations.E_Http)
if ext == nil {
return nil
}
if httpRule, ok := ext.(*annotations.HttpRule); ok {
r = append(r, httpOpt{
body: httpRule.Body,
method: getHttpMethod(httpRule),
})
for _, b := range httpRule.GetAdditionalBindings() {
r = append(r, httpOpt{
body: b.Body,
method: getHttpMethod(b),
})
}
} else {
return nil
}
return r
}
// getAllowUnknown function picks up correct allowUnknown option from file/service/method
// hierarchy.
func (b *validateBuilder) getAllowUnknown(file proto.Message, svc proto.Message, method proto.Message) bool {
var gavOpt *av_opts.AtlasValidateFileOption
v := proto.GetExtension(file, av_opts.E_File)
if v != nil {
gavOpt = v.(*av_opts.AtlasValidateFileOption)
}
var savOpt *av_opts.AtlasValidateServiceOption
v = proto.GetExtension(svc, av_opts.E_Service)
if v != nil {
savOpt = v.(*av_opts.AtlasValidateServiceOption)
}
var mavOpt *av_opts.AtlasValidateMethodOption
v = proto.GetExtension(method, av_opts.E_Method)
if v != nil {
mavOpt = v.(*av_opts.AtlasValidateMethodOption)
}
if mavOpt != nil {
return mavOpt.GetAllowUnknownFields()
} else if savOpt != nil {
return savOpt.GetAllowUnknownFields()
}
return gavOpt.GetAllowUnknownFields()
}
// renderValidatorMethods function generates entrypoints for validator one per each
// HTTP request (and HTTP request additional_bindings).
func (b *validateBuilder) renderValidatorMethods(protoFile *protogen.File, g *protogen.GeneratedFile) {
for _, m := range b.methods[*protoFile.Proto.Name] {
g.P(`// validate_`, m.gwPattern, ` is an entrypoint for validating "`, m.httpMethod, `" HTTP request `)
g.P(`// that match *.pb.gw.go/pattern_`, m.gwPattern, `.`)
g.P(`func validate_`, m.gwPattern, `(ctx `, generateImport("Context", "context", g), `, r `, generateImport("RawMessage", "encoding/json", g), `) (err error) {`)
if m.httpBody == "" {
g.P(`if len(r) != 0 {`)
g.P(`return `, generateImport("Errorf", "fmt", g), `("body is not allowed")`)
g.P(`}`)
g.P(`return nil`)
} else if b.isWKT(m.inputTypeMessage.Desc) {
g.P(`return nil`)
} else {
typeName := string(m.inputTypeMessage.Desc.Name())
msg := m.inputTypeMessage.Desc
goImportPath := string(m.inputTypeMessage.GoIdent.GoImportPath)
if m.httpBody != "*" {
for _, field := range m.inputTypeMessage.Fields {
if string(field.Desc.Name()) == m.httpBody {
typeName = string(field.Message.Desc.Name())
msg = field.Message.Desc
goImportPath = string(field.Message.GoIdent.GoImportPath)
break
}
}
}
if b.isLocal(msg) {
g.P(`return validate_Object_`, typeName, `(ctx, r, "")`)
} else {
nonLocalName := generateImport(typeName, goImportPath, g)
g.P(`if validator, ok := `, b.generateAtlasValidateJSONInterfaceSignature(nonLocalName, g), `; ok {`)
g.P(`return validator.AtlasValidateJSON(ctx, r, "")`)
g.P(`}`)
g.P(`return nil`)
}
}
g.P(`}`)
g.P()
}
}
func generateImport(name string, importPath string, g *protogen.GeneratedFile) string {
return g.QualifiedGoIdent(protogen.GoIdent{
GoName: name,
GoImportPath: protogen.GoImportPath(importPath),
})
}
func (b *validateBuilder) isLocal(msgType protoreflect.MessageDescriptor) bool {
return strings.HasPrefix(string(msgType.FullName()), b.packageName)
}
var wkt = map[string]bool{
// ptypes
"google.protobuf.Timestamp": true,
"google.protobuf.Duration": true,
"google.protobuf.Empty": true,
"google.protobuf.Any": true,
"google.protobuf.Struct": true,
// nillable values
"google.protobuf.StringValue": true,
"google.protobuf.BytesValue": true,
"google.protobuf.Int32Value": true,
"google.protobuf.UInt32Value": true,
"google.protobuf.Int64Value": true,
"google.protobuf.UInt64Value": true,
"google.protobuf.FloatValue": true,
"google.protobuf.DoubleValue": true,
"google.protobuf.BoolValue": true,
}
func (b *validateBuilder) isWKT(msgDesc protoreflect.MessageDescriptor) bool {
return wkt[string(msgDesc.FullName())]
}
func (b *validateBuilder) generateAtlasValidateJSONInterfaceSignature(t string, g *protogen.GeneratedFile) string {
return fmt.Sprintf(`interface{}(&%s{}).(interface{ AtlasValidateJSON(%s, %s, string) error })`,
t, generateImport("Context", "context", g), generateImport("RawMessage", "encoding/json", g))
}
func (b *validateBuilder) generateAtlasJSONValidateInterfaceSignature(t string, g *protogen.GeneratedFile) string {
rawMessage := generateImport("RawMessage", "encoding/json", g)
return fmt.Sprintf(`interface{}(&%s{}).(interface { AtlasJSONValidate(%s, %s, string) (%s, error) })`,
t, generateImport("Context", "context", g), rawMessage, rawMessage)
}
func (b *validateBuilder) renderValidatorObjectMethods(protoFile *protogen.File, g *protogen.GeneratedFile) {
for _, message := range protoFile.Messages {
b.renderValidatorObjectMethod(message, g)
b.generateValidateRequired(message, g)
for _, innerMessage := range message.Messages {
if innerMessage.Desc.IsMapEntry() {
continue
}
b.renderValidatorObjectMethod(innerMessage, g)
b.generateValidateRequired(innerMessage, g)
}
}
}
func (b *validateBuilder) renderValidatorObjectMethod(message *protogen.Message, g *protogen.GeneratedFile) {
msgName := message.GoIdent.GoName
g.P(`// validate_Object_`, msgName, ` function validates a JSON for a given object.`)
g.P(`func validate_Object_`, msgName, `(ctx `, generateImport("Context", "context", g), `, r `, generateImport("RawMessage", "encoding/json", g), `, path string) (err error) {`)
g.P(`if hook, ok := `, b.generateAtlasJSONValidateInterfaceSignature(msgName, g), `; ok {`)
g.P(`if r, err = hook.AtlasJSONValidate(ctx, r, path); err != nil {`)
g.P(`return err`)
g.P(`}`)
g.P(`}`)
g.P()
g.P(`var v map[string]`, generateImport("RawMessage", "encoding/json", g))
g.P(`if err = `, generateImport("Unmarshal", "encoding/json", g), `(r, &v); err != nil {`)
g.P(`return `, generateImport("Errorf", "fmt", g), `("invalid value for %q: expected object.", path)`)
g.P(`}`)
g.P()
g.P(`if err = validate_required_Object_`, msgName, `(ctx, v, path); err != nil {`)
g.P(`return err`)
g.P(`}`)
g.P()
g.P(`allowUnknown := `, generateImport("AllowUnknownFromContext", runtimePkgPath, g), `(ctx)`)
g.P()
g.P(`for k, _ := range v {`)
g.P(`switch k {`)
for _, f := range message.Fields {
g.P(`case "`, f.Desc.Name(), `":`)
if f.Desc.IsMap() {
continue
}
fExt := proto.GetExtension(f.Desc.Options(), av_opts.E_Field)
if fExt != nil {
favOpt := fExt.(*av_opts.AtlasValidateFieldOption)
methods := b.GetDeniedMethods(favOpt.GetDeny())
if len(methods) != 0 {
cond := strings.Join(methods, `" || method == "`)
g.P(`method := `, generateImport("HTTPMethodFromContext", runtimePkgPath, g), `(ctx)`)
g.P(`if method == "`, cond, `" {`)
g.P(`return `, generateImport("Errorf", "fmt", g), `("field %q is unsupported for %q operation.", k, method)`)
g.P("}")
}
}
if f.Message != nil && f.Desc.IsList() {
g.P(`if v[k] == nil {`)
g.P(`continue`)
g.P(`}`)
g.P(`var vArr []`, generateImport("RawMessage", "encoding/json", g))
g.P(`vArrPath := `, generateImport("JoinPath", runtimePkgPath, g), `(path, k)`)
g.P(`if err = `, generateImport("Unmarshal", "encoding/json", g), `(v[k], &vArr); err != nil {`)
g.P(`return `, generateImport("Errorf", "fmt", g), `("invalid value for %q: expected array.", vArrPath)`)
g.P(`}`)
msg := f.Desc.Message()
msgName := f.Message.GoIdent.GoName
if b.isWKT(msg) {
continue
}
if !b.isLocal(msg) {
nonLocalName := generateImport(msgName, string(f.Message.GoIdent.GoImportPath), g)
g.P(`validator, ok := `, b.generateAtlasValidateJSONInterfaceSignature(nonLocalName, g))
g.P(`if !ok {`)
g.P(`continue`)
g.P(`}`)
}
g.P(`for i, vv := range vArr {`)
g.P(`vvPath := `, generateImport("Sprintf", "fmt", g), `("%s.[%d]", vArrPath, i)`)
if b.isLocal(msg) {
g.P(`if err = validate_Object_`, msgName, `(ctx, vv, vvPath); err != nil {`)
g.P(`return err`)
g.P(`}`)
} else {
g.P(`if err = validator.AtlasValidateJSON(ctx, vv, vvPath); err != nil {`)
g.P(`return err`)
g.P(`}`)
}
g.P(`}`)
} else if f.Message != nil {
msg := f.Desc.Message()
msgName := f.Message.GoIdent.GoName
if b.isWKT(msg) {
continue
}
g.P(`if v[k] == nil {`)
g.P(`continue`)
g.P(`}`)
g.P(`vv := v[k]`)
g.P(`vvPath := `, generateImport("JoinPath", runtimePkgPath, g), `(path, k)`)
if b.isLocal(msg) {
g.P(`if err = validate_Object_`, msgName, `(ctx, vv, vvPath); err != nil {`)
g.P(`return err`)
g.P(`}`)
} else {
nonLocalName := generateImport(msgName, string(f.Message.GoIdent.GoImportPath), g)
g.P(`validator, ok := `, b.generateAtlasValidateJSONInterfaceSignature(nonLocalName, g))
g.P(`if !ok {`)
g.P(`continue`)
g.P(`}`)
g.P(`if err = validator.AtlasValidateJSON(ctx, vv, vvPath); err != nil {`)
g.P(`return err`)
g.P(`}`)
}
}
}
g.P(`default:`)
g.P(`if !allowUnknown {`)
g.P(`return `, generateImport("Errorf", "fmt", g), `("unknown field %q.", `, generateImport("JoinPath", runtimePkgPath, g), `(path, k))`)
g.P(`}`)
g.P(`}`)
g.P(`}`)
g.P(`return nil`)
g.P(`}`)
g.P()
g.P(`// AtlasValidateJSON function validates a JSON for object `, msgName, `.`)
g.P(`func (_ *`, msgName, `) AtlasValidateJSON(ctx `, generateImport("Context", "context", g), `, r `, generateImport("RawMessage", "encoding/json", g), `, path string) (err error) {`)
g.P(`if hook, ok := `, b.generateAtlasJSONValidateInterfaceSignature(msgName, g), `; ok {`)
g.P(`if r, err = hook.AtlasJSONValidate(ctx, r, path); err != nil {`)
g.P(`return err`)
g.P(`}`)
g.P(`}`)
g.P(`return validate_Object_`, msgName, `(ctx, r, path)`)
g.P(`}`)
g.P()
}
//Return methods to which field marked as denied
func (b *validateBuilder) GetDeniedMethods(options []av_opts.AtlasValidateFieldOption_Operation) []string {
httpMethods := make(map[string]struct{})
for _, op := range options {
switch op {
case av_opts.AtlasValidateFieldOption_create:
httpMethods["POST"] = struct{}{}
case av_opts.AtlasValidateFieldOption_update:
httpMethods["PATCH"] = struct{}{}
case av_opts.AtlasValidateFieldOption_replace:
httpMethods["PUT"] = struct{}{}
}
}
uniqueMethods := make([]string, 0)
for m := range httpMethods {
uniqueMethods = append(uniqueMethods, m)
}
sort.StringSlice(uniqueMethods).Sort()
return uniqueMethods
}
//Return methods to which field marked as required
func (b *validateBuilder) GetRequiredMethods(options []av_opts.AtlasValidateFieldOption_Operation) []string {
requiredMethods := make(map[string]struct{})
for _, op := range options {
switch op {
case av_opts.AtlasValidateFieldOption_create:
requiredMethods["POST"] = struct{}{}
case av_opts.AtlasValidateFieldOption_update:
requiredMethods["PATCH"] = struct{}{}
case av_opts.AtlasValidateFieldOption_replace:
requiredMethods["PUT"] = struct{}{}
}
}
uniqueMethods := make([]string, 0)
for m := range requiredMethods {
uniqueMethods = append(uniqueMethods, m)
}
sort.StringSlice(uniqueMethods).Sort()
return uniqueMethods
}
func (b *validateBuilder) generateValidateRequired(message *protogen.Message, g *protogen.GeneratedFile) {
requiredFields := make(map[string][]string)
msgName := message.GoIdent.GoName
for _, f := range message.Fields {
fExt := proto.GetExtension(f.Desc.Options(), av_opts.E_Field)
if fExt != nil {
favOpt := fExt.(*av_opts.AtlasValidateFieldOption)
methods := b.GetRequiredMethods(favOpt.GetRequired())
if len(methods) == 0 {
continue
}
requiredFields[string(f.Desc.Name())] = methods
}
}
g.P(`func validate_required_Object_`, msgName, `(ctx `, generateImport("Context", "context", g), `, v map[string]`, generateImport("RawMessage", "encoding/json", g), `, path string) error {`)
g.P(`method := `, generateImport("HTTPMethodFromContext", runtimePkgPath, g), `(ctx)`)
g.P(`_ = method`)
var fields []string
for v := range requiredFields {
fields = append(fields, v)
}
sort.StringSlice(fields).Sort()
for _, field := range fields {
methods := requiredFields[field]
if len(methods) == 3 {
g.P(`if _, ok := v["`, field, `"]; !ok {`)
g.P(`path = `, generateImport("JoinPath", runtimePkgPath, g), `(path, "`, field, `")`)
g.P(`return `, generateImport("Errorf", "fmt", g), `("field %q is required for %q operation.", path, method)`)
g.P(`}`)
} else {
cond := strings.Join(methods, `" || method == "`)
g.P(`if _, ok := v["`, field, `"]; !ok && (method == "`, cond, `") {`)
g.P(`path = `, generateImport("JoinPath", runtimePkgPath, g), `(path, "`, field, `")`)
g.P(`return `, generateImport("Errorf", "fmt", g), `("field %q is required for %q operation.", path, method)`)
g.P(`}`)
}
}
g.P(`return nil`)
g.P(`}`)
}
// renderMethodDescriptors renders array of structs that are used to trigger validation
// function on correct HTTP request according to HTTP method and grpc-gateway/runtime.Pattern.
func (b *validateBuilder) renderMethodDescriptors(g *protogen.GeneratedFile) {
g.P(`var validate_Patterns = []struct{`)
g.P(`pattern `, generateImport("Pattern", gwruntimePkgPath, g))
g.P(`httpMethod string`)
g.P(`validator func(`, generateImport("Context", "context", g), `, `, generateImport("RawMessage", "encoding/json", g), `) error`)
g.P(`// Included for introspection purpose.`)
g.P(`allowUnknown bool`)
g.P(`} {`)
var files []string
for f := range b.methods {
files = append(files, f)
}
sort.StringSlice(files).Sort()
for _, f := range files {
methods := b.methods[f]
g.P(`// patterns for file `, f)
for _, m := range methods {
g.P(`{`)
// NOTE: pattern reiles on code generated by protoc-gen-grpc-gateway.
g.P(`pattern: `, "pattern_"+m.gwPattern, `,`)
g.P(`httpMethod: "`, m.httpMethod, `",`)
g.P(`validator: `, "validate_"+m.gwPattern, `,`)
g.P(`allowUnknown: `, m.allowUnknown, `,`)
g.P(`},`)
}
g.P()
}
g.P(`}`)
g.P()
}
func (b *validateBuilder) renderAnnotator(g *protogen.GeneratedFile) {
g.P(`// AtlasValidateAnnotator parses JSON input and validates unknown fields`)
g.P(`// based on 'allow_unknown_fields' options specified in proto file.`)
g.P(`func AtlasValidateAnnotator(ctx `, generateImport("Context", "context", g), `, r *`,
generateImport("Request", "net/http", g), `) `, generateImport("MD", metadataPkgPath, g), ` {`)
g.P(`md := make(`, generateImport("MD", metadataPkgPath, g), `)`)
g.P(`for _, v := range validate_Patterns {`)
g.P(`if r.Method == v.httpMethod && `, generateImport("PatternMatch", runtimePkgPath, g), `(v.pattern, r.URL.Path) {`)
g.P(`var b []byte`)
g.P(`var err error`)
g.P(`if b, err = `, generateImport("ReadAll", "io/ioutil", g), `(r.Body); err != nil {`)
g.P(`md.Set("Atlas-Validation-Error", "invalid value: unable to parse body")`)
g.P(`return md`)
g.P(`}`)
g.P(`r.Body = `, generateImport("NopCloser", "io/ioutil", g), `(`, generateImport("NewReader", "bytes", g), `(b))`)
g.P(`ctx := `, generateImport("WithValue", "context", g), `(`, generateImport("WithValue", "context", g), `(`, generateImport("Background", "context", g), `(), `,
generateImport("HTTPMethodContextKey", runtimePkgPath, g), `, r.Method), `, generateImport("AllowUnknownContextKey", runtimePkgPath, g), `, v.allowUnknown)`)
g.P(`if err = v.validator(ctx, b); err != nil {`)
g.P(`md.Set("Atlas-Validation-Error", err.Error())`)
g.P(`}`)
g.P(`break`)
g.P(`}`)
g.P(`}`)
g.P(`return md`)
g.P(`}`)
g.P()
}