Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions errors/parameter_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,47 @@ func InvalidDeepObject(param *v3.Parameter, qp *helpers.QueryParam) *ValidationE
}
}

func deepObjectPathForError(qp *helpers.QueryParam) string {
if qp == nil {
return ""
}
if len(qp.PropertyPath) > 0 {
return strings.Join(qp.PropertyPath, ".")
}
return qp.Property
}

func deepObjectBracketPathForError(qp *helpers.QueryParam) string {
if qp == nil {
return ""
}
if len(qp.PropertyPath) > 0 {
return strings.Join(qp.PropertyPath, "][")
}
return qp.Property
}

func InvalidDeepObjectPathConflict(param *v3.Parameter, prefixParam, nestedParam *helpers.QueryParam) *ValidationError {
specLine, specCol := paramStyleLineCol(param)
prefixPath := deepObjectPathForError(prefixParam)
nestedPath := deepObjectPathForError(nestedParam)
return &ValidationError{
ValidationType: helpers.ParameterValidation,
ValidationSubType: helpers.ParameterValidationQuery,
Message: fmt.Sprintf("Query parameter '%s' is not a valid deepObject", param.Name),
Reason: fmt.Sprintf("The query parameter '%s' has the 'deepObject' style defined, "+
"but the property path '%s' is also used as a nested object prefix for '%s'",
param.Name, prefixPath, nestedPath),
SpecLine: specLine,
SpecCol: specCol,
ParameterName: param.Name,
Context: param,
HowToFix: fmt.Sprintf(HowToFixParamInvalidDeepObjectPathConflict,
param.Name, deepObjectBracketPathForError(prefixParam),
param.Name, deepObjectBracketPathForError(nestedParam)),
}
}

func QueryParameterMissing(param *v3.Parameter, pathTemplate string, operation string, renderedSchema string) *ValidationError {
keywordLocation := helpers.ConstructParameterJSONPointer(pathTemplate, operation, param.Name, "required")
specLine, specCol := paramRequiredLineCol(param)
Expand Down
72 changes: 72 additions & 0 deletions errors/parameter_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,66 @@ func TestInvalidDeepObject(t *testing.T) {
require.Contains(t, err.HowToFix, "testParam=value1|value2")
}

func TestInvalidDeepObjectPathConflict(t *testing.T) {
param := createMockParameterWithDeepObjectStyle()
prefixParam := &helpers.QueryParam{
Key: "testParam",
Values: []string{"bad"},
Property: "nested",
PropertyPath: []string{"nested"},
}
nestedParam := &helpers.QueryParam{
Key: "testParam",
Values: []string{"ok"},
Property: "nested",
PropertyPath: []string{"nested", "child"},
}

err := InvalidDeepObjectPathConflict(param, prefixParam, nestedParam)

require.NotNil(t, err)
require.Equal(t, helpers.ParameterValidation, err.ValidationType)
require.Equal(t, helpers.ParameterValidationQuery, err.ValidationSubType)
require.Equal(t, "testParam", err.ParameterName)
require.Contains(t, err.Message, "Query parameter 'testParam' is not a valid deepObject")
require.Contains(t, err.Reason, "property path 'nested'")
require.Contains(t, err.Reason, "'nested.child'")
require.Contains(t, err.HowToFix, "testParam[nested]")
require.Contains(t, err.HowToFix, "testParam[nested][child]")
}

func TestInvalidDeepObjectPathConflict_NilPaths(t *testing.T) {
param := createMockParameterWithDeepObjectStyle()

err := InvalidDeepObjectPathConflict(param, nil, nil)

require.NotNil(t, err)
require.Contains(t, err.Reason, "property path ''")
require.Contains(t, err.HowToFix, "testParam[]")
}

func TestInvalidDeepObjectPathConflict_PropertyFallback(t *testing.T) {
param := createMockParameterWithDeepObjectStyle()
prefixParam := &helpers.QueryParam{
Key: "testParam",
Values: []string{"bad"},
Property: "nested",
}
nestedParam := &helpers.QueryParam{
Key: "testParam",
Values: []string{"ok"},
Property: "nested.child",
}

err := InvalidDeepObjectPathConflict(param, prefixParam, nestedParam)

require.NotNil(t, err)
require.Contains(t, err.Reason, "property path 'nested'")
require.Contains(t, err.Reason, "'nested.child'")
require.Contains(t, err.HowToFix, "testParam[nested]")
require.Contains(t, err.HowToFix, "testParam[nested.child]")
}

func createMockParameterForBooleanArray() *v3.Parameter {
param := &lowv3.Parameter{
Name: low.NodeReference[string]{Value: "testCookieParam"},
Expand Down Expand Up @@ -1290,6 +1350,18 @@ func TestParameterErrors_NilGoLowNodes(t *testing.T) {
require.Equal(t, 0, err.SpecCol)
})

t.Run("InvalidDeepObjectPathConflict", func(t *testing.T) {
err := InvalidDeepObjectPathConflict(param, qp, &helpers.QueryParam{
Key: "test",
Values: []string{"ok"},
Property: "foo",
PropertyPath: []string{"foo", "bar"},
})
require.NotNil(t, err)
require.Equal(t, 1, err.SpecLine)
require.Equal(t, 0, err.SpecCol)
})

t.Run("QueryParameterMissing", func(t *testing.T) {
err := QueryParameterMissing(param, "/test", "get", "{}")
require.NotNil(t, err)
Expand Down
29 changes: 15 additions & 14 deletions errors/parameters_howtofix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ const (
"they should be separated by pipes '|'. For example: '%s'"
HowToFixParamInvalidDeepObjectMultipleValues string = "There can only be a single value per property name, " +
"deepObject parameters should contain the property key in square brackets next to the parameter name. For example: '%s'"
HowToFixInvalidJSON string = "The JSON submitted is invalid, please check the syntax"
HowToFixInvalidUrlEncoded string = "Ensure URL Encoded submitted is well-formed and matches schema structure"
HowToFixDecodingError string = "The object can't be decoded, so make sure it's being encoded correctly according to the spec."
HowToFixInvalidContentType string = "The content type is invalid, Use one of the %d supported types for this operation: %s"
HowToFixInvalidResponseCode string = "The service is responding with a code that is not defined in the spec, fix the service or add the code to the specification"
HowToFixInvalidEncoding string = "Ensure the correct encoding has been used on the object"
HowToFixMissingValue string = "Ensure the value has been set"
HowToFixPath string = "Check the path is correct, and check that the correct HTTP method has been used (e.g. GET, POST, PUT, DELETE)"
HowToFixPathMethod string = "Add the missing operation to the contract for the path"
HowToFixInvalidMaxItems string = "Reduce the number of items in the array to %d or less"
HowToFixInvalidMinItems string = "Increase the number of items in the array to %d or more"
HowToFixMissingHeader string = "Make sure the service responding sets the required headers with this response code"
HowToFixInvalidRenderedSchema string = "Check the request schema for circular references or invalid structures"
HowToFixInvalidJsonSchema string = "Check the request schema for invalid JSON Schema syntax, complex regex patterns, or unsupported schema constructs"
HowToFixParamInvalidDeepObjectPathConflict string = "Use either '%s[%s]' or nested properties like '%s[%s]', not both"
HowToFixInvalidJSON string = "The JSON submitted is invalid, please check the syntax"
HowToFixInvalidUrlEncoded string = "Ensure URL Encoded submitted is well-formed and matches schema structure"
HowToFixDecodingError string = "The object can't be decoded, so make sure it's being encoded correctly according to the spec."
HowToFixInvalidContentType string = "The content type is invalid, Use one of the %d supported types for this operation: %s"
HowToFixInvalidResponseCode string = "The service is responding with a code that is not defined in the spec, fix the service or add the code to the specification"
HowToFixInvalidEncoding string = "Ensure the correct encoding has been used on the object"
HowToFixMissingValue string = "Ensure the value has been set"
HowToFixPath string = "Check the path is correct, and check that the correct HTTP method has been used (e.g. GET, POST, PUT, DELETE)"
HowToFixPathMethod string = "Add the missing operation to the contract for the path"
HowToFixInvalidMaxItems string = "Reduce the number of items in the array to %d or less"
HowToFixInvalidMinItems string = "Increase the number of items in the array to %d or more"
HowToFixMissingHeader string = "Make sure the service responding sets the required headers with this response code"
HowToFixInvalidRenderedSchema string = "Check the request schema for circular references or invalid structures"
HowToFixInvalidJsonSchema string = "Check the request schema for invalid JSON Schema syntax, complex regex patterns, or unsupported schema constructs"
)
Loading
Loading