Skip to content

Commit

Permalink
openapi3: add a test for additionalProperties: false validation (#975)
Browse files Browse the repository at this point in the history
* test: add a test for additionalProperties: false validation

* goimports
  • Loading branch information
AnatolyRugalev authored Jul 4, 2024
1 parent 0ed9f5d commit 4b53bf6
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions openapi3/issue82_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package openapi3

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func TestIssue82(t *testing.T) {
payload := map[string]interface{}{
"prop1": "val",
"prop3": "val",
}

schemas := []string{`
{
"type": "object",
"additionalProperties": false,
"required": ["prop1"],
"properties": {
"prop1": {
"type": "string"
}
}
}`, `{
"anyOf": [
{
"type": "object",
"additionalProperties": false,
"required": ["prop1"],
"properties": {
"prop1": {
"type": "string"
}
}
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"prop2": {
"type": "string"
}
}
}
]
}`, `{
"oneOf": [
{
"type": "object",
"additionalProperties": false,
"required": ["prop1"],
"properties": {
"prop1": {
"type": "string"
}
}
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"prop2": {
"type": "string"
}
}
}
]
}`, `{
"allOf": [
{
"type": "object",
"additionalProperties": false,
"required": ["prop1"],
"properties": {
"prop1": {
"type": "string"
}
}
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"prop2": {
"type": "string"
}
}
}
]
}
`}

for _, jsonSchema := range schemas {
var dataSchema Schema
err := json.Unmarshal([]byte(jsonSchema), &dataSchema)
require.NoError(t, err)

err = dataSchema.VisitJSON(payload)
require.Error(t, err)
}
}

0 comments on commit 4b53bf6

Please sign in to comment.