Skip to content

[TT-5588] [OAS] gateway apiKey import generates unnecessary object #7270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

MaciekMis
Copy link
Contributor

@MaciekMis MaciekMis commented Aug 4, 2025

User description

TT-5588
Summary [OAS] gateway apiKey import generates unnecessary object
Type Bug Bug
Status In Dev
Points N/A
Labels codilime_refined

Description

The header object is unnecessarily generated during creation of a Tyk OAS API by importing an OpenAPI description with security scheme defined.

Related Issue

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • I ensured that the documentation is up to date
  • I explained why this PR updates go.mod in detail with reasoning why it's required
  • I would like a code coverage CI quality gate exception and have explained why

PR Type

Bug fix, Tests


Description

  • Prevent AuthSources from being serialized in Token struct

  • Update JSON serialization tags to exclude AuthSources

  • Add test to verify AuthSources are not serialized

  • Ensure unmarshalled Token omits AuthSources fields


Diagram Walkthrough

flowchart LR
  TokenStruct["Token struct"]
  Serialization["JSON Serialization"]
  TestCase["Test: AuthSources not serialized"]
  TokenStruct -- "exclude AuthSources from JSON" --> Serialization
  Serialization -- "verify exclusion" --> TestCase
Loading

File Walkthrough

Relevant files
Bug fix
security.go
Exclude AuthSources from Token JSON serialization               

apidef/oas/security.go

  • Changed JSON struct tag for AuthSources to exclude from serialization
  • Prevents AuthSources from appearing in serialized JSON output
+1/-1     
Tests
security_test.go
Add test for non-serialization of AuthSources in Token     

apidef/oas/security_test.go

  • Added test to ensure AuthSources fields are not serialized
  • Verifies that Query, Header, and Cookie are nil after serialization
    round-trip
+15/-0   

@buger
Copy link
Member

buger commented Aug 4, 2025

I'm a bot and I 👍 this PR title. 🤖

Copy link
Contributor

github-actions bot commented Aug 4, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Serialization Change

The change to the AuthSources struct field in the Token type removes JSON inlining and disables JSON serialization. This could impact any consumers expecting these fields in the serialized output. Review for compatibility and unintended side effects.

AuthSources `bson:",inline" json:"-"`
Test Coverage

New tests were added to ensure AuthSources fields are not serialized to JSON. Confirm that these tests sufficiently cover all relevant use cases and edge cases for serialization and deserialization.

// Make sure AuthSources are not serialized into json.
token.Query = &AuthSource{Enabled: true}
token.Header = &AuthSource{Enabled: true}
token.Cookie = &AuthSource{Enabled: true}
bytes, err := json.Marshal(token)
assert.NoError(t, err)

var unmarshalledToken Token
err = json.Unmarshal(bytes, &unmarshalledToken)
assert.NoError(t, err)
assert.Nil(t, unmarshalledToken.Query)
assert.Nil(t, unmarshalledToken.Header)
assert.Nil(t, unmarshalledToken.Cookie)

Copy link
Contributor

github-actions bot commented Aug 4, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Prevent test side effects from shared state

The test does not reset the token object after modifying its fields, which could
cause side effects in subsequent tests if token is reused. Ensure that the test uses
a fresh Token instance for JSON marshaling to avoid test contamination.

apidef/oas/security_test.go [207-216]

-bytes, err := json.Marshal(token)
+freshToken := *token
+freshToken.Query = &AuthSource{Enabled: true}
+freshToken.Header = &AuthSource{Enabled: true}
+freshToken.Cookie = &AuthSource{Enabled: true}
+bytes, err := json.Marshal(freshToken)
 assert.NoError(t, err)
 
 var unmarshalledToken Token
 err = json.Unmarshal(bytes, &unmarshalledToken)
 assert.NoError(t, err)
 assert.Nil(t, unmarshalledToken.Query)
 assert.Nil(t, unmarshalledToken.Header)
 assert.Nil(t, unmarshalledToken.Cookie)
Suggestion importance[1-10]: 6

__

Why: The suggestion to use a fresh Token instance for JSON marshaling is a good practice to prevent test contamination, especially if token is reused elsewhere. However, if token is only used within this test, the impact is moderate rather than critical.

Low

Copy link
Contributor

github-actions bot commented Aug 4, 2025

API Changes

--- prev.txt	2025-08-11 08:25:56.484167431 +0000
+++ current.txt	2025-08-11 08:25:47.216030778 +0000
@@ -4555,7 +4555,7 @@
 	Enabled bool `bson:"enabled" json:"enabled"` // required
 
 	// AuthSources contains the configuration for authentication sources.
-	AuthSources `bson:",inline" json:",inline"`
+	AuthSources `bson:",inline" json:"-"`
 
 	// EnableClientCertificate allows to create dynamic keys based on certificates.
 	//

Copy link
Contributor

github-actions bot commented Aug 4, 2025

📦 Impact Review Snapshot

Effort Downstream Updates Compatibility Docs TL;DR
Low 🟢 📖 Minor change to prevent unnecessary serialization of AuthSources in Token struct
## Impact Assessment

This PR makes a targeted change to prevent the AuthSources field from being serialized in the Token struct by changing its JSON tag from json:",inline" to json:"-". This fixes an issue where unnecessary objects were being generated during OAS API import with security schemes defined.

The change is isolated to the serialization behavior of the Token struct and doesn't alter the API's functionality or interface. It's a bug fix that prevents extraneous data from appearing in the serialized output, making the API definition cleaner and more accurate.

## Required Updates

No updates are required in downstream repositories:

  • tyk-operator: No changes needed as this is a serialization-only fix that doesn't affect the API structure or behavior.
  • tyk-charts: No configuration changes are required.
  • portal: No changes needed as this only affects internal serialization.
  • tyk-sink: No changes needed as this doesn't affect RPC communication protocols.
## Compatibility Concerns

This change is backward compatible:

  • It removes fields from JSON output rather than adding required fields
  • The removed fields were unnecessary and not being used by consumers
  • The test verifies that after serialization and deserialization, the fields remain nil as expected

No consumers should be relying on these fields being present in the serialized output, as they were being unnecessarily included.

## Summary & Recommendations
  • This is a low-impact bug fix that prevents unnecessary objects from being generated during OAS API import
  • The change is well-tested with a specific test case that verifies the serialization behavior
  • No downstream repositories need updates as this is purely a serialization fix
  • No documentation updates are required as this fixes an implementation detail

No suggestions to provide – change LGTM.


Tip: Mention me again using /dependency <request>.
Powered by Probe AI
Tyk Gateway Dependency Impact Reviewer

Copy link
Contributor

github-actions bot commented Aug 4, 2025

🚀 Performance Snapshot

Effort Perf Risk Hot Paths Benchmarks TL;DR
Low 🟢 Minimal performance impact with potential memory savings during API serialization
## Performance Impact Analysis

The PR changes the JSON serialization behavior of the Token struct in the OAS API definition by preventing the AuthSources field from being included in the serialized output. This is accomplished by changing the JSON tag from json:",inline" to json:"-". The change affects how API definitions are serialized during API creation and updates, particularly when importing OpenAPI descriptions with security schemes.

## Critical Areas

The change affects the API loading and serialization path, which is not in the critical request path. This modification only impacts:

  1. API definition serialization during API creation/updates
  2. OpenAPI import operations
  3. API definition storage and retrieval

Since this change only affects control plane operations (API definition management) and not data plane operations (request processing), the performance impact on gateway throughput is negligible.

## Optimization Recommendations

The change is already an optimization that reduces the size of serialized API definitions by omitting unnecessary fields. This should result in:

  • Slightly smaller API definition JSON payloads
  • Reduced memory usage during API definition serialization/deserialization
  • Cleaner API definition storage

No further optimizations are needed for this specific change.

## Summary
  • The change has a positive impact by reducing unnecessary data in serialized API definitions
  • The modification only affects control plane operations, not request processing paths
  • Memory usage during API definition serialization will be slightly reduced
  • No negative performance impacts are expected from this change

No suggestions to provide – change LGTM.


Tip: Mention me again using /performance <request>.
Powered by Probe AI
Performance Impact Reviewer Prompt

Copy link
Contributor

github-actions bot commented Aug 4, 2025

🚦 Connectivity Review Snapshot

Effort Tests Security Perf TL;DR
1/5 🔒 none 🟢 Prevents unnecessary serialization of AuthSources in Token struct
## Connectivity Assessment
  • Redis Connections: No impact on Redis connectivity as this is a serialization-only change.
  • RPC Connections: No impact on RPC connections as the change only affects JSON serialization.
  • Synchronization Mechanisms: The change prevents unnecessary object generation during OAS API creation but doesn't affect synchronization between components.
## Test Coverage Validation
  • Redis Tests: Not applicable as this change doesn't affect Redis functionality.
  • RPC Tests: Not applicable as this change doesn't affect RPC functionality.
  • Failure Scenario Tests: The added test properly verifies that AuthSources fields are not serialized to JSON and remain nil after unmarshaling.
## Security & Performance Impact
  • Authentication Changes: No security impact as this is only changing what fields are serialized, not changing authentication behavior.
  • Performance Considerations: Slight positive performance impact by reducing the size of serialized JSON data.
  • Error Handling: No changes to error handling mechanisms.
## Summary & Recommendations
  • This change correctly addresses the issue of unnecessary object generation during OAS API creation by preventing AuthSources from being serialized to JSON.
  • The implementation is clean and focused, changing only the JSON tag from json:",inline" to json:"-".
  • The test case properly verifies that AuthSources fields (Query, Header, Cookie) are not included in serialized output.
  • No suggestions to provide – change LGTM.

Tip: Mention me again using /connectivity <request>.
Powered by Probe AI
Connectivity Issues Reviewer Prompt for Tyk Gateway

Copy link
Contributor

github-actions bot commented Aug 4, 2025

🛡️ Security Snapshot

Effort Risk Level Tests Compliance TL;DR
Low 🟢 ✔️ Prevents unnecessary object generation by excluding AuthSources from JSON serialization
## Security Impact Analysis

The PR modifies the JSON serialization tag for the AuthSources field in the Token struct from json:",inline" to json:"-", preventing this field from being serialized to JSON. This change is security-positive as it reduces the attack surface by eliminating unnecessary data exposure in API responses and prevents potential information leakage about authentication configuration.

## Identified Vulnerabilities

No vulnerabilities were identified in this change. The modification is a security improvement that:

  • Reduces unnecessary data exposure
  • Prevents potential information leakage about authentication configuration
  • Minimizes the JSON payload size, which is a good practice
## Security Recommendations

The implementation is appropriate and includes proper test coverage. No additional security measures are needed for this change.

Consider applying similar pattern to other authentication-related structs that might have the same issue (JWT, Basic, OAuth, ExternalOAuth) if they also unnecessarily expose authentication configuration details.

## OWASP Compliance

This change aligns with OWASP best practices:

  • Supports the principle of minimizing attack surface (OWASP A6:2017 - Security Misconfiguration)
  • Follows the principle of least privilege by not exposing unnecessary information
  • Helps prevent potential information leakage that could aid attackers in understanding the system's authentication mechanisms
## Summary
  • The change is security-positive, preventing unnecessary exposure of authentication configuration
  • The implementation includes proper test coverage to verify the behavior
  • No security issues identified – change LGTM.

Tip: Mention me again using /security <request>.
Powered by Probe AI
Security Impact Reviewer Prompt

Copy link
Contributor

@edsonmichaque edsonmichaque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

github-actions bot commented Aug 8, 2025

PR Analysis: Preventing Unnecessary Object Generation in OAS API Import

This PR addresses a bug where unnecessary objects were being generated during the creation of a Tyk OAS API by importing an OpenAPI description with security schemes defined.

Issue and Solution

The issue occurs because the AuthSources struct is embedded in the Token struct with json:",inline", causing all its fields (Header, Query, Cookie) to be included in the JSON serialization even when they're not needed. This results in unnecessary objects in the serialized output.

The PR fixes this by:

  1. Changing the JSON tag for AuthSources in the Token struct from json:",inline" to json:"-", which excludes it from JSON serialization
  2. Adding a test to verify that the AuthSources fields are not serialized to JSON

Technical Details

The AuthSources struct is embedded in several authentication-related structs:

  • Token (modified in this PR)
  • JWT
  • HMAC
  • OIDC
  • CustomPluginAuthentication
  • ExternalOAuth

The change only affects the JSON serialization of the Token struct. The AuthSources fields are still accessible in memory through the embedded struct, and the Fill and ExtractTo methods continue to work as before. This ensures that the in-memory behavior and API functionality remain unchanged.

Impact

This is a low-impact change that:

  • Reduces the size of serialized API definitions
  • Makes the API definition cleaner and more accurate
  • Has no effect on runtime behavior or API functionality
  • Is backward compatible since it removes fields rather than adding required ones

The added test properly verifies that after serialization and deserialization, the AuthSources fields remain nil as expected, confirming the fix works as intended.


Tip: Mention me again using /connectivity <request>.
Powered by Probe AI
Connectivity Issues Reviewer Prompt for Tyk Gateway

Copy link
Contributor

github-actions bot commented Aug 8, 2025

🛡️ Security Snapshot

Effort Risk Level Tests Compliance TL;DR
Low 🟢 ✔️ Prevents unnecessary object generation by excluding AuthSources from JSON serialization
## Security Impact Analysis

The PR modifies the JSON serialization tag for the AuthSources field in the Token struct from json:",inline" to json:"-", preventing this field from being serialized to JSON. This change is security-positive as it reduces the attack surface by eliminating unnecessary data exposure in API responses and prevents potential information leakage about authentication configuration.

## Identified Vulnerabilities

No vulnerabilities were identified in this change. The modification is a security improvement that:

  • Reduces unnecessary data exposure
  • Prevents potential information leakage about authentication configuration
  • Minimizes the JSON payload size, which is a good practice
## Security Recommendations

The implementation is appropriate and includes proper test coverage. No additional security measures are needed for this change.

Consider applying similar pattern to other authentication-related structs that might have the same issue (JWT, Basic, OAuth, ExternalOAuth) if they also unnecessarily expose authentication configuration details.

## OWASP Compliance

This change aligns with OWASP best practices:

  • Supports the principle of minimizing attack surface (OWASP A6:2017 - Security Misconfiguration)
  • Follows the principle of least privilege by not exposing unnecessary information
  • Helps prevent potential information leakage that could aid attackers in understanding the system's authentication mechanisms
## Summary
  • The change is security-positive, preventing unnecessary exposure of authentication configuration
  • The implementation includes proper test coverage to verify the behavior
  • No security issues identified – change LGTM.

Tip: Mention me again using /security <request>.
Powered by Probe AI
Security Impact Reviewer Prompt

Copy link
Contributor

github-actions bot commented Aug 8, 2025

Performance Impact Analysis of PR #7270

This PR addresses an issue where unnecessary objects were being generated during OAS API import with security schemes defined. The fix is simple but effective - changing the JSON serialization tag for the AuthSources field in the Token struct from json:",inline" to json:"-" to exclude it from serialization.

Key Changes

  1. JSON Serialization Tag Change: The PR modifies the AuthSources field in the Token struct to prevent it from being serialized to JSON:

    // Before
    AuthSources `bson:",inline" json:",inline"`
    
    // After
    AuthSources `bson:",inline" json:"-"`
  2. Test Coverage: A test was added to verify that the AuthSources fields (Query, Header, Cookie) are not serialized to JSON. The test:

    • Sets these fields with test values
    • Marshals the token to JSON
    • Unmarshals it back
    • Verifies that the fields are nil as expected

Impact Assessment

This change is low-impact and focused on improving the cleanliness of the API definition:

  • Performance: Slight positive impact by reducing the size of serialized JSON data
  • Compatibility: No breaking changes as it only removes unnecessary fields
  • Security: Positive impact by reducing attack surface and preventing unnecessary information exposure
  • Maintainability: Improves code clarity by ensuring only necessary fields are serialized

The PR correctly addresses the issue of unnecessary object generation during OAS API import with security schemes defined, making the API definition cleaner and more accurate without introducing any regressions.


Tip: Mention me again using /performance <request>.
Powered by Probe AI
Performance Impact Reviewer Prompt

Copy link
Contributor

github-actions bot commented Aug 8, 2025

Analysis of PR #7270: [OAS] Gateway apiKey Import Generates Unnecessary Object

Overview

This PR addresses a bug where unnecessary objects are generated during the creation of a Tyk OAS API when importing an OpenAPI description with security schemes defined. The fix is minimal but effective, changing a single JSON struct tag to prevent serialization of embedded fields.

The Issue

The Token struct in apidef/oas/security.go embeds the AuthSources struct with the JSON tag json:",inline". This causes all fields from AuthSources (Header, Query, and Cookie) to be included directly in the JSON output when a Token is serialized, even when these fields are not needed.

// Before the change
type Token struct {
    // ...
    AuthSources `bson:",inline" json:",inline"`
    // ...
}

When an OpenAPI description with a security scheme is imported, this results in unnecessary objects being generated in the API definition, making it more verbose than necessary.

The Solution

The PR changes the JSON tag from json:",inline" to json:"-", which completely excludes the AuthSources fields from JSON serialization:

// After the change
type Token struct {
    // ...
    AuthSources `bson:",inline" json:"-"`
    // ...
}

This prevents the unnecessary generation of Header, Query, and Cookie objects in the serialized output, making the API definition cleaner and more accurate. The MongoDB serialization is maintained with bson:",inline" since that's still needed for storage.

Test Coverage

A test has been added to verify this behavior:

// Make sure AuthSources are not serialized into json.
token.Query = &AuthSource{Enabled: true}
token.Header = &AuthSource{Enabled: true}
token.Cookie = &AuthSource{Enabled: true}
bytes, err := json.Marshal(token)
assert.NoError(t, err)

var unmarshalledToken Token
err = json.Unmarshal(bytes, &unmarshalledToken)
assert.NoError(t, err)
assert.Nil(t, unmarshalledToken.Query)
assert.Nil(t, unmarshalledToken.Header)
assert.Nil(t, unmarshalledToken.Cookie)

This test confirms that after serialization and deserialization, the AuthSources fields are properly excluded.

Technical Analysis

  1. Scope of Change: The change is isolated to the Token struct and doesn't affect other places where AuthSources is embedded with json:",inline" (such as in HMAC, OIDC, and CustomPluginAuthentication structs).

  2. Compatibility: This change is backward compatible since it removes fields from the JSON output rather than adding required ones. No consumers should be relying on these fields being present in the serialized output.

  3. Performance Impact: There's a minor positive performance impact as the serialized JSON will be smaller, requiring less bandwidth and processing.

  4. Security Implications: None. This is purely a serialization change and doesn't affect authentication or authorization logic.

Conclusion

This PR provides a clean, focused fix for the issue of unnecessary objects being generated during OAS API import. The change is minimal, well-tested, and improves the cleanliness of the API definition JSON output without introducing any compatibility risks.


Tip: Mention me again using /dependency <request>.
Powered by Probe AI
Tyk Gateway Dependency Impact Reviewer

Copy link
Contributor

🛡️ Security Snapshot

Effort Risk Level Tests Compliance TL;DR
Low 🟢 ✔️ Prevents unnecessary object generation by excluding AuthSources from JSON serialization
## Security Impact Analysis

The PR modifies the JSON serialization tag for the AuthSources field in the Token struct from json:",inline" to json:"-", preventing this field from being serialized to JSON. This change is security-positive as it reduces the attack surface by eliminating unnecessary data exposure in API responses and prevents potential information leakage about authentication configuration.

## Identified Vulnerabilities

No vulnerabilities were identified in this change. The modification is a security improvement that:

  • Reduces unnecessary data exposure
  • Prevents potential information leakage about authentication configuration
  • Minimizes the JSON payload size, which is a good practice
## Security Recommendations

The implementation is appropriate and includes proper test coverage. No additional security measures are needed for this change.

Consider applying similar pattern to other authentication-related structs that might have the same issue (JWT, Basic, OAuth, ExternalOAuth) if they also unnecessarily expose authentication configuration details.

## OWASP Compliance

This change aligns with OWASP best practices:

  • Supports the principle of minimizing attack surface (OWASP A6:2017 - Security Misconfiguration)
  • Follows the principle of least privilege by not exposing unnecessary information
  • Helps prevent potential information leakage that could aid attackers in understanding the system's authentication mechanisms
## Summary
  • The change is security-positive, preventing unnecessary exposure of authentication configuration
  • The implementation includes proper test coverage to verify the behavior
  • No security issues identified – change LGTM.

Tip: Mention me again using /security <request>.
Powered by Probe AI
Security Impact Reviewer Prompt

Copy link
Contributor

🚀 Performance Snapshot

Effort Perf Risk Hot Paths Benchmarks TL;DR
Low 🟢 Minimal performance impact with potential memory savings during API serialization
## Performance Impact Analysis

The PR changes the JSON serialization behavior of the Token struct in the OAS API definition by preventing the AuthSources field from being included in the serialized output. This is accomplished by changing the JSON tag from json:",inline" to json:"-".

The change affects how API definitions are serialized during API creation and updates, particularly when importing OpenAPI descriptions with security schemes defined. This modification prevents unnecessary objects (Header, Query, Cookie) from being generated in the resulting Tyk OAS API, making the API definition cleaner and more accurate.

From a performance perspective, this change will slightly reduce:

  • The size of serialized JSON payloads
  • Memory usage during serialization/deserialization
  • Network bandwidth when transferring API definitions
  • Storage requirements for API definitions

The change is isolated to the serialization behavior and doesn't affect the runtime functionality of the API or request processing paths.

## Critical Areas

The change affects the API loading and serialization path, which is not in the critical request path. This modification only impacts:

  1. API definition serialization during API creation/updates
  2. OpenAPI import operations
  3. API definition storage and retrieval

Since this change only affects control plane operations (API definition management) and not data plane operations (request processing), the performance impact on gateway throughput is negligible.

The Token struct is used in the authentication flow, but this change only affects how it's serialized to JSON, not how it's used in memory during request processing. The AuthSources field is still accessible in memory through the embedded struct, and the Fill and ExtractTo methods continue to work as before.

## Optimization Recommendations

The change is already an optimization that reduces the size of serialized API definitions by omitting unnecessary fields. This should result in:

  • Slightly smaller API definition JSON payloads
  • Reduced memory usage during API definition serialization/deserialization
  • Cleaner API definition storage
  • Improved readability of the API definition

No further optimizations are needed for this specific change. The test added ensures that the serialization behavior works as expected, verifying that the AuthSources fields are properly excluded from JSON output.

If similar issues exist in other authentication-related structs (JWT, Basic, OAuth, ExternalOAuth) that also embed AuthSources, a similar approach could be applied to them in the future, but that's outside the scope of this PR.

## Summary
  • The change has a positive impact by reducing unnecessary data in serialized API definitions
  • The modification only affects control plane operations, not request processing paths
  • Memory usage during API definition serialization will be slightly reduced
  • The change is well-tested with a specific test case that verifies the serialization behavior
  • No negative performance impacts are expected from this change

No suggestions to provide – change LGTM.


Tip: Mention me again using /performance <request>.
Powered by Probe AI
Performance Impact Reviewer Prompt

Copy link
Contributor

Analysis of PR #7270: Preventing Unnecessary Object Generation in OAS API Import

Issue Overview

This PR addresses a bug where unnecessary objects were being generated during the creation of a Tyk OAS API when importing an OpenAPI description with security schemes defined. Specifically, the AuthSources fields (Header, Query, Cookie) were being included in the JSON serialization output even when they weren't needed.

Root Cause

The issue is in the Token struct in apidef/oas/security.go, which embeds the AuthSources struct with the JSON tag json:",inline":

// Token contains the configuration for token authentication.
type Token struct {
    // ...
    // AuthSources contains the configuration for authentication sources.
    AuthSources `bson:",inline" json:",inline"`
    // ...
}

This tag causes all fields from AuthSources (Header, Query, and Cookie) to be included directly in the JSON output when a Token is serialized, even when these fields are not needed.

Solution

The PR changes the JSON tag from json:",inline" to json:"-":

// AuthSources contains the configuration for authentication sources.
AuthSources `bson:",inline" json:"-"`

This change prevents the AuthSources fields from being included in the JSON serialization, while maintaining the MongoDB serialization with bson:",inline" since that's still needed for storage.

Test Coverage

A test was added to the existing TestOAS_Token function to verify this behavior:

// Make sure AuthSources are not serialized into json.
token.Query = &AuthSource{Enabled: true}
token.Header = &AuthSource{Enabled: true}
token.Cookie = &AuthSource{Enabled: true}
bytes, err := json.Marshal(token)
assert.NoError(t, err)

var unmarshalledToken Token
err = json.Unmarshal(bytes, &unmarshalledToken)
assert.NoError(t, err)
assert.Nil(t, unmarshalledToken.Query)
assert.Nil(t, unmarshalledToken.Header)
assert.Nil(t, unmarshalledToken.Cookie)

This test confirms that after serialization and deserialization, the AuthSources fields are properly excluded from the JSON output.

Impact Analysis

  1. Scope of Change: The change is isolated to the Token struct and doesn't affect other places where AuthSources is embedded with json:",inline" (such as in JWT, HMAC, OIDC, and other authentication types).

  2. Compatibility: This change is backward compatible since it removes fields from the JSON output rather than adding required ones. No consumers should be relying on these fields being present in the serialized output.

  3. Performance Impact: There's a minor positive performance impact as the serialized JSON will be smaller, requiring less bandwidth and processing.

  4. Security Implications: None. This is purely a serialization change and doesn't affect authentication or authorization logic.

Conclusion

This PR provides a clean, focused fix for the issue of unnecessary objects being generated during OAS API import. The change is minimal, well-tested, and improves the cleanliness of the API definition JSON output without introducing any compatibility risks.

The PR maintains the functionality of the Token struct while ensuring that the serialized JSON representation is cleaner and more efficient. The test case properly verifies that the AuthSources fields are excluded from serialization, confirming that the fix works as intended.


Tip: Mention me again using /connectivity <request>.
Powered by Probe AI
Connectivity Issues Reviewer Prompt for Tyk Gateway

Copy link

Copy link
Contributor

📦 Impact Review Snapshot

Effort Downstream Updates Compatibility Docs TL;DR
Low 🟢 📖 Minor change to prevent unnecessary serialization of AuthSources in Token struct
## Impact Assessment

This PR makes a targeted change to prevent the AuthSources field from being serialized in the Token struct by changing its JSON tag from json:",inline" to json:"-". This fixes an issue where unnecessary objects were being generated during OAS API import with security schemes defined.

The change is isolated to the serialization behavior of the Token struct and doesn't alter the API's functionality or interface. It's a bug fix that prevents extraneous data from appearing in the serialized output, making the API definition cleaner and more accurate.

## Required Updates

No updates are required in downstream repositories:

  • tyk-operator: No changes needed as this is a serialization-only fix that doesn't affect the API structure or behavior.
  • tyk-charts: No configuration changes are required.
  • portal: No changes needed as this only affects internal serialization.
  • tyk-sink: No changes needed as this doesn't affect RPC communication protocols.
## Compatibility Concerns

This change is backward compatible:

  • It removes fields from JSON output rather than adding required fields
  • The removed fields were unnecessary and not being used by consumers
  • The test verifies that after serialization and deserialization, the fields remain nil as expected

No consumers should be relying on these fields being present in the serialized output, as they were being unnecessarily included.

## Summary & Recommendations
  • This is a low-impact bug fix that prevents unnecessary objects from being generated during OAS API import
  • The change is well-tested with a specific test case that verifies the serialization behavior
  • No downstream repositories need updates as this is purely a serialization fix
  • No documentation updates are required as this fixes an implementation detail

No suggestions to provide – change LGTM.


Tip: Mention me again using /dependency <request>.
Powered by Probe AI
Tyk Gateway Dependency Impact Reviewer

@MaciekMis MaciekMis merged commit 8b4fa8e into master Aug 11, 2025
43 of 45 checks passed
@MaciekMis MaciekMis deleted the TT-5588-oas-gateway-api-key-import-generates-unnecessary-object branch August 11, 2025 09:20
@MaciekMis
Copy link
Contributor Author

/release to release-5.8

Copy link

tykbot bot commented Aug 11, 2025

Working on it! Note that it can take a few minutes.

@MaciekMis
Copy link
Contributor Author

/release to release-5.10

tykbot bot pushed a commit that referenced this pull request Aug 11, 2025
…7270)

### **User description**
<details open>
<summary><a href="https://tyktech.atlassian.net/browse/TT-5588"
title="TT-5588" target="_blank">TT-5588</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
      <td>[OAS] gateway apiKey import generates unnecessary object</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Bug"
src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10303?size=medium"
/>
        Bug
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Dev</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
<td><a
href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20codilime_refined%20ORDER%20BY%20created%20DESC"
title="codilime_refined">codilime_refined</a></td>
    </tr>
  </table>
</details>
<!--
  do not remove this marker as it will break jira-lint's functionality.
  added_by_jira_lint
-->

---

<!-- Provide a general summary of your changes in the Title above -->

## Description

The header object is unnecessarily generated during creation of a Tyk
OAS API by importing an OpenAPI description with security scheme
defined.

## Related Issue

<!-- This project only accepts pull requests related to open issues. -->
<!-- If suggesting a new feature or change, please discuss it in an
issue first. -->
<!-- If fixing a bug, there should be an issue describing it with steps
to reproduce. -->
<!-- OSS: Please link to the issue here. Tyk: please create/link the
JIRA ticket. -->

## Motivation and Context

<!-- Why is this change required? What problem does it solve? -->

## How This Has Been Tested

<!-- Please describe in detail how you tested your changes -->
<!-- Include details of your testing environment, and the tests -->
<!-- you ran to see how your change affects other areas of the code,
etc. -->
<!-- This information is helpful for reviewers and QA. -->

## Screenshots (if appropriate)

## Types of changes

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Refactoring or add test (improvements in base code or adds test
coverage to functionality)

## Checklist

<!-- Go over all the following points, and put an `x` in all the boxes
that apply -->
<!-- If there are no documentation updates required, mark the item as
checked. -->
<!-- Raise up any additional concerns not covered by the checklist. -->

- [ ] I ensured that the documentation is up to date
- [ ] I explained why this PR updates go.mod in detail with reasoning
why it's required
- [ ] I would like a code coverage CI quality gate exception and have
explained why


___

### **PR Type**
Bug fix, Tests


___

### **Description**
- Prevent `AuthSources` from being serialized in `Token` struct

- Update JSON serialization tags to exclude `AuthSources`

- Add test to verify `AuthSources` are not serialized

- Ensure unmarshalled `Token` omits `AuthSources` fields


___

### Diagram Walkthrough


```mermaid
flowchart LR
  TokenStruct["Token struct"]
  Serialization["JSON Serialization"]
  TestCase["Test: AuthSources not serialized"]
  TokenStruct -- "exclude AuthSources from JSON" --> Serialization
  Serialization -- "verify exclusion" --> TestCase
```



<details> <summary><h3> File Walkthrough</h3></summary>

<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Bug
fix</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>security.go</strong><dd><code>Exclude AuthSources from
Token JSON serialization</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; </dd></summary>
<hr>

apidef/oas/security.go

<ul><li>Changed JSON struct tag for <code>AuthSources</code> to exclude
from serialization<br> <li> Prevents <code>AuthSources</code> from
appearing in serialized JSON output</ul>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/7270/files#diff-15e7d47137452ca4f3f6139aa8c007cdb426152c41846f712f8bf5dfb607afcc">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Tests</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>security_test.go</strong><dd><code>Add test for
non-serialization of AuthSources in Token</code>&nbsp; &nbsp; &nbsp;
</dd></summary>
<hr>

apidef/oas/security_test.go

<ul><li>Added test to ensure <code>AuthSources</code> fields are not
serialized<br> <li> Verifies that <code>Query</code>,
<code>Header</code>, and <code>Cookie</code> are nil after serialization
<br>round-trip</ul>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/7270/files#diff-5184167309db0462243e424baca87b5bb668962d8cc1076629fdcf11f00487e5">+15/-0</a>&nbsp;
&nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

</details>

___

(cherry picked from commit 8b4fa8e)
Copy link

tykbot bot commented Aug 11, 2025

@MaciekMis Created merge PRs

Copy link

tykbot bot commented Aug 11, 2025

@MaciekMis Release branch not found

buger added a commit that referenced this pull request Aug 11, 2025
…es unnecessary object (#7270)

[TT-5588] [OAS] gateway apiKey import generates unnecessary object (#7270)

### **User description**
<details open>
<summary><a href="https://tyktech.atlassian.net/browse/TT-5588"
title="TT-5588" target="_blank">TT-5588</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
      <td>[OAS] gateway apiKey import generates unnecessary object</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Bug"
src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10303?size=medium"
/>
        Bug
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Dev</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
<td><a
href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20codilime_refined%20ORDER%20BY%20created%20DESC"
title="codilime_refined">codilime_refined</a></td>
    </tr>
  </table>
</details>
<!--
  do not remove this marker as it will break jira-lint's functionality.
  added_by_jira_lint
-->

---

<!-- Provide a general summary of your changes in the Title above -->

## Description

The header object is unnecessarily generated during creation of a Tyk
OAS API by importing an OpenAPI description with security scheme
defined.

## Related Issue

<!-- This project only accepts pull requests related to open issues. -->
<!-- If suggesting a new feature or change, please discuss it in an
issue first. -->
<!-- If fixing a bug, there should be an issue describing it with steps
to reproduce. -->
<!-- OSS: Please link to the issue here. Tyk: please create/link the
JIRA ticket. -->

## Motivation and Context

<!-- Why is this change required? What problem does it solve? -->

## How This Has Been Tested

<!-- Please describe in detail how you tested your changes -->
<!-- Include details of your testing environment, and the tests -->
<!-- you ran to see how your change affects other areas of the code,
etc. -->
<!-- This information is helpful for reviewers and QA. -->

## Screenshots (if appropriate)

## Types of changes

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Refactoring or add test (improvements in base code or adds test
coverage to functionality)

## Checklist

<!-- Go over all the following points, and put an `x` in all the boxes
that apply -->
<!-- If there are no documentation updates required, mark the item as
checked. -->
<!-- Raise up any additional concerns not covered by the checklist. -->

- [ ] I ensured that the documentation is up to date
- [ ] I explained why this PR updates go.mod in detail with reasoning
why it's required
- [ ] I would like a code coverage CI quality gate exception and have
explained why


___

### **PR Type**
Bug fix, Tests


___

### **Description**
- Prevent `AuthSources` from being serialized in `Token` struct

- Update JSON serialization tags to exclude `AuthSources`

- Add test to verify `AuthSources` are not serialized

- Ensure unmarshalled `Token` omits `AuthSources` fields


___

### Diagram Walkthrough


```mermaid
flowchart LR
  TokenStruct["Token struct"]
  Serialization["JSON Serialization"]
  TestCase["Test: AuthSources not serialized"]
  TokenStruct -- "exclude AuthSources from JSON" --> Serialization
  Serialization -- "verify exclusion" --> TestCase
```



<details> <summary><h3> File Walkthrough</h3></summary>

<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Bug
fix</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>security.go</strong><dd><code>Exclude AuthSources from
Token JSON serialization</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; </dd></summary>
<hr>

apidef/oas/security.go

<ul><li>Changed JSON struct tag for <code>AuthSources</code> to exclude
from serialization<br> <li> Prevents <code>AuthSources</code> from
appearing in serialized JSON output</ul>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/7270/files#diff-15e7d47137452ca4f3f6139aa8c007cdb426152c41846f712f8bf5dfb607afcc">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Tests</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>security_test.go</strong><dd><code>Add test for
non-serialization of AuthSources in Token</code>&nbsp; &nbsp; &nbsp;
</dd></summary>
<hr>

apidef/oas/security_test.go

<ul><li>Added test to ensure <code>AuthSources</code> fields are not
serialized<br> <li> Verifies that <code>Query</code>,
<code>Header</code>, and <code>Cookie</code> are nil after serialization
<br>round-trip</ul>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/7270/files#diff-5184167309db0462243e424baca87b5bb668962d8cc1076629fdcf11f00487e5">+15/-0</a>&nbsp;
&nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

</details>

___
MaciekMis pushed a commit that referenced this pull request Aug 12, 2025
… generates unnecessary object (#7270)" (#7298)

### **User description**
<details open>
<summary><a href="https://tyktech.atlassian.net/browse/TT-5588"
title="TT-5588" target="_blank">TT-5588</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
      <td>[OAS] gateway apiKey import generates unnecessary object</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Bug"
src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10303?size=medium"
/>
        Bug
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Dev</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
<td><a
href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20codilime_refined%20ORDER%20BY%20created%20DESC"
title="codilime_refined">codilime_refined</a></td>
    </tr>
  </table>
</details>
<!--
  do not remove this marker as it will break jira-lint's functionality.
  added_by_jira_lint
-->

---

Reverts #7291


___

### **PR Type**
Bug fix, Tests


___

### **Description**
Revert exclusion of `AuthSources` from JSON.
Restore JSON inline serialization for `Token.AuthSources`.
Remove test asserting non-serialization of `AuthSources`.
Keep token fill logic and assertions intact.


___

### Diagram Walkthrough


```mermaid
flowchart LR
  Token["Token struct"]
  JSONTag["JSON tag for AuthSources"]
  Tests["Security tests"]
  Token -- "AuthSources json:',inline'" --> JSONTag
  Tests -- "remove non-serialization round-trip" --> JSONTag
```



<details> <summary><h3> File Walkthrough</h3></summary>

<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Bug
fix</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>security.go</strong><dd><code>Restore inline JSON
serialization for AuthSources</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

apidef/oas/security.go

<ul><li>Change <code>Token.AuthSources</code> tag to
<code>json:",inline"</code>.<br> <li> Re-enable JSON serialization of
embedded <code>AuthSources</code>.</ul>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/7298/files#diff-15e7d47137452ca4f3f6139aa8c007cdb426152c41846f712f8bf5dfb607afcc">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Tests</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>security_test.go</strong><dd><code>Remove test
asserting AuthSources non-serialization</code>&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

apidef/oas/security_test.go

<ul><li>Remove JSON round-trip test for <code>AuthSources</code>.<br>
<li> Drop <code>encoding/json</code> import no longer used.</ul>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/7298/files#diff-5184167309db0462243e424baca87b5bb668962d8cc1076629fdcf11f00487e5">+0/-15</a>&nbsp;
&nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

</details>

___
MaciekMis pushed a commit that referenced this pull request Aug 12, 2025
…bject" (#7299)

### **User description**
<details open>
<summary><a href="https://tyktech.atlassian.net/browse/TT-5588"
title="TT-5588" target="_blank">TT-5588</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
      <td>[OAS] gateway apiKey import generates unnecessary object</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Bug"
src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10303?size=medium"
/>
        Bug
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Dev</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
<td><a
href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20codilime_refined%20ORDER%20BY%20created%20DESC"
title="codilime_refined">codilime_refined</a></td>
    </tr>
  </table>
</details>
<!--
  do not remove this marker as it will break jira-lint's functionality.
  added_by_jira_lint
-->

---

Reverts #7270


___

### **PR Type**
Bug fix, Tests


___

### **Description**
Re-enable JSON inlining for `AuthSources`
Remove test asserting non-serialization
Keep token auth fill/roundtrip behavior intact
Align JSON tags with intended API shape


___

### Diagram Walkthrough


```mermaid
flowchart LR
  Token["Token struct"]
  AuthSources["AuthSources fields"]
  JSONTag["json:\",inline\""]
  TestRemoval["Remove non-serialization test"]

  Token -- contains --> AuthSources
  AuthSources -- applied via --> JSONTag
  JSONTag -- implies --> InlinedInJSON["Inlined in JSON output"]
  TestRemoval -- aligns with --> InlinedInJSON
```



<details> <summary><h3> File Walkthrough</h3></summary>

<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Bug
fix</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>security.go</strong><dd><code>Re-enable JSON inlining
for AuthSources in Token</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

apidef/oas/security.go

<ul><li>Change <code>Token.AuthSources</code> JSON tag to
<code>",inline"</code><br> <li> Revert exclusion from JSON
serialization</ul>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/7299/files#diff-15e7d47137452ca4f3f6139aa8c007cdb426152c41846f712f8bf5dfb607afcc">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Tests</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>security_test.go</strong><dd><code>Delete test
asserting AuthSources not serialized</code>&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

apidef/oas/security_test.go

<ul><li>Remove round-trip JSON test for <code>AuthSources</code>
omission<br> <li> Keep existing Token tests intact</ul>


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/7299/files#diff-5184167309db0462243e424baca87b5bb668962d8cc1076629fdcf11f00487e5">+0/-15</a>&nbsp;
&nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

</details>

___
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants