Skip to content

Commit

Permalink
Merge branch 'master' into fix/oas-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu authored Dec 22, 2023
2 parents ac99879 + 99f1da0 commit 9deaf4e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
10 changes: 10 additions & 0 deletions apidef/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,12 @@ func (a *APIDefinition) Migrate() (versions []APIDefinition, err error) {
a.MigrateEndpointMeta()
a.MigrateCachePlugin()
a.migrateGlobalHeaders()
a.migrateGlobalResponseHeaders()
for i := 0; i < len(versions); i++ {
versions[i].MigrateEndpointMeta()
versions[i].MigrateCachePlugin()
versions[i].migrateGlobalHeaders()
versions[i].migrateGlobalResponseHeaders()
}

return versions, nil
Expand Down Expand Up @@ -321,6 +323,14 @@ func (a *APIDefinition) migrateGlobalHeaders() {
}
}

func (a *APIDefinition) migrateGlobalResponseHeaders() {
vInfo := a.VersionData.Versions[""]
if len(vInfo.GlobalResponseHeaders) == 0 && len(vInfo.GlobalResponseHeadersRemove) == 0 {
vInfo.GlobalResponseHeadersDisabled = true
a.VersionData.Versions[""] = vInfo
}
}

func (a *APIDefinition) MigrateCachePlugin() {
vInfo := a.VersionData.Versions[""]
list := vInfo.ExtendedPaths.Cached
Expand Down
31 changes: 31 additions & 0 deletions apidef/oas/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ type Global struct {
// TransformRequestHeaders contains the configurations related to API level request header transformation.
// Tyk classic API definition: `global_headers`/`global_headers_remove`.
TransformRequestHeaders *TransformHeaders `bson:"transformRequestHeaders,omitempty" json:"transformRequestHeaders,omitempty"`

// TransformResponseHeaders contains the configurations related to API level response header transformation.
// Tyk classic API definition: `global_response_headers`/`global_response_headers_remove`.
TransformResponseHeaders *TransformHeaders `bson:"transformResponseHeaders,omitempty" json:"transformResponseHeaders,omitempty"`
}

// Fill fills *Global from apidef.APIDefinition.
Expand Down Expand Up @@ -153,6 +157,19 @@ func (g *Global) Fill(api apidef.APIDefinition) {
if ShouldOmit(g.TransformRequestHeaders) {
g.TransformRequestHeaders = nil
}

if g.TransformResponseHeaders == nil {
g.TransformResponseHeaders = &TransformHeaders{}
}

g.TransformResponseHeaders.Fill(apidef.HeaderInjectionMeta{
Disabled: vInfo.GlobalResponseHeadersDisabled,
AddHeaders: vInfo.GlobalResponseHeaders,
DeleteHeaders: vInfo.GlobalResponseHeadersRemove,
})
if ShouldOmit(g.TransformResponseHeaders) {
g.TransformResponseHeaders = nil
}
}

// ExtractTo extracts *Global into *apidef.APIDefinition.
Expand Down Expand Up @@ -230,6 +247,16 @@ func (g *Global) ExtractTo(api *apidef.APIDefinition) {
var headerMeta apidef.HeaderInjectionMeta
g.TransformRequestHeaders.ExtractTo(&headerMeta)

if g.TransformResponseHeaders == nil {
g.TransformResponseHeaders = &TransformHeaders{}
defer func() {
g.TransformResponseHeaders = nil
}()
}

var resHeaderMeta apidef.HeaderInjectionMeta
g.TransformResponseHeaders.ExtractTo(&resHeaderMeta)

if len(api.VersionData.Versions) == 0 {
api.VersionData.Versions = map[string]apidef.VersionInfo{
Main: {},
Expand All @@ -240,6 +267,10 @@ func (g *Global) ExtractTo(api *apidef.APIDefinition) {
vInfo.GlobalHeadersDisabled = headerMeta.Disabled
vInfo.GlobalHeaders = headerMeta.AddHeaders
vInfo.GlobalHeadersRemove = headerMeta.DeleteHeaders

vInfo.GlobalResponseHeadersDisabled = resHeaderMeta.Disabled
vInfo.GlobalResponseHeaders = resHeaderMeta.AddHeaders
vInfo.GlobalResponseHeadersRemove = resHeaderMeta.DeleteHeaders
api.VersionData.Versions[Main] = vInfo
}

Expand Down
4 changes: 1 addition & 3 deletions apidef/oas/oas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func TestOAS_ExtractTo_ResetAPIDefinition(t *testing.T) {
vInfo.Paths.BlackList = nil
vInfo.OverrideTarget = ""
vInfo.GlobalHeadersDisabled = false
vInfo.GlobalResponseHeadersDisabled = false
vInfo.UseExtendedPaths = false
vInfo.ExtendedPaths.MockResponse = nil
vInfo.ExtendedPaths.Cached = nil
Expand Down Expand Up @@ -245,9 +246,6 @@ func TestOAS_ExtractTo_ResetAPIDefinition(t *testing.T) {
"APIDefinition.VersionData.Versions[0].ExtendedPaths.PersistGraphQL[0].Method",
"APIDefinition.VersionData.Versions[0].ExtendedPaths.PersistGraphQL[0].Operation",
"APIDefinition.VersionData.Versions[0].ExtendedPaths.PersistGraphQL[0].Variables[0]",
"APIDefinition.VersionData.Versions[0].GlobalResponseHeaders[0]",
"APIDefinition.VersionData.Versions[0].GlobalResponseHeadersRemove[0]",
"APIDefinition.VersionData.Versions[0].GlobalResponseHeadersDisabled",
"APIDefinition.VersionData.Versions[0].IgnoreEndpointCase",
"APIDefinition.VersionData.Versions[0].GlobalSizeLimit",
"APIDefinition.UptimeTests.CheckList[0].CheckURL",
Expand Down
3 changes: 3 additions & 0 deletions apidef/oas/schema/x-tyk-api-gateway.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@
},
"transformRequestHeaders": {
"$ref": "#/definitions/X-Tyk-TransformHeaders"
},
"transformResponseHeaders": {
"$ref": "#/definitions/X-Tyk-TransformHeaders"
}
}
},
Expand Down
5 changes: 5 additions & 0 deletions apidef/oas/schema/x-tyk-gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@ TransformRequestHeaders contains the configurations related to API level request

Tyk classic API definition: `global_headers`/`global_headers_remove`.

**Field: `transformResponseHeaders` ([TransformHeaders](#transformheaders))**
TransformResponseHeaders contains the configurations related to API level response header transformation.

Tyk classic API definition: `global_response_headers`/`global_response_headers_remove`.


### **PluginConfig**

Expand Down

0 comments on commit 9deaf4e

Please sign in to comment.