Skip to content

Commit 851dc0a

Browse files
author
Florent Boisselier
committed
Separate validation of the integer type schema from the number type schema
1 parent c322bf8 commit 851dc0a

14 files changed

+1547
-40
lines changed

errors/parameter_errors.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ func IncorrectCookieParamArrayBoolean(
166166
}
167167
}
168168

169+
func IncorrectQueryParamArrayInteger(
170+
param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema,
171+
) *ValidationError {
172+
return &ValidationError{
173+
ValidationType: helpers.ParameterValidation,
174+
ValidationSubType: helpers.ParameterValidationQuery,
175+
Message: fmt.Sprintf("Query array parameter '%s' is not a valid integer", param.Name),
176+
Reason: fmt.Sprintf("The query parameter (which is an array) '%s' is defined as being an integer, "+
177+
"however the value '%s' is not a valid integer", param.Name, item),
178+
SpecLine: sch.Items.A.GoLow().Schema().Type.KeyNode.Line,
179+
SpecCol: sch.Items.A.GoLow().Schema().Type.KeyNode.Column,
180+
Context: itemsSchema,
181+
HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, item),
182+
}
183+
}
184+
169185
func IncorrectQueryParamArrayNumber(
170186
param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema,
171187
) *ValidationError {
@@ -226,6 +242,20 @@ func IncorrectQueryParamBool(param *v3.Parameter, ef string, sch *base.Schema) *
226242
}
227243
}
228244

245+
func InvalidQueryParamInteger(param *v3.Parameter, ef string, sch *base.Schema) *ValidationError {
246+
return &ValidationError{
247+
ValidationType: helpers.ParameterValidation,
248+
ValidationSubType: helpers.ParameterValidationQuery,
249+
Message: fmt.Sprintf("Query parameter '%s' is not a valid integer", param.Name),
250+
Reason: fmt.Sprintf("The query parameter '%s' is defined as being an integer, "+
251+
"however the value '%s' is not a valid integer", param.Name, ef),
252+
SpecLine: param.GoLow().Schema.KeyNode.Line,
253+
SpecCol: param.GoLow().Schema.KeyNode.Column,
254+
Context: sch,
255+
HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, ef),
256+
}
257+
}
258+
229259
func InvalidQueryParamNumber(param *v3.Parameter, ef string, sch *base.Schema) *ValidationError {
230260
return &ValidationError{
231261
ValidationType: helpers.ParameterValidation,
@@ -294,6 +324,20 @@ func IncorrectReservedValues(param *v3.Parameter, ef string, sch *base.Schema) *
294324
}
295325
}
296326

327+
func InvalidHeaderParamInteger(param *v3.Parameter, ef string, sch *base.Schema) *ValidationError {
328+
return &ValidationError{
329+
ValidationType: helpers.ParameterValidation,
330+
ValidationSubType: helpers.ParameterValidationHeader,
331+
Message: fmt.Sprintf("Header parameter '%s' is not a valid integer", param.Name),
332+
Reason: fmt.Sprintf("The header parameter '%s' is defined as being an integer, "+
333+
"however the value '%s' is not a valid integer", param.Name, ef),
334+
SpecLine: param.GoLow().Schema.KeyNode.Line,
335+
SpecCol: param.GoLow().Schema.KeyNode.Column,
336+
Context: sch,
337+
HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, ef),
338+
}
339+
}
340+
297341
func InvalidHeaderParamNumber(param *v3.Parameter, ef string, sch *base.Schema) *ValidationError {
298342
return &ValidationError{
299343
ValidationType: helpers.ParameterValidation,
@@ -308,6 +352,20 @@ func InvalidHeaderParamNumber(param *v3.Parameter, ef string, sch *base.Schema)
308352
}
309353
}
310354

355+
func InvalidCookieParamInteger(param *v3.Parameter, ef string, sch *base.Schema) *ValidationError {
356+
return &ValidationError{
357+
ValidationType: helpers.ParameterValidation,
358+
ValidationSubType: helpers.ParameterValidationCookie,
359+
Message: fmt.Sprintf("Cookie parameter '%s' is not a valid integer", param.Name),
360+
Reason: fmt.Sprintf("The cookie parameter '%s' is defined as being an integer, "+
361+
"however the value '%s' is not a valid integer", param.Name, ef),
362+
SpecLine: param.GoLow().Schema.KeyNode.Line,
363+
SpecCol: param.GoLow().Schema.KeyNode.Column,
364+
Context: sch,
365+
HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, ef),
366+
}
367+
}
368+
311369
func InvalidCookieParamNumber(param *v3.Parameter, ef string, sch *base.Schema) *ValidationError {
312370
return &ValidationError{
313371
ValidationType: helpers.ParameterValidation,
@@ -434,6 +492,20 @@ func IncorrectPathParamEnum(param *v3.Parameter, ef string, sch *base.Schema) *V
434492
}
435493
}
436494

495+
func IncorrectPathParamInteger(param *v3.Parameter, item string, sch *base.Schema) *ValidationError {
496+
return &ValidationError{
497+
ValidationType: helpers.ParameterValidation,
498+
ValidationSubType: helpers.ParameterValidationPath,
499+
Message: fmt.Sprintf("Path parameter '%s' is not a valid integer", param.Name),
500+
Reason: fmt.Sprintf("The path parameter '%s' is defined as being an integer, "+
501+
"however the value '%s' is not a valid integer", param.Name, item),
502+
SpecLine: param.GoLow().Schema.KeyNode.Line,
503+
SpecCol: param.GoLow().Schema.KeyNode.Column,
504+
Context: sch,
505+
HowToFix: fmt.Sprintf(HowToFixParamInvalidInteger, item),
506+
}
507+
}
508+
437509
func IncorrectPathParamNumber(param *v3.Parameter, item string, sch *base.Schema) *ValidationError {
438510
return &ValidationError{
439511
ValidationType: helpers.ParameterValidation,
@@ -464,6 +536,22 @@ func IncorrectPathParamArrayNumber(
464536
}
465537
}
466538

539+
func IncorrectPathParamArrayInteger(
540+
param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema,
541+
) *ValidationError {
542+
return &ValidationError{
543+
ValidationType: helpers.ParameterValidation,
544+
ValidationSubType: helpers.ParameterValidationPath,
545+
Message: fmt.Sprintf("Path array parameter '%s' is not a valid integer", param.Name),
546+
Reason: fmt.Sprintf("The path parameter (which is an array) '%s' is defined as being an integer, "+
547+
"however the value '%s' is not a valid integer", param.Name, item),
548+
SpecLine: sch.Items.A.GoLow().Schema().Type.KeyNode.Line,
549+
SpecCol: sch.Items.A.GoLow().Schema().Type.KeyNode.Column,
550+
Context: itemsSchema,
551+
HowToFix: fmt.Sprintf(HowToFixParamInvalidNumber, item),
552+
}
553+
}
554+
467555
func IncorrectPathParamArrayBoolean(
468556
param *v3.Parameter, item string, sch *base.Schema, itemsSchema *base.Schema,
469557
) *ValidationError {

errors/parameter_errors_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,25 @@ func createMockLowBaseSchemaForNumberArray() *lowbase.Schema {
367367
return itemsSchema
368368
}
369369

370+
func TestIncorrectQueryParamArrayInteger(t *testing.T) {
371+
// Create mock parameter and schemas
372+
param := createMockParameterForNumberArray()
373+
baseSchema := createMockLowBaseSchemaForNumberArray()
374+
s := base.NewSchema(baseSchema)
375+
itemsSchema := base.NewSchema(baseSchema.Items.Value.A.Schema())
376+
377+
// Call the function with an invalid number value in the array
378+
err := IncorrectQueryParamArrayInteger(param, "notNumber", s, itemsSchema)
379+
380+
// Validate the error
381+
require.NotNil(t, err)
382+
require.Equal(t, helpers.ParameterValidation, err.ValidationType)
383+
require.Equal(t, helpers.ParameterValidationQuery, err.ValidationSubType)
384+
require.Contains(t, err.Message, "Query array parameter 'testQueryParam' is not a valid integer")
385+
require.Contains(t, err.Reason, "the value 'notNumber' is not a valid integer")
386+
require.Contains(t, err.HowToFix, "notNumber")
387+
}
388+
370389
func TestIncorrectQueryParamArrayNumber(t *testing.T) {
371390
// Create mock parameter and schemas
372391
param := createMockParameterForNumberArray()
@@ -568,6 +587,22 @@ func TestInvalidQueryParamNumber(t *testing.T) {
568587
require.Contains(t, err.HowToFix, "notNumber")
569588
}
570589

590+
func TestInvalidQueryParamInteger(t *testing.T) {
591+
param := createMockParameter()
592+
baseSchema := createMockLowBaseSchema()
593+
594+
// Call the function with an invalid number value
595+
err := InvalidQueryParamInteger(param, "notNumber", base.NewSchema(baseSchema))
596+
597+
// Validate the error
598+
require.NotNil(t, err)
599+
require.Equal(t, helpers.ParameterValidation, err.ValidationType)
600+
require.Equal(t, helpers.ParameterValidationQuery, err.ValidationSubType)
601+
require.Contains(t, err.Message, "Query parameter 'testQueryParam' is not a valid integer")
602+
require.Contains(t, err.Reason, "the value 'notNumber' is not a valid integer")
603+
require.Contains(t, err.HowToFix, "notNumber")
604+
}
605+
571606
func TestIncorrectQueryParamEnum(t *testing.T) {
572607
enum := `enum: [fish, crab, lobster]`
573608
var n yaml.Node
@@ -645,6 +680,29 @@ func TestIncorrectReservedValues(t *testing.T) {
645680
require.Contains(t, err.HowToFix, "borked%3A%3A%3F%5E%26%2A")
646681
}
647682

683+
func TestInvalidHeaderParamInteger(t *testing.T) {
684+
enum := `name: blip`
685+
var n yaml.Node
686+
_ = yaml.Unmarshal([]byte(enum), &n)
687+
688+
schemaProxy := &lowbase.SchemaProxy{}
689+
require.NoError(t, schemaProxy.Build(context.Background(), n.Content[0], n.Content[0], nil))
690+
691+
highSchema := base.NewSchema(schemaProxy.Schema())
692+
param := createMockParameter()
693+
param.Name = "bunny"
694+
695+
err := InvalidHeaderParamInteger(param, "bunmy", highSchema)
696+
697+
// Validate the error
698+
require.NotNil(t, err)
699+
require.Equal(t, helpers.ParameterValidation, err.ValidationType)
700+
require.Equal(t, helpers.ParameterValidationHeader, err.ValidationSubType)
701+
require.Contains(t, err.Message, "Header parameter 'bunny' is not a valid integer")
702+
require.Contains(t, err.Reason, "The header parameter 'bunny' is defined as being an integer")
703+
require.Contains(t, err.HowToFix, "bunmy")
704+
}
705+
648706
func TestInvalidHeaderParamNumber(t *testing.T) {
649707
enum := `name: blip`
650708
var n yaml.Node
@@ -691,6 +749,29 @@ func TestInvalidCookieParamNumber(t *testing.T) {
691749
require.Contains(t, err.HowToFix, "milky")
692750
}
693751

752+
func TestInvalidCookieParamInteger(t *testing.T) {
753+
enum := `name: blip`
754+
var n yaml.Node
755+
_ = yaml.Unmarshal([]byte(enum), &n)
756+
757+
schemaProxy := &lowbase.SchemaProxy{}
758+
require.NoError(t, schemaProxy.Build(context.Background(), n.Content[0], n.Content[0], nil))
759+
760+
highSchema := base.NewSchema(schemaProxy.Schema())
761+
param := createMockParameter()
762+
param.Name = "cookies"
763+
764+
err := InvalidCookieParamInteger(param, "milky", highSchema)
765+
766+
// Validate the error
767+
require.NotNil(t, err)
768+
require.Equal(t, helpers.ParameterValidation, err.ValidationType)
769+
require.Equal(t, helpers.ParameterValidationCookie, err.ValidationSubType)
770+
require.Contains(t, err.Message, "Cookie parameter 'cookies' is not a valid integer")
771+
require.Contains(t, err.Reason, "The cookie parameter 'cookies' is defined as being an integer")
772+
require.Contains(t, err.HowToFix, "milky")
773+
}
774+
694775
func TestIncorrectHeaderParamBool(t *testing.T) {
695776
enum := `name: blip`
696777
var n yaml.Node
@@ -900,6 +981,31 @@ func TestIncorrectPathParamNumber(t *testing.T) {
900981
require.Contains(t, err.HowToFix, "milky")
901982
}
902983

984+
func TestIncorrectPathParamInteger(t *testing.T) {
985+
items := `items:
986+
type: integer`
987+
var n yaml.Node
988+
_ = yaml.Unmarshal([]byte(items), &n)
989+
990+
schemaProxy := &lowbase.SchemaProxy{}
991+
require.NoError(t, schemaProxy.Build(context.Background(), n.Content[0], n.Content[0], nil))
992+
993+
highSchema := base.NewSchema(schemaProxy.Schema())
994+
param := createMockParameter()
995+
param.Schema = base.CreateSchemaProxy(highSchema)
996+
param.GoLow().Schema.KeyNode = &yaml.Node{}
997+
998+
err := IncorrectPathParamInteger(param, "milky", highSchema)
999+
1000+
// Validate the error
1001+
require.NotNil(t, err)
1002+
require.Equal(t, helpers.ParameterValidation, err.ValidationType)
1003+
require.Equal(t, helpers.ParameterValidationPath, err.ValidationSubType)
1004+
require.Contains(t, err.Message, "Path parameter 'testQueryParam' is not a valid integer")
1005+
require.Contains(t, err.Reason, "The path parameter 'testQueryParam' is defined as being an integer")
1006+
require.Contains(t, err.HowToFix, "milky")
1007+
}
1008+
9031009
func TestIncorrectPathParamArrayNumber(t *testing.T) {
9041010
items := `items:
9051011
type: number`
@@ -926,6 +1032,32 @@ func TestIncorrectPathParamArrayNumber(t *testing.T) {
9261032
require.Contains(t, err.HowToFix, "milky")
9271033
}
9281034

1035+
func TestIncorrectPathParamArrayInteger(t *testing.T) {
1036+
items := `items:
1037+
type: integer`
1038+
var n yaml.Node
1039+
_ = yaml.Unmarshal([]byte(items), &n)
1040+
1041+
schemaProxy := &lowbase.SchemaProxy{}
1042+
require.NoError(t, schemaProxy.Build(context.Background(), n.Content[0], n.Content[0], nil))
1043+
1044+
highSchema := base.NewSchema(schemaProxy.Schema())
1045+
highSchema.GoLow().Items.Value.A.Schema()
1046+
1047+
param := createMockParameter()
1048+
param.Name = "bubbles"
1049+
1050+
err := IncorrectPathParamArrayInteger(param, "milky", highSchema, nil)
1051+
1052+
// Validate the error
1053+
require.NotNil(t, err)
1054+
require.Equal(t, helpers.ParameterValidation, err.ValidationType)
1055+
require.Equal(t, helpers.ParameterValidationPath, err.ValidationSubType)
1056+
require.Contains(t, err.Message, "Path array parameter 'bubbles' is not a valid integer")
1057+
require.Contains(t, err.Reason, "The path parameter (which is an array) 'bubbles' is defined as being an integer")
1058+
require.Contains(t, err.HowToFix, "milky")
1059+
}
1060+
9291061
func TestIncorrectPathParamArrayBoolean(t *testing.T) {
9301062
items := `items:
9311063
type: number`

errors/parameters_howtofix.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package errors
66
const (
77
HowToFixReservedValues string = "parameter values need to URL Encoded to ensure reserved " +
88
"values are correctly encoded, for example: '%s'"
9+
HowToFixParamInvalidInteger string = "Convert the value '%s' into an integer"
910
HowToFixParamInvalidNumber string = "Convert the value '%s' into a number"
1011
HowToFixParamInvalidString string = "Convert the value '%s' into a string (cannot start with a number, or be a floating point)"
1112
HowToFixParamInvalidBoolean string = "Convert the value '%s' into a true/false value"

parameters/cookie_parameters.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,27 @@ func (v *paramValidator) ValidateCookieParamsWithPathItem(request *http.Request,
5656

5757
for _, ty := range pType {
5858
switch ty {
59-
case helpers.Integer, helpers.Number:
59+
case helpers.Integer:
60+
if _, err := strconv.ParseInt(cookie.Value, 10, 64); err != nil {
61+
validationErrors = append(validationErrors,
62+
errors.InvalidCookieParamInteger(p, strings.ToLower(cookie.Value), sch))
63+
break
64+
}
65+
// check if enum is in range
66+
if sch.Enum != nil {
67+
matchFound := false
68+
for _, enumVal := range sch.Enum {
69+
if strings.TrimSpace(cookie.Value) == fmt.Sprint(enumVal.Value) {
70+
matchFound = true
71+
break
72+
}
73+
}
74+
if !matchFound {
75+
validationErrors = append(validationErrors,
76+
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
77+
}
78+
}
79+
case helpers.Number:
6080
if _, err := strconv.ParseFloat(cookie.Value, 64); err != nil {
6181
validationErrors = append(validationErrors,
6282
errors.InvalidCookieParamNumber(p, strings.ToLower(cookie.Value), sch))

0 commit comments

Comments
 (0)