From c7f7f394454f1d9ac534cacafff33e12b3caab0b Mon Sep 17 00:00:00 2001 From: Dragos Gabriel Ghinea <142506926+dgghinea@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:03:22 +0300 Subject: [PATCH] APIGOV-DG000 - fix NPE on spec.components (#246) --- go.mod | 2 +- go.sum | 2 ++ pkg/discovery/servicehandler.go | 7 +++++-- pkg/discovery/servicehandler_test.go | 6 +++--- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 406b3f0..a25e870 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 toolchain go1.21.3 require ( - github.com/Axway/agent-sdk v1.1.89 + github.com/Axway/agent-sdk v1.1.90-0.20240703131051-2f7d0eeca272 github.com/elastic/beats/v7 v7.17.20 github.com/getkin/kin-openapi v0.125.0 github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum index 20bc291..4cdd69f 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/Axway/agent-sdk v1.1.89 h1:YEtScppqRLbX730g1RAzwgoMP45dnJCOdPluphQYk3Q= github.com/Axway/agent-sdk v1.1.89/go.mod h1:LvcLFYbRJnI1iWXpkK01mSdMMwoIKd49M5Aik07gZhM= +github.com/Axway/agent-sdk v1.1.90-0.20240703131051-2f7d0eeca272 h1:x/cKr0Lc5PflLlWlgTdQBh/N/0mssv61Vf/D5BYNs+Q= +github.com/Axway/agent-sdk v1.1.90-0.20240703131051-2f7d0eeca272/go.mod h1:LvcLFYbRJnI1iWXpkK01mSdMMwoIKd49M5Aik07gZhM= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= github.com/Azure/go-autorest v12.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24= diff --git a/pkg/discovery/servicehandler.go b/pkg/discovery/servicehandler.go index 593bf64..7154e1d 100644 --- a/pkg/discovery/servicehandler.go +++ b/pkg/discovery/servicehandler.go @@ -351,7 +351,7 @@ func setOAS2policies(swagger *openapi2.T, configuration map[string]interface{}) } swagger.SecurityDefinitions[common.BasicAuthName] = &ss swagger.Security = append(swagger.Security, map[string][]string{ - common.BasicAuthName: []string{}, + common.BasicAuthName: {}, }) case apic.Oauth: @@ -371,7 +371,7 @@ func setOAS2policies(swagger *openapi2.T, configuration map[string]interface{}) }) } else { swagger.Security = append(swagger.Security, map[string][]string{ - common.Oauth2Name: []string{}, + common.Oauth2Name: {}, }) } @@ -390,6 +390,9 @@ func setOAS2policies(swagger *openapi2.T, configuration map[string]interface{}) } func setOAS3policies(spec *openapi3.T, configuration map[string]interface{}) ([]byte, error) { + if spec.Components == nil { + spec.Components = &openapi3.Components{} + } // remove existing security spec.Components.SecuritySchemes = make(openapi3.SecuritySchemes) spec.Security = *openapi3.NewSecurityRequirements() diff --git a/pkg/discovery/servicehandler_test.go b/pkg/discovery/servicehandler_test.go index 565c726..e3d278f 100644 --- a/pkg/discovery/servicehandler_test.go +++ b/pkg/discovery/servicehandler_test.go @@ -435,7 +435,7 @@ func TestSetPolicies(t *testing.T) { Info: &openapi3.Info{ Title: "petstore3", }, - Paths: openapi3.Paths{}, + Paths: &openapi3.Paths{}, Servers: openapi3.Servers{{URL: "http://google.com"}}, }, expectedContent: map[string]interface{}{ @@ -479,7 +479,7 @@ func TestSetPolicies(t *testing.T) { Info: &openapi3.Info{ Title: "petstore3", }, - Paths: openapi3.Paths{}, + Paths: &openapi3.Paths{}, Servers: openapi3.Servers{{URL: "http://google.com"}}, }, expectedContent: map[string]interface{}{ @@ -528,7 +528,7 @@ func TestSetPolicies(t *testing.T) { Info: &openapi3.Info{ Title: "petstore3", }, - Paths: openapi3.Paths{}, + Paths: &openapi3.Paths{}, Servers: openapi3.Servers{{URL: "http://google.com"}}, }, expectedContent: map[string]interface{}{