| 1 |
// Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package errors |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"net/http" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/pb33f/libopenapi/orderedmap" |
| 12 |
|
| 13 |
v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 14 |
|
| 15 |
"github.com/pb33f/libopenapi-validator/helpers" |
| 16 |
) |
| 17 |
|
| 18 |
func RequestContentTypeNotFound(op *v3.Operation, request *http.Request, specPath string) *ValidationError { |
| 19 |
ct := request.Header.Get(helpers.ContentTypeHeader) |
| 20 |
var ctypes []string |
| 21 |
var contentMap *orderedmap.Map[string, *v3.MediaType] |
| 22 |
specLine, specCol := 1, 0 |
| 23 |
if op.RequestBody != nil { |
| 24 |
contentMap = op.RequestBody.Content |
| 25 |
for pair := orderedmap.First(op.RequestBody.Content); pair != nil; pair = pair.Next() { |
| 26 |
ctypes = append(ctypes, pair.Key()) |
| 27 |
} |
| 28 |
if low := op.RequestBody.GoLow(); low != nil && low.Content.KeyNode != nil { |
| 29 |
specLine = low.Content.KeyNode.Line |
| 30 |
specCol = low.Content.KeyNode.Column |
| 31 |
} |
| 32 |
} |
| 33 |
return &ValidationError{ |
| 34 |
ValidationType: helpers.RequestBodyValidation, |
| 35 |
ValidationSubType: helpers.RequestBodyContentType, |
| 36 |
Message: fmt.Sprintf("%s operation request content type '%s' does not exist", |
| 37 |
request.Method, ct), |
| 38 |
Reason: fmt.Sprintf("The content type '%s' of the %s request submitted has not "+ |
| 39 |
"been defined, it's an unknown type", ct, request.Method), |
| 40 |
SpecLine: specLine, |
| 41 |
SpecCol: specCol, |
| 42 |
Context: op, |
| 43 |
HowToFix: fmt.Sprintf(HowToFixInvalidContentType, orderedmap.Len(contentMap), strings.Join(ctypes, ", ")), |
| 44 |
RequestPath: request.URL.Path, |
| 45 |
RequestMethod: request.Method, |
| 46 |
SpecPath: specPath, |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func OperationNotFound(pathItem *v3.PathItem, request *http.Request, method string, specPath string) *ValidationError { |
| 51 |
specLine, specCol := 1, 0 |
| 52 |
if low := pathItem.GoLow(); low != nil && low.KeyNode != nil { |
| 53 |
specLine = low.KeyNode.Line |
| 54 |
specCol = low.KeyNode.Column |
| 55 |
} |
| 56 |
return &ValidationError{ |
| 57 |
ValidationType: helpers.RequestValidation, |
| 58 |
ValidationSubType: helpers.ValidationMissingOperation, |
| 59 |
Message: fmt.Sprintf("%s operation request content type '%s' does not exist", |
| 60 |
request.Method, method), |
| 61 |
Reason: fmt.Sprintf("The path was found, but there was no '%s' method found in the spec", request.Method), |
| 62 |
SpecLine: specLine, |
| 63 |
SpecCol: specCol, |
| 64 |
Context: pathItem, |
| 65 |
HowToFix: HowToFixPathMethod, |
| 66 |
RequestPath: request.URL.Path, |
| 67 |
RequestMethod: request.Method, |
| 68 |
SpecPath: specPath, |
| 69 |
} |
| 70 |
} |