|
4 | 4 | package requests |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
7 | 10 | "github.com/pb33f/libopenapi-validator/errors" |
8 | 11 | "github.com/pb33f/libopenapi-validator/helpers" |
9 | 12 | "github.com/pb33f/libopenapi-validator/paths" |
10 | 13 | "github.com/pb33f/libopenapi/datamodel/high/base" |
11 | 14 | v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
12 | 15 | "github.com/pb33f/libopenapi/utils" |
13 | | - "net/http" |
14 | | - "strings" |
15 | 16 | ) |
16 | 17 |
|
17 | 18 | func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool, []*errors.ValidationError) { |
18 | 19 |
|
19 | 20 | // find path |
20 | | - var pathItem *v3.PathItem |
21 | | - var errs []*errors.ValidationError |
| 21 | + var pathItem *v3.PathItem = v.pathItem |
22 | 22 | if v.pathItem == nil { |
23 | | - pathItem, errs, _ = paths.FindPath(request, v.document) |
24 | | - if pathItem == nil || errs != nil { |
25 | | - v.errors = errs |
26 | | - return false, errs |
| 23 | + var validationErrors []*errors.ValidationError |
| 24 | + pathItem, validationErrors, _ = paths.FindPath(request, v.document) |
| 25 | + if pathItem == nil || validationErrors != nil { |
| 26 | + v.errors = validationErrors |
| 27 | + return false, validationErrors |
27 | 28 | } |
28 | | - } else { |
29 | | - pathItem = v.pathItem |
30 | 29 | } |
31 | 30 |
|
32 | | - var validationErrors []*errors.ValidationError |
33 | 31 | operation := helpers.ExtractOperation(request, pathItem) |
| 32 | + if operation.RequestBody == nil { |
| 33 | + // TODO: check if requestBody is marked as required |
| 34 | + return true, nil |
| 35 | + } |
34 | 36 |
|
35 | | - var contentType string |
36 | 37 | // extract the content type from the request |
| 38 | + contentType := request.Header.Get(helpers.ContentTypeHeader) |
| 39 | + if contentType == "" { |
| 40 | + //TODO: should this ever return errors? |
| 41 | + return true, nil |
| 42 | + } |
37 | 43 |
|
38 | | - if contentType = request.Header.Get(helpers.ContentTypeHeader); contentType != "" { |
39 | | - |
40 | | - // extract the media type from the content type header. |
41 | | - ct, _, _ := helpers.ExtractContentType(contentType) |
42 | | - if operation.RequestBody != nil { |
43 | | - if mediaType, ok := operation.RequestBody.Content[ct]; ok { |
44 | | - |
45 | | - // we currently only support JSON validation for request bodies |
46 | | - // this will capture *everything* that contains some form of 'json' in the content type |
47 | | - if strings.Contains(strings.ToLower(contentType), helpers.JSONType) { |
48 | | - |
49 | | - // extract schema from media type |
50 | | - if mediaType.Schema != nil { |
51 | | - |
52 | | - var schema *base.Schema |
53 | | - var renderedInline, renderedJSON []byte |
54 | | - |
55 | | - // have we seen this schema before? let's hash it and check the cache. |
56 | | - hash := mediaType.GoLow().Schema.Value.Hash() |
57 | | - |
58 | | - // perform work only once and cache the result in the validator. |
59 | | - if cacheHit, ch := v.schemaCache[hash]; ch { |
60 | | - |
61 | | - // got a hit, use cached values |
62 | | - schema = cacheHit.schema |
63 | | - renderedInline = cacheHit.renderedInline |
64 | | - renderedJSON = cacheHit.renderedJSON |
65 | | - |
66 | | - } else { |
67 | | - |
68 | | - // render the schema inline and perform the intensive work of rendering and converting |
69 | | - // this is only performed once per schema and cached in the validator. |
70 | | - schema = mediaType.Schema.Schema() |
71 | | - renderedInline, _ = schema.RenderInline() |
72 | | - renderedJSON, _ = utils.ConvertYAMLtoJSON(renderedInline) |
73 | | - v.schemaCache[hash] = &schemaCache{ |
74 | | - schema: schema, |
75 | | - renderedInline: renderedInline, |
76 | | - renderedJSON: renderedJSON, |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - //render the schema, to be used for validation |
81 | | - valid, vErrs := ValidateRequestSchema(request, schema, renderedInline, renderedJSON) |
82 | | - if !valid { |
83 | | - validationErrors = append(validationErrors, vErrs...) |
84 | | - } |
85 | | - } |
86 | | - } |
87 | | - } else { |
88 | | - // content type not found in the contract |
89 | | - validationErrors = append(validationErrors, errors.RequestContentTypeNotFound(operation, request)) |
90 | | - } |
91 | | - } |
| 44 | + // extract the media type from the content type header. |
| 45 | + ct, _, _ := helpers.ExtractContentType(contentType) |
| 46 | + mediaType, ok := operation.RequestBody.Content[ct] |
| 47 | + if !ok { |
| 48 | + return false, []*errors.ValidationError{errors.RequestContentTypeNotFound(operation, request)} |
92 | 49 | } |
93 | | - if len(validationErrors) > 0 { |
94 | | - return false, validationErrors |
| 50 | + |
| 51 | + // we currently only support JSON validation for request bodies |
| 52 | + // this will capture *everything* that contains some form of 'json' in the content type |
| 53 | + if !strings.Contains(strings.ToLower(contentType), helpers.JSONType) { |
| 54 | + return true, nil |
95 | 55 | } |
96 | | - return true, nil |
| 56 | + |
| 57 | + // Nothing to validate |
| 58 | + if mediaType.Schema == nil { |
| 59 | + return true, nil |
| 60 | + } |
| 61 | + |
| 62 | + // extract schema from media type |
| 63 | + var schema *base.Schema |
| 64 | + var renderedInline, renderedJSON []byte |
| 65 | + |
| 66 | + // have we seen this schema before? let's hash it and check the cache. |
| 67 | + hash := mediaType.GoLow().Schema.Value.Hash() |
| 68 | + |
| 69 | + // perform work only once and cache the result in the validator. |
| 70 | + if cacheHit, ch := v.schemaCache[hash]; ch { |
| 71 | + // got a hit, use cached values |
| 72 | + schema = cacheHit.schema |
| 73 | + renderedInline = cacheHit.renderedInline |
| 74 | + renderedJSON = cacheHit.renderedJSON |
| 75 | + |
| 76 | + } else { |
| 77 | + |
| 78 | + // render the schema inline and perform the intensive work of rendering and converting |
| 79 | + // this is only performed once per schema and cached in the validator. |
| 80 | + schema = mediaType.Schema.Schema() |
| 81 | + renderedInline, _ = schema.RenderInline() |
| 82 | + renderedJSON, _ = utils.ConvertYAMLtoJSON(renderedInline) |
| 83 | + v.schemaCache[hash] = &schemaCache{ |
| 84 | + schema: schema, |
| 85 | + renderedInline: renderedInline, |
| 86 | + renderedJSON: renderedJSON, |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + //render the schema, to be used for validation |
| 91 | + return ValidateRequestSchema(request, schema, renderedInline, renderedJSON) |
97 | 92 | } |
0 commit comments