From eebc1f66148998caa7f1929c3d1d7355a3c7af84 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Tue, 5 Mar 2024 13:34:03 +0100 Subject: [PATCH] Fix once again oneOf constraint for HTTP Bearer scheme (#102) --- openapi3/entities_extra_test.go | 21 +++++++++++++++++++++ openapi31/entities.go | 4 ++++ openapi31/entities_extra_test.go | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/openapi3/entities_extra_test.go b/openapi3/entities_extra_test.go index b7a64cd..e6510f6 100644 --- a/openapi3/entities_extra_test.go +++ b/openapi3/entities_extra_test.go @@ -72,3 +72,24 @@ paths: require.NoError(t, s.UnmarshalYAML([]byte(spec))) } + +func TestSpec_MarshalYAML_3(t *testing.T) { + var s openapi3.Spec + + spec := `openapi: 3.0.3 +info: + title: MyProject + description: "My Project Description" + version: v1.0.0 +components: + securitySchemes: + basicAuth: # <-- arbitrary name for the security scheme + type: http + scheme: basic +security: + - basicAuth: [] # <-- use the same name here +paths: +` + + require.NoError(t, s.UnmarshalYAML([]byte(spec))) +} diff --git a/openapi31/entities.go b/openapi31/entities.go index 668d905..1b79855 100644 --- a/openapi31/entities.go +++ b/openapi31/entities.go @@ -4345,6 +4345,10 @@ func (s *SecuritySchemeHTTPBearer) UnmarshalJSON(data []byte) error { return fmt.Errorf(`bad const value for "type" ("http" expected, %s received)`, v) } + if strings.ToLower(ms.Scheme) != "bearer" { + return errors.New("bearer scheme required") + } + delete(rawMap, "type") *s = SecuritySchemeHTTPBearer(ms) diff --git a/openapi31/entities_extra_test.go b/openapi31/entities_extra_test.go index 5047a92..e589125 100644 --- a/openapi31/entities_extra_test.go +++ b/openapi31/entities_extra_test.go @@ -72,3 +72,24 @@ paths: require.NoError(t, s.UnmarshalYAML([]byte(spec))) } + +func TestSpec_MarshalYAML_3(t *testing.T) { + var s openapi31.Spec + + spec := `openapi: 3.1.0 +info: + title: MyProject + description: "My Project Description" + version: v1.0.0 +components: + securitySchemes: + basicAuth: # <-- arbitrary name for the security scheme + type: http + scheme: basic +security: + - basicAuth: [] # <-- use the same name here +paths: +` + + require.NoError(t, s.UnmarshalYAML([]byte(spec))) +}