From e4301e396598fa771b888cb8024235746d26acc5 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Wed, 26 Jul 2023 15:27:38 +0300 Subject: [PATCH] Merging to release-5.1: [TT-9586] Prevent panic in setHasMock func (#5361) (#5363) [TT-9586] Prevent panic in setHasMock func (#5361) This PR fixes a panic case inside `APISpec.setHasMock` func. [TT-9586]: https://tyktech.atlassian.net/browse/TT-9586?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Furkan Senharputlu --- apidef/oas/oas.go | 7 +++--- gateway/api_definition.go | 2 +- gateway/api_definition_test.go | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/apidef/oas/oas.go b/apidef/oas/oas.go index b28ffd330d0..10c4b1ef15c 100644 --- a/apidef/oas/oas.go +++ b/apidef/oas/oas.go @@ -267,7 +267,8 @@ func (s *OAS) getTykSecurityScheme(name string) interface{} { return securitySchemes[name] } -func (s *OAS) getTykMiddleware() (middleware *Middleware) { +// GetTykMiddleware returns middleware section from XTykAPIGateway. +func (s *OAS) GetTykMiddleware() (middleware *Middleware) { if s.GetTykExtension() != nil { middleware = s.GetTykExtension().Middleware } @@ -276,8 +277,8 @@ func (s *OAS) getTykMiddleware() (middleware *Middleware) { } func (s *OAS) getTykOperations() (operations Operations) { - if s.getTykMiddleware() != nil { - operations = s.getTykMiddleware().Operations + if s.GetTykMiddleware() != nil { + operations = s.GetTykMiddleware().Operations } return diff --git a/gateway/api_definition.go b/gateway/api_definition.go index 5a1b6cf11d6..271c7c5ac2e 100644 --- a/gateway/api_definition.go +++ b/gateway/api_definition.go @@ -1728,7 +1728,7 @@ func (a *APISpec) setHasMock() { return } - middleware := a.OAS.GetTykExtension().Middleware + middleware := a.OAS.GetTykMiddleware() if middleware == nil { a.HasMock = false return diff --git a/gateway/api_definition_test.go b/gateway/api_definition_test.go index 891115d9b94..d9ba6b85aab 100644 --- a/gateway/api_definition_test.go +++ b/gateway/api_definition_test.go @@ -17,6 +17,8 @@ import ( "text/template" "time" + "github.com/TykTechnologies/tyk/apidef/oas" + "github.com/TykTechnologies/storage/persistent/model" "github.com/TykTechnologies/tyk/rpc" @@ -1549,3 +1551,44 @@ func TestAPISpec_isListeningOnPort(t *testing.T) { s.ListenPort = 8000 assert.True(t, s.isListeningOnPort(8000, cfg)) } + +func TestAPISpec_setHasMock(t *testing.T) { + s := APISpec{APIDefinition: &apidef.APIDefinition{}} + + s.setHasMock() + assert.False(t, s.HasMock) + + s.IsOAS = true + s.setHasMock() + assert.False(t, s.HasMock) + + s.OAS = oas.OAS{} + s.setHasMock() + assert.False(t, s.HasMock) + + xTyk := &oas.XTykAPIGateway{} + s.OAS.SetTykExtension(xTyk) + s.setHasMock() + assert.False(t, s.HasMock) + + middleware := &oas.Middleware{} + xTyk.Middleware = middleware + s.setHasMock() + assert.False(t, s.HasMock) + + op := &oas.Operation{} + middleware.Operations = oas.Operations{ + "my-operation": op, + } + s.setHasMock() + assert.False(t, s.HasMock) + + mock := &oas.MockResponse{} + op.MockResponse = mock + s.setHasMock() + assert.False(t, s.HasMock) + + mock.Enabled = true + s.setHasMock() + assert.True(t, s.HasMock) +}