From 0074840768dc6b582d21c80509b1384ca95f775f Mon Sep 17 00:00:00 2001 From: tomato0111 Date: Thu, 1 Dec 2022 23:04:09 +0000 Subject: [PATCH 1/3] fix: setting defaults for oneOf and anyOf --- openapi3/schema.go | 17 +++++++++-------- openapi3filter/validate_set_default_test.go | 6 ++++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/openapi3/schema.go b/openapi3/schema.go index d2cd31c5f..e594d513f 100644 --- a/openapi3/schema.go +++ b/openapi3/schema.go @@ -933,10 +933,6 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val matchedOneOfIdx = 0 tempValue = value ) - // make a deep copy to protect origin value from being injected default value that defined in mismatched oneOf schema - if settings.asreq || settings.asrep { - tempValue = deepcopy.Copy(value) - } for idx, item := range v { v := item.Value if v == nil { @@ -947,6 +943,11 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val continue } + // make a deep copy to protect origin value from being injected default value that defined in mismatched oneOf schema + if settings.asreq || settings.asrep { + tempValue = deepcopy.Copy(value) + } + if err := v.visitJSON(settings, tempValue); err != nil { validationErrors = append(validationErrors, err) continue @@ -989,15 +990,15 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val matchedAnyOfIdx = 0 tempValue = value ) - // make a deep copy to protect origin value from being injected default value that defined in mismatched anyOf schema - if settings.asreq || settings.asrep { - tempValue = deepcopy.Copy(value) - } for idx, item := range v { v := item.Value if v == nil { return foundUnresolvedRef(item.Ref) } + // make a deep copy to protect origin value from being injected default value that defined in mismatched anyOf schema + if settings.asreq || settings.asrep { + tempValue = deepcopy.Copy(value) + } if err := v.visitJSON(settings, tempValue); err == nil { ok = true matchedAnyOfIdx = idx diff --git a/openapi3filter/validate_set_default_test.go b/openapi3filter/validate_set_default_test.go index 4550b51b2..70c3f88f8 100644 --- a/openapi3filter/validate_set_default_test.go +++ b/openapi3filter/validate_set_default_test.go @@ -262,7 +262,8 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) { "type": "string", "default": "www.twitter.com" } - } + }, + "additionalProperties": false }, { "type": "object", @@ -278,7 +279,8 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) { "type": "string", "default": "www.facebook.com" } - } + }, + "additionalProperties": false } ] }, From e4305b04649d60f1bd03fc0cc77612513547c775 Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 5 Dec 2022 13:52:45 -0800 Subject: [PATCH 2/3] test: set oneof and anyof default value with strict properties --- openapi3filter/validate_set_default_test.go | 119 +++++++++++++++++++- 1 file changed, 117 insertions(+), 2 deletions(-) diff --git a/openapi3filter/validate_set_default_test.go b/openapi3filter/validate_set_default_test.go index 70c3f88f8..b91c0aac0 100644 --- a/openapi3filter/validate_set_default_test.go +++ b/openapi3filter/validate_set_default_test.go @@ -279,8 +279,7 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) { "type": "string", "default": "www.facebook.com" } - }, - "additionalProperties": false + } } ] }, @@ -319,6 +318,70 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) { } } ] + }, + "contact": { + "oneOf": [ + { + "type": "object", + "required": ["email"], + "properties": { + "email": { + "type": "string" + }, + "allow_image": { + "type": "boolean", + "default": true + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": ["phone"], + "properties": { + "phone": { + "type": "string" + }, + "allow_text": { + "type": "boolean", + "default": false + } + }, + "additionalProperties": false + } + ] + }, + "contact2": { + "anyOf": [ + { + "type": "object", + "required": ["email"], + "properties": { + "email": { + "type": "string" + }, + "allow_image": { + "type": "boolean", + "default": true + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": ["phone"], + "properties": { + "phone": { + "type": "string" + }, + "allow_text": { + "type": "boolean", + "default": false + } + }, + "additionalProperties": false + } + ] } } } @@ -360,6 +423,10 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) { FBLink string `json:"fb_link,omitempty"` TWLink string `json:"tw_link,omitempty"` } + type contact struct { + Email string `json:"email,omitempty"` + Phone string `json:"phone,omitempty"` + } type body struct { ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` @@ -369,6 +436,8 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) { Filters []filter `json:"filters,omitempty"` SocialNetwork *socialNetwork `json:"social_network,omitempty"` SocialNetwork2 *socialNetwork `json:"social_network_2,omitempty"` + Contact *contact `json:"contact,omitempty"` + Contact2 *contact `json:"contact2,omitempty"` } testCases := []struct { @@ -658,6 +727,52 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) { "platform": "facebook", "fb_link": "www.facebook.com" } +} + `, body) + }, + }, + { + name: "contact(oneOf)", + body: body{ + ID: "bt6kdc3d0cvp6u8u3ft0", + Contact: &contact{ + Phone: "123456", + }, + }, + bodyAssertion: func(t *testing.T, body string) { + require.JSONEq(t, ` +{ + "id": "bt6kdc3d0cvp6u8u3ft0", + "name": "default", + "code": 123, + "all": false, + "contact": { + "phone": "123456", + "allow_text": false + } +} + `, body) + }, + }, + { + name: "contact(anyOf)", + body: body{ + ID: "bt6kdc3d0cvp6u8u3ft0", + Contact2: &contact{ + Phone: "123456", + }, + }, + bodyAssertion: func(t *testing.T, body string) { + require.JSONEq(t, ` +{ + "id": "bt6kdc3d0cvp6u8u3ft0", + "name": "default", + "code": 123, + "all": false, + "contact2": { + "phone": "123456", + "allow_text": false + } } `, body) }, From d9a86a1ae62d275332479e420377a1412e54a40e Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 5 Dec 2022 13:59:37 -0800 Subject: [PATCH 3/3] fix: undo origin test change --- openapi3filter/validate_set_default_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/openapi3filter/validate_set_default_test.go b/openapi3filter/validate_set_default_test.go index b91c0aac0..731cbbdca 100644 --- a/openapi3filter/validate_set_default_test.go +++ b/openapi3filter/validate_set_default_test.go @@ -262,8 +262,7 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) { "type": "string", "default": "www.twitter.com" } - }, - "additionalProperties": false + } }, { "type": "object",