Skip to content
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

Merging to release-5.5: [TT-12762]respect response plugins contract over responsePlugin.plugins (#6441) #6443

Conversation

buger
Copy link
Member

@buger buger commented Aug 2, 2024

User description

TT-12762respect response plugins contract over responsePlugin.plugins (#6441)

User description

Description

Fix a bug where response plugins are not loaded when using OAS APIs.

Related Issue

https://tyktech.atlassian.net/browse/TT-12762

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

  • Fixed a bug where response plugins were not loaded correctly when
    using OAS APIs by adding a return statement after extracting response
    plugins.
  • Added a new test case to verify the correct extraction of response
    plugins and ensure the regression issue TT-12762 is resolved.

Changes walkthrough 📝

Relevant files
Bug fix
middleware.go
Fix response plugins extraction logic                                       

apidef/oas/middleware.go

  • Added a return statement after extracting response plugins to ensure
    correct behavior.
  • Initialized ResponsePlugin if it is nil.
  • +1/-0     
    Tests
    middleware_test.go
    Add test for response plugins extraction                                 

    apidef/oas/middleware_test.go

  • Added a new test case to verify the correct extraction of response
    plugins.
  • Ensured the test case checks for the regression issue TT-12762.
  • +56/-0   

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools
    and their descriptions


    PR Type

    Bug fix, Tests


    Description

    • Fixed a bug where response plugins were not loaded correctly when using OAS APIs by adding a return statement after extracting response plugins.
    • Added a new test case to verify the correct loading and extraction of response plugins.

    Changes walkthrough 📝

    Relevant files
    Bug fix
    middleware.go
    Ensure response plugins are loaded correctly in OAS APIs 

    apidef/oas/middleware.go

  • Added a return statement after extracting response plugins to ensure
    proper loading.
  • +1/-0     
    Tests
    middleware_test.go
    Add test cases for response plugins in middleware               

    apidef/oas/middleware_test.go

  • Added a new test case for response plugins.
  • Verified the length of response plugins after extraction.
  • Ensured proper filling of middleware with response plugins.
  • +56/-0   

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    …ns (#6441)
    
    ### **User description**
    <!-- Provide a general summary of your changes in the Title above -->
    
    ## Description
    
    Fix a bug where response plugins are not loaded when using OAS APIs.
    ## Related Issue
    https://tyktech.atlassian.net/browse/TT-12762
    
    ## 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: -->
    
    - [ ] 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**
    - Fixed a bug where response plugins were not loaded correctly when
    using OAS APIs by adding a return statement after extracting response
    plugins.
    - Added a new test case to verify the correct extraction of response
    plugins and ensure the regression issue TT-12762 is resolved.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <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>middleware.go</strong><dd><code>Fix response plugins
    extraction logic</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    apidef/oas/middleware.go
    
    <li>Added a return statement after extracting response plugins to ensure
    <br>correct behavior.<br> <li> Initialized <code>ResponsePlugin</code>
    if it is nil.<br>
    
    
    </details>
    
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6441/files#diff-992ec7c28d25fd54f6491d295389757705cd114bc869a35cba50d42e548cdc6e">+1/-0</a>&nbsp;
    &nbsp; &nbsp; </td>
    
    </tr>                    
    </table></td></tr><tr><td><strong>Tests</strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>middleware_test.go</strong><dd><code>Add test for
    response plugins extraction</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; </dd></summary>
    <hr>
    
    apidef/oas/middleware_test.go
    
    <li>Added a new test case to verify the correct extraction of response
    <br>plugins.<br> <li> Ensured the test case checks for the regression
    issue TT-12762.<br>
    
    
    </details>
    
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6441/files#diff-0af31cb29ae298a6ac3e402b283ab364a6fd793fd04f253ef7c4983234c17bef">+56/-0</a>&nbsp;
    &nbsp; </td>
    
    </tr>                    
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > 💡 **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools
    and their descriptions
    
    (cherry picked from commit 5689c82)
    @buger buger merged commit 5c5652d into release-5.5 Aug 2, 2024
    @buger buger deleted the merge/release-5.5/5689c829c82c377a7e767e681ebc2d158393ffaf branch August 2, 2024 07:26
    Copy link
    Contributor

    github-actions bot commented Aug 2, 2024

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Early Return Logic
    The addition of the return statement at line 417 might cause the subsequent logic to be skipped, which could lead to unintended behavior if there are additional conditions or setups needed after the if block handling g.ResponsePlugins.

    Copy link
    Contributor

    github-actions bot commented Aug 2, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Add detailed assertions for plugin properties in the test case

    The test case "response plugins" lacks assertions for the properties of the plugins
    within convertedAPI.CustomMiddleware.Response. To ensure that not only the count but
    also the content of the plugins is correctly transferred, add assertions to check
    individual plugin properties like Enabled, FunctionName, and Path.

    apidef/oas/middleware_test.go [113]

     assert.Equal(t, len(responsePlugins), len(convertedAPI.CustomMiddleware.Response))
    +for i, plugin := range responsePlugins {
    +    assert.Equal(t, plugin.Enabled, convertedAPI.CustomMiddleware.Response[i].Enabled)
    +    assert.Equal(t, plugin.FunctionName, convertedAPI.CustomMiddleware.Response[i].FunctionName)
    +    assert.Equal(t, plugin.Path, convertedAPI.CustomMiddleware.Response[i].Path)
    +}
     
    Suggestion importance[1-10]: 9

    Why: Adding detailed assertions for the properties of the plugins ensures that the content is correctly transferred, not just the count. This improves the robustness and accuracy of the test.

    9
    Possible issue
    Remove the premature return statement to allow further processing

    The return statement at line 417 might prematurely exit the extractResponsePluginsTo
    function, skipping any logic that might be needed for handling g.ResponsePlugin when
    g.ResponsePlugins is not nil. Consider removing the return statement to allow the
    function to continue its execution for any subsequent checks or logic.

    apidef/oas/middleware.go [417]

    -return
    +// return removed to allow further processing
     
    Suggestion importance[1-10]: 8

    Why: Removing the return statement ensures that the function continues to process any subsequent logic, which might be necessary for handling g.ResponsePlugin. This change addresses a potential functional issue.

    8
    Best practice
    Verify the state of convertedAPI after setting disabled flags in the test

    The test case "response plugins" initializes convertedAPI but does not explicitly
    check if SetDisabledFlags modifies any relevant properties before calling ExtractTo.
    To ensure clarity and correctness of the test, verify the state of convertedAPI
    after calling SetDisabledFlags.

    apidef/oas/middleware_test.go [108]

     var convertedAPI apidef.APIDefinition
     convertedAPI.SetDisabledFlags()
    +// Example assertion, adjust according to actual implementation details
    +assert.False(t, convertedAPI.Disabled)
     
    Suggestion importance[1-10]: 7

    Why: Verifying the state of convertedAPI after calling SetDisabledFlags adds clarity and ensures correctness, although it is a minor enhancement to the test.

    7
    Maintainability
    Refine assertions to target specific fields for better test maintainability

    The test case "response plugins" uses a hardcoded assertion for comparing entire
    middleware structures which might lead to fragile tests if the middleware structure
    is extended or modified. Use a more targeted approach to assert specific fields
    relevant to the test to improve test maintainability and clarity.

    apidef/oas/middleware_test.go [128]

    -assert.Equal(t, expectedMW, resultMiddleware)
    +assert.Equal(t, expectedMW.Global.ResponsePlugins, resultMiddleware.Global.ResponsePlugins)
     
    Suggestion importance[1-10]: 6

    Why: Using more targeted assertions improves test maintainability and clarity, but the current assertion is not necessarily incorrect. This suggestion is more about improving future-proofing.

    6

    Copy link

    sonarqubecloud bot commented Aug 2, 2024

    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.

    2 participants