Skip to content

Merging to release-5.8: [TT-5588] [OAS] gateway apiKey import generates unnecessary object (#7270) #7291

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

Merged

Conversation

buger
Copy link
Member

@buger buger commented Aug 11, 2025

User description

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

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   


PR Type

Bug fix, Tests


Description

  • Stop serializing Token.AuthSources to JSON

  • Update struct tag to json:"-"

  • Add test validating non-serialization

  • Ensure round-trip omits AuthSources fields


Diagram Walkthrough

flowchart LR
  Token["Token struct (AuthSources)"]
  JSON["JSON serialization"]
  Test["Test ensures omission"]
  Token -- "json:\"-\" on AuthSources" --> JSON
  JSON -- "round-trip marshal/unmarshal" --> Test
Loading

File Walkthrough

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

apidef/oas/security.go

  • Change Token.AuthSources JSON tag to json:"-".
  • Prevent inline serialization of auth sources.
+1/-1     
Tests
security_test.go
Test non-serialization of AuthSources                                       

apidef/oas/security_test.go

  • Add JSON round-trip test for Token.
  • Verify Query, Header, Cookie remain nil after marshal/unmarshal.
  • Import encoding/json for serialization.
+15/-0   

…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)
@buger buger enabled auto-merge (squash) August 11, 2025 09:22
Copy link
Contributor

API Changes

--- prev.txt	2025-08-11 09:23:36.540342635 +0000
+++ current.txt	2025-08-11 09:23:32.046325981 +0000
@@ -4505,7 +4505,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

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Preserve unmarshaling while omitting output

Using json:"-" removes AuthSources from all JSON serializations, which may break
upstream consumers that rely on reading these fields. If the intent is to skip only
marshaling while still allowing unmarshaling, implement custom MarshalJSON that
omits AuthSources but a UnmarshalJSON that populates it.

apidef/oas/security.go [33]

-AuthSources `bson:",inline" json:"-"`
+type Token struct {
+	Enabled      bool        `bson:"enabled" json:"enabled"`
+	AuthSources  `bson:",inline" json:"-"`
+	// ... other fields ...
+}
 
+func (t Token) MarshalJSON() ([]byte, error) {
+	type alias Token
+	type view struct {
+		Enabled bool `json:"enabled"`
+		// add other exported fields that should be serialized, excluding AuthSources
+	}
+	v := view{
+		Enabled: t.Enabled,
+	}
+	return json.Marshal(v)
+}
+
+func (t *Token) UnmarshalJSON(data []byte) error {
+	type alias Token
+	// First unmarshal to alias to keep all fields (including AuthSources) available
+	aux := struct {
+		Enabled bool `json:"enabled"`
+		// include other serialized fields
+		// inline sources to allow their presence without emitting them
+		Query  *AuthSource `json:"query,omitempty"`
+		Header *AuthSource `json:"header,omitempty"`
+		Cookie *AuthSource `json:"cookie,omitempty"`
+	}{}
+	if err := json.Unmarshal(data, &aux); err != nil {
+		return err
+	}
+	t.Enabled = aux.Enabled
+	// preserve ability to accept incoming AuthSources if present
+	t.Query = aux.Query
+	t.Header = aux.Header
+	t.Cookie = aux.Cookie
+	return nil
+}
+
Suggestion importance[1-10]: 3

__

Why: The existing code intentionally uses json:"-" and the test asserts that AuthSources fields are not serialized, aligning with the PR’s goal. Proposing custom Marshal/Unmarshal is unnecessary and potentially overcomplicates the model without demonstrated need; it may also contradict the test that ensures these fields are omitted.

Low

Copy link

@buger buger merged commit d194364 into release-5.8 Aug 11, 2025
37 of 38 checks passed
@buger buger deleted the merge/release-5.8/8b4fa8e74dfd920488cf4580066186e749c6c6e7 branch August 11, 2025 09:54
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>

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

Successfully merging this pull request may close these issues.

2 participants