-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: optional read only and write only validations (#689)
- Loading branch information
Showing
7 changed files
with
308 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package openapi3_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
) | ||
|
||
func TestIssue689(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := [...]struct { | ||
name string | ||
schema *openapi3.Schema | ||
value map[string]interface{} | ||
opts []openapi3.SchemaValidationOption | ||
checkErr require.ErrorAssertionFunc | ||
}{ | ||
// read-only | ||
{ | ||
name: "read-only property succeeds when read-only validation is disabled", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", ReadOnly: true}}), | ||
value: map[string]interface{}{"foo": true}, | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsRequest(), | ||
openapi3.DisableReadOnlyValidation()}, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "non read-only property succeeds when read-only validation is disabled", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", ReadOnly: false}}), | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsRequest()}, | ||
value: map[string]interface{}{"foo": true}, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "read-only property fails when read-only validation is enabled", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", ReadOnly: true}}), | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsRequest()}, | ||
value: map[string]interface{}{"foo": true}, | ||
checkErr: require.Error, | ||
}, | ||
{ | ||
name: "non read-only property succeeds when read-only validation is enabled", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", ReadOnly: false}}), | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsRequest()}, | ||
value: map[string]interface{}{"foo": true}, | ||
checkErr: require.NoError, | ||
}, | ||
// write-only | ||
{ | ||
name: "write-only property succeeds when write-only validation is disabled", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", WriteOnly: true}}), | ||
value: map[string]interface{}{"foo": true}, | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsResponse(), | ||
openapi3.DisableWriteOnlyValidation()}, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "non write-only property succeeds when write-only validation is disabled", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", WriteOnly: false}}), | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsResponse()}, | ||
value: map[string]interface{}{"foo": true}, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "write-only property fails when write-only validation is enabled", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", WriteOnly: true}}), | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsResponse()}, | ||
value: map[string]interface{}{"foo": true}, | ||
checkErr: require.Error, | ||
}, | ||
{ | ||
name: "non write-only property succeeds when write-only validation is enabled", | ||
schema: openapi3.NewSchema().WithProperties(map[string]*openapi3.Schema{ | ||
"foo": {Type: "boolean", WriteOnly: false}}), | ||
opts: []openapi3.SchemaValidationOption{ | ||
openapi3.VisitAsResponse()}, | ||
value: map[string]interface{}{"foo": true}, | ||
checkErr: require.NoError, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
err := test.schema.VisitJSON(test.value, test.opts...) | ||
test.checkErr(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package openapi3filter | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/getkin/kin-openapi/routers/gorillamux" | ||
) | ||
|
||
func TestIssue689(t *testing.T) { | ||
loader := openapi3.NewLoader() | ||
ctx := loader.Context | ||
spec := ` | ||
openapi: 3.0.0 | ||
info: | ||
version: 1.0.0 | ||
title: Sample API | ||
paths: | ||
/items: | ||
put: | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
testWithReadOnly: | ||
readOnly: true | ||
type: boolean | ||
testNoReadOnly: | ||
type: boolean | ||
type: object | ||
responses: | ||
'200': | ||
description: OK | ||
get: | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
testWithWriteOnly: | ||
writeOnly: true | ||
type: boolean | ||
testNoWriteOnly: | ||
type: boolean | ||
`[1:] | ||
|
||
doc, err := loader.LoadFromData([]byte(spec)) | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(ctx) | ||
require.NoError(t, err) | ||
|
||
router, err := gorillamux.NewRouter(doc) | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
options *Options | ||
body string | ||
method string | ||
checkErr require.ErrorAssertionFunc | ||
}{ | ||
// read-only | ||
{ | ||
name: "non read-only property is added to request when validation enabled", | ||
body: `{"testNoReadOnly": true}`, | ||
method: http.MethodPut, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "non read-only property is added to request when validation disabled", | ||
body: `{"testNoReadOnly": true}`, | ||
method: http.MethodPut, | ||
options: &Options{ | ||
ExcludeReadOnlyValidations: true, | ||
}, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "read-only property is added to requests when validation enabled", | ||
body: `{"testWithReadOnly": true}`, | ||
method: http.MethodPut, | ||
checkErr: require.Error, | ||
}, | ||
{ | ||
name: "read-only property is added to requests when validation disabled", | ||
body: `{"testWithReadOnly": true}`, | ||
method: http.MethodPut, | ||
options: &Options{ | ||
ExcludeReadOnlyValidations: true, | ||
}, | ||
checkErr: require.NoError, | ||
}, | ||
// write-only | ||
{ | ||
name: "non write-only property is added to request when validation enabled", | ||
body: `{"testNoWriteOnly": true}`, | ||
method: http.MethodGet, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "non write-only property is added to request when validation disabled", | ||
body: `{"testNoWriteOnly": true}`, | ||
method: http.MethodGet, | ||
options: &Options{ | ||
ExcludeWriteOnlyValidations: true, | ||
}, | ||
checkErr: require.NoError, | ||
}, | ||
{ | ||
name: "write-only property is added to requests when validation enabled", | ||
body: `{"testWithWriteOnly": true}`, | ||
method: http.MethodGet, | ||
checkErr: require.Error, | ||
}, | ||
{ | ||
name: "write-only property is added to requests when validation disabled", | ||
body: `{"testWithWriteOnly": true}`, | ||
method: http.MethodGet, | ||
options: &Options{ | ||
ExcludeWriteOnlyValidations: true, | ||
}, | ||
checkErr: require.NoError, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
httpReq, err := http.NewRequest(test.method, "/items", strings.NewReader(test.body)) | ||
require.NoError(t, err) | ||
httpReq.Header.Set("Content-Type", "application/json") | ||
require.NoError(t, err) | ||
|
||
route, pathParams, err := router.FindRoute(httpReq) | ||
require.NoError(t, err) | ||
|
||
requestValidationInput := &RequestValidationInput{ | ||
Request: httpReq, | ||
PathParams: pathParams, | ||
Route: route, | ||
Options: test.options, | ||
} | ||
|
||
if test.method == http.MethodGet { | ||
responseValidationInput := &ResponseValidationInput{ | ||
RequestValidationInput: requestValidationInput, | ||
Status: 200, | ||
Header: httpReq.Header, | ||
Body: io.NopCloser(strings.NewReader(test.body)), | ||
Options: test.options, | ||
} | ||
err = ValidateResponse(ctx, responseValidationInput) | ||
|
||
} else { | ||
err = ValidateRequest(ctx, requestValidationInput) | ||
} | ||
test.checkErr(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters