Skip to content

Commit 0a3478f

Browse files
emilien-pugetdaveshanley
authored andcommitted
support slash server
1 parent 13261d1 commit 0a3478f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

paths/paths.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ func StripRequestPath(request *http.Request, document *v3.Document) string {
216216
if request.URL.Fragment != "" {
217217
stripped = fmt.Sprintf("%s#%s", stripped, request.URL.Fragment)
218218
}
219+
if len(stripped) > 0 && !strings.HasPrefix(stripped, "/") {
220+
stripped = "/" + stripped
221+
}
219222
return stripped
220223
}
221224

validator_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/pb33f/libopenapi"
1515
"github.com/pb33f/libopenapi-validator/helpers"
1616
"github.com/stretchr/testify/assert"
17+
"github.com/stretchr/testify/require"
1718
)
1819

1920
func TestNewValidator(t *testing.T) {
@@ -149,6 +150,36 @@ paths:
149150

150151
}
151152

153+
func TestNewValidator_slash_server_url(t *testing.T) {
154+
155+
spec := `openapi: 3.1.0
156+
servers:
157+
- url: /
158+
paths:
159+
/burgers/{burgerId}/locate:
160+
patch:
161+
operationId: locateBurger
162+
parameters:
163+
- name: burgerId
164+
in: path
165+
required: true
166+
schema:
167+
type: string
168+
format: uuid
169+
`
170+
171+
doc, err := libopenapi.NewDocument([]byte(spec))
172+
require.NoError(t, err)
173+
174+
request, _ := http.NewRequest(http.MethodPatch, "https://things.com/burgers/edd0189c-420b-489c-98f2-0facc5a26f3a/locate", nil)
175+
v, _ := NewValidator(doc)
176+
177+
valid, errors := v.ValidateHttpRequest(request)
178+
179+
assert.True(t, valid)
180+
assert.Len(t, errors, 0)
181+
}
182+
152183
func TestNewValidator_ValidateHttpRequest_SetPath_ValidPostSimpleSchema(t *testing.T) {
153184

154185
spec := `openapi: 3.1.0

0 commit comments

Comments
 (0)