Skip to content

Commit 8d7a6ad

Browse files
Soumya Mukhopadhyaydaveshanley
authored andcommitted
Address #78
For a path parameter which is marked as required , empty value should not be allowed. Ideally , in this case , we do not need to proceed towards enum check as the absence of a value should violate the mandatory path parameter requirement criteria irrespective of the schema of path parameter
1 parent 6a51b95 commit 8d7a6ad

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

errors/parameter_errors.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package errors
22

33
import (
44
"fmt"
5-
"github.com/pb33f/libopenapi-validator/helpers"
6-
"github.com/pb33f/libopenapi/datamodel/high/base"
7-
"github.com/pb33f/libopenapi/datamodel/high/v3"
85
"net/url"
96
"strings"
7+
8+
"github.com/pb33f/libopenapi-validator/helpers"
9+
"github.com/pb33f/libopenapi/datamodel/high/base"
10+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
1011
)
1112

1213
func IncorrectFormEncoding(param *v3.Parameter, qp *helpers.QueryParam, i int) *ValidationError {
@@ -468,3 +469,16 @@ func IncorrectPathParamArrayBoolean(
468469
HowToFix: fmt.Sprintf(HowToFixParamInvalidBoolean, item),
469470
}
470471
}
472+
473+
func PathParameterMissing(param *v3.Parameter) *ValidationError {
474+
return &ValidationError{
475+
ValidationType: helpers.ParameterValidation,
476+
ValidationSubType: helpers.ParameterValidationPath,
477+
Message: fmt.Sprintf("Path parameter '%s' is missing", param.Name),
478+
Reason: fmt.Sprintf("The path parameter '%s' is defined as being required, "+
479+
"however it's missing from the requests", param.Name),
480+
SpecLine: param.GoLow().Required.KeyNode.Line,
481+
SpecCol: param.GoLow().Required.KeyNode.Column,
482+
HowToFix: HowToFixMissingValue,
483+
}
484+
}

parameters/path_parameters.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/pb33f/libopenapi-validator/helpers"
1414
"github.com/pb33f/libopenapi-validator/paths"
1515
"github.com/pb33f/libopenapi/datamodel/high/base"
16-
"github.com/pb33f/libopenapi/datamodel/high/v3"
16+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
1717
)
1818

1919
func (v *paramValidator) ValidatePathParams(request *http.Request) (bool, []*errors.ValidationError) {
@@ -85,7 +85,10 @@ func (v *paramValidator) ValidatePathParams(request *http.Request) (bool, []*err
8585
}
8686

8787
if paramValue == "" {
88-
// TODO: check path match issue.
88+
//Mandatory path parameter cannot be empty
89+
if p.Required != nil && *p.Required {
90+
validationErrors = append(validationErrors, errors.PathParameterMissing(p))
91+
}
8992
continue
9093
}
9194

parameters/path_parameters_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,3 +1436,31 @@ paths:
14361436
assert.True(t, valid)
14371437
assert.Len(t, errors, 0)
14381438
}
1439+
func TestNewValidator_MandatorydPathSegmentEmpty(t *testing.T) {
1440+
1441+
spec := `openapi: 3.1.0
1442+
servers:
1443+
- url: https://api.pb33f.io
1444+
description: Live production endpoint for general use.
1445+
paths:
1446+
/burgers/{burger}:
1447+
get:
1448+
parameters:
1449+
- name: burger
1450+
in: path
1451+
required: true
1452+
schema:
1453+
type: string`
1454+
1455+
doc, _ := libopenapi.NewDocument([]byte(spec))
1456+
1457+
m, _ := doc.BuildV3Model()
1458+
1459+
v := NewParameterValidator(&m.Model)
1460+
1461+
request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/", nil)
1462+
valid, errors := v.ValidatePathParams(request)
1463+
1464+
assert.False(t, valid)
1465+
assert.Len(t, errors, 1)
1466+
}

0 commit comments

Comments
 (0)