Skip to content

Commit

Permalink
Merging to release-5.1: [TT-9586] Prevent panic in setHasMock func (#…
Browse files Browse the repository at this point in the history
…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 <furkan_senharputlu@hotmail.com>
  • Loading branch information
buger and furkansenharputlu authored Jul 26, 2023
1 parent e0a0945 commit e4301e3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
7 changes: 4 additions & 3 deletions apidef/oas/oas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gateway/api_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions gateway/api_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}

0 comments on commit e4301e3

Please sign in to comment.