Skip to content

[TT-11966/TT-12064] refactor oas events, update contract#6263

Merged
jeffy-mathew merged 19 commits into
masterfrom
fix/TT-12064/refactor-OAS-events
May 9, 2024
Merged

[TT-11966/TT-12064] refactor oas events, update contract#6263
jeffy-mathew merged 19 commits into
masterfrom
fix/TT-12064/refactor-OAS-events

Conversation

@jeffy-mathew

@jeffy-mathew jeffy-mathew commented May 8, 2024

Copy link
Copy Markdown
Contributor

User description

  • refactor OAS events

  • update OAS schema

  • update Events contract

  • introduce ReadableTimeout to make use of shorthand notation for time.Duration in json contracts.

Related Issue

Parent task: https://tyktech.atlassian.net/browse/TT-11966
Sub task: https://tyktech.atlassian.net/browse/TT-12064

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

enhancement


Description

  • Introduced new structures (Event and WebhookEvent) in event.go to handle API events according to OAS specifications.
  • Implemented JSON marshaling/unmarshaling for these structures to ensure compatibility with OAS.
  • Developed methods to convert and manage event configurations, facilitating integration with existing API definitions.
  • Added comprehensive unit tests in event_test.go to validate the functionality of the new event handling features.
  • Updated the OAS schema in x-tyk-api-gateway.json to reflect the new event handling capabilities, ensuring accurate API documentation and client generation.

Changes walkthrough 📝

Relevant files
Enhancement
event.go
Implement Event Handling Structures and Methods                   

apidef/oas/event.go

  • Introduced new Event and WebhookEvent structures for handling API
    events.
  • Added JSON marshaling and unmarshaling for these structures to align
    with the OAS API definition.
  • Implemented methods to convert between internal event configurations
    and the new OAS structure.
  • Added comprehensive methods to manage event lists, including filling
    from and extracting to API definitions.
  • +193/-0 
    x-tyk-api-gateway.json
    Update OAS Schema for Enhanced Event Definitions                 

    apidef/oas/schema/x-tyk-api-gateway.json

  • Updated the OAS schema to include definitions for events with and
    without IDs.
  • Added new properties and required fields to the webhook event
    definitions.
  • +70/-51 
    Tests
    event_test.go
    Unit Tests for Event Handling in OAS                                         

    apidef/oas/event_test.go

  • Added extensive unit tests for the new event handling features.
  • Tests cover the conversion between internal and OAS event
    configurations.
  • Ensures correct behavior for both enabling and disabling events, and
    for handling webhook-specific settings.
  • +222/-0 

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

    @github-actions

    github-actions Bot commented May 8, 2024

    Copy link
    Copy Markdown
    Contributor

    API Changes

    --- prev.txt	2024-05-09 15:15:50.111713571 +0000
    +++ current.txt	2024-05-09 15:15:47.239713082 +0000
    @@ -2420,6 +2420,9 @@
     )
         Enumerated constants for inputs and conditions.
     
    +const WebhookKind = event.WebhookKind
    +    WebhookKind is an alias maintained to be used in imports.
    +
     
     VARIABLES
     
    @@ -2996,13 +2999,13 @@
     func (et *EnforceTimeout) Fill(meta apidef.HardTimeoutMeta)
         Fill fills *EnforceTimeout from apidef.HardTimeoutMeta.
     
    -type Event struct {
    +type EventHandler struct {
     	// Enabled enables the event handler.
     	Enabled bool `json:"enabled" bson:"enabled"`
    -	// Type specifies the TykEvent that should trigger the event handler.
    -	Type event.Event `json:"type" bson:"type"`
    -	// Action specifies the action to be taken on the event trigger.
    -	Action event.Action `json:"action" bson:"action"`
    +	// Trigger specifies the TykEvent that should trigger the event handler.
    +	Trigger event.Event `json:"trigger" bson:"trigger"`
    +	// Kind specifies the action to be taken on the event trigger.
    +	Kind Kind `json:"type" bson:"type"` // json tag is changed as per contract
     	// ID is the ID of event handler in storage.
     	ID string `json:"id,omitempty" bson:"id,omitempty"`
     	// Name is the name of event handler
    @@ -3010,26 +3013,27 @@
     
     	Webhook WebhookEvent `bson:"-" json:"-"`
     }
    -    Event holds information about individual event to be configured on the API.
    +    EventHandler holds information about individual event to be configured on
    +    the API.
     
    -func (e *Event) GetWebhookConf() apidef.WebHookHandlerConf
    -    GetWebhookConf converts Event.WebhookEvent apidef.WebHookHandlerConf.
    +func (e *EventHandler) GetWebhookConf() apidef.WebHookHandlerConf
    +    GetWebhookConf converts EventHandler.WebhookEvent apidef.WebHookHandlerConf.
     
    -func (e *Event) MarshalJSON() ([]byte, error)
    -    MarshalJSON marshals Event as per Tyk OAS API definition contract.
    +func (e EventHandler) MarshalJSON() ([]byte, error)
    +    MarshalJSON marshals EventHandler as per Tyk OAS API definition contract.
     
    -func (e *Event) UnmarshalJSON(in []byte) error
    -    UnmarshalJSON unmarshals Event as per Tyk OAS API definition contract.
    +func (e *EventHandler) UnmarshalJSON(in []byte) error
    +    UnmarshalJSON unmarshal EventHandler as per Tyk OAS API definition contract.
     
    -type Events []Event
    -    Events holds the list of events to be processed for the API.
    +type EventHandlers []EventHandler
    +    EventHandlers holds the list of events to be processed for the API.
     
    -func (e *Events) ExtractTo(api *apidef.APIDefinition)
    -    ExtractTo extracts events to apidef.APIDefinition.
    +func (e *EventHandlers) ExtractTo(api *apidef.APIDefinition)
    +    ExtractTo EventHandlers events to apidef.APIDefinition.
     
    -func (e *Events) Fill(api apidef.APIDefinition)
    -    Fill fills Events from classic API definition. Currently only webhook events
    -    are supported.
    +func (e *EventHandlers) Fill(api apidef.APIDefinition)
    +    Fill fills EventHandlers from classic API definition. Currently only webhook
    +    events are supported.
     
     type ExternalOAuth struct {
     	Enabled     bool `bson:"enabled" json:"enabled"` // required
    @@ -3196,6 +3200,15 @@
     }
         Header holds a header name and value pair.
     
    +type Headers []Header
    +    Headers is an array of Header.
    +
    +func NewHeaders(in map[string]string) Headers
    +    NewHeaders creates Headers from in map.
    +
    +func (hs Headers) Map() map[string]string
    +    Map transforms Headers into a map.
    +
     type IDExtractor struct {
     	// Enabled activates ID extractor with coprocess authentication.
     	Enabled bool `bson:"enabled" json:"enabled"` // required
    @@ -3363,6 +3376,9 @@
     
     func (j *JWTValidation) Fill(jwt apidef.JWTValidation)
     
    +type Kind = event.Kind
    +    Kind is an alias maintained to be used in imports.
    +
     type ListenPath struct {
     	// Value is the value of the listen path e.g. `/api/` or `/` or `/httpbin/`.
     	// Tyk classic API definition: `proxy.listen_path`
    @@ -3411,7 +3427,7 @@
     	// Body is the HTTP response body that will be returned.
     	Body string `bson:"body,omitempty" json:"body,omitempty"`
     	// Headers are the HTTP response headers that will be returned.
    -	Headers []Header `bson:"headers,omitempty" json:"headers,omitempty"`
    +	Headers Headers `bson:"headers,omitempty" json:"headers,omitempty"`
     	// FromOASExamples is the configuration to extract a mock response from OAS documentation.
     	FromOASExamples *FromOASExamples `bson:"fromOASExamples,omitempty" json:"fromOASExamples,omitempty"`
     }
    @@ -3838,7 +3854,7 @@
     	// be considered as 0s/empty.
     	//
     	// Tyk classic API definition: `global_rate_limit.per`.
    -	Per string `json:"per" bson:"per"`
    +	Per time.ReadableDuration `json:"per" bson:"per"`
     }
         RateLimit holds the configurations related to rate limit. The API-level rate
         limit applies a base-line limit on the frequency of requests to the upstream
    @@ -3852,6 +3868,9 @@
     func (r *RateLimit) Fill(api apidef.APIDefinition)
         Fill fills *RateLimit from apidef.APIDefinition.
     
    +type ReadableDuration = time.ReadableDuration
    +    ReadableDuration is an alias maintained to be used in imports.
    +
     type RequestSizeLimit struct {
     	// Enabled activates the Request Size Limit functionality.
     	Enabled bool `bson:"enabled" json:"enabled"`
    @@ -3954,7 +3973,7 @@
     	// Events contains the configuration related to Tyk Events.
     	//
     	// Tyk classic API definition: `event_handlers`
    -	Events Events `bson:"events,omitempty" json:"events,omitempty"`
    +	EventHandlers EventHandlers `bson:"eventHandlers,omitempty" json:"eventHandlers,omitempty"`
     }
         Server contains the configuration that sets Tyk up to receive requests from
         the client applications.
    @@ -4206,7 +4225,7 @@
     	// Remove specifies header names to be removed from the request/response.
     	Remove []string `bson:"remove,omitempty" json:"remove,omitempty"`
     	// Add specifies headers to be added to the request/response.
    -	Add []Header `bson:"add,omitempty" json:"add,omitempty"`
    +	Add Headers `bson:"add,omitempty" json:"add,omitempty"`
     }
         TransformHeaders holds configuration about request/response header
         transformations.
    @@ -4474,11 +4493,31 @@
         that were previously used to represent the same data.
     
     type WebhookEvent struct {
    -	URL          string            `json:"url" bson:"url"`
    -	Method       string            `json:"method" bson:"method"`
    -	Timeout      int64             `json:"timeout" bson:"timeout"`
    -	BodyTemplate string            `json:"bodyTemplate,omitempty" bson:"bodyTemplate,omitempty"`
    -	Headers      map[string]string `json:"headers,omitempty" bson:"headers,omitempty"`
    +	// URL is the target URL for the webhook.
    +	URL string `json:"url" bson:"url"`
    +	// Method is the HTTP method for the webhook.
    +	Method string `json:"method" bson:"method"`
    +	// CoolDownPeriod defines cool-down for the event, so it does not trigger again.
    +	// It uses shorthand notation.
    +	// The value of CoolDownPeriod is a string that specifies the interval in a compact form,
    +	// where hours, minutes and seconds are denoted by 'h', 'm' and 's' respectively.
    +	// Multiple units can be combined to represent the duration.
    +	//
    +	// Examples of valid shorthand notations:
    +	// - "1h"   : one hour
    +	// - "20m"  : twenty minutes
    +	// - "30s"  : thirty seconds
    +	// - "1m29s": one minute and twenty-nine seconds
    +	// - "1h30m" : one hour and thirty minutes
    +	//
    +	// An empty value is interpreted as "0s", implying no cool-down.
    +	// It's important to format the string correctly, as invalid formats will
    +	// be considered as 0s/empty.
    +	CoolDownPeriod time.ReadableDuration `json:"coolDownPeriod" bson:"coolDownPeriod"`
    +	// BodyTemplate is the template to be used for request payload.
    +	BodyTemplate string `json:"bodyTemplate,omitempty" bson:"bodyTemplate,omitempty"`
    +	// Headers are the list of request headers to be used.
    +	Headers Headers `json:"headers,omitempty" bson:"headers,omitempty"`
     }
         WebhookEvent stores the core information about a webhook event.
     

    @github-actions

    github-actions Bot commented May 8, 2024

    Copy link
    Copy Markdown
    Contributor

    PR Description updated to latest commit (e465b9e)

    @github-actions

    github-actions Bot commented May 8, 2024

    Copy link
    Copy Markdown
    Contributor

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    3, because the PR introduces a significant amount of new functionality related to event handling in the system, including JSON marshaling/unmarshaling, and integration with existing API definitions. The complexity of the changes and the potential impact on existing functionalities make this a moderately challenging PR to review thoroughly.

    🧪 Relevant tests

    Yes

    ⚡ Possible issues

    Possible Bug: The reflect.Cast function is used extensively for type conversion, which might not handle all edge cases or could lead to runtime panics if assumptions about types are incorrect.

    Error Handling: The error from reflect.Cast in MarshalJSON and UnmarshalJSON methods is handled, but there's no logging or further action taken which might make debugging issues in production environments difficult.

    🔒 Security concerns

    No

    Code feedback:
    relevant fileapidef/oas/event.go
    suggestion      

    Consider adding detailed logging for error scenarios in the MarshalJSON and UnmarshalJSON methods. This would help in diagnosing issues during the marshaling and unmarshaling processes, especially since reflection is used, which can be error-prone. [important]

    relevant linereturn nil, err

    relevant fileapidef/oas/event.go
    suggestion      

    Implement a fallback or alternative mechanism when reflect.Cast fails in the MarshalJSON method. This could prevent the method from failing and allow the system to handle the situation more gracefully, possibly by using a default configuration or simpler type assertion. [important]

    relevant linereturn nil, err

    relevant fileapidef/oas/event.go
    suggestion      

    Add validation for the WebhookEvent fields such as URL and Method to ensure they are in a valid format before processing them. This can prevent issues when these values are used in HTTP requests, improving the robustness of event handling. [medium]

    relevant lineURL string `json:"url" bson:"url"`

    relevant fileapidef/oas/event.go
    suggestion      

    Consider handling cases where the Events slice is nil in the ExtractTo method. This would prevent potential nil pointer dereferences when there are no events configured but the method is called. [medium]

    relevant lineif e == nil || len(*e) == 0 {

    @github-actions

    github-actions Bot commented May 8, 2024

    Copy link
    Copy Markdown
    Contributor

    PR Code Suggestions ✨

    CategorySuggestions                                                                                                                                                       
    Bug
    Handle potential nil pointer dereference by checking errors immediately after calls that might fail.

    **Consider handling the error from reflect.Cast directly in the MarshalJSON method to avoid
    potential nil pointer dereference when accessing outMap or webhookMap if reflect.Cast
    fails.

    apidef/oas/event.go [33-41]

     outMap, err := reflect.Cast[map[string]interface{}](*e)
     if err != nil {
       return nil, err
     }
     webhookMap, err := reflect.Cast[map[string]interface{}](e.Webhook)
     if err != nil {
       return nil, err
     }
    +outMapVal := *outMap
    +for k, v := range *webhookMap {
    +  outMapVal[k] = v
    +}
     
    Add error handling after type casting to prevent runtime errors.

    Consider adding error handling for the reflect.Cast operation within the ExtractTo method
    to ensure robustness and avoid runtime panics on type assertion failures.

    apidef/oas/event.go [171]

     handlerMeta, err = reflect.Cast[map[string]interface{}](whConf)
    +if err != nil {
    +  log.WithError(err).Error("failed to cast webhook config to map")
    +  continue
    +}
     
    Maintainability
    Improve code readability by using descriptive variable names.

    Use a more descriptive variable name than e for the Events receiver in methods to improve
    code readability and maintainability.

    apidef/oas/event.go [94]

    -func (e *Events) Fill(api apidef.APIDefinition) {
    +func (events *Events) Fill(api apidef.APIDefinition) {
     
    Enhancement
    Use functional programming styles to simplify and clarify event filtering logic.

    Instead of manually filtering out non-webhook events, consider using a more functional
    style with higher-order functions like filter to improve code clarity and reduce
    boilerplate.

    apidef/oas/event.go [149-155]

    -for _, eventTrigger := range eventTriggers {
    -  if eventTrigger.Handler == event.WebHookHandler {
    -    continue
    -  }
    -  triggersExcludingWebhooks = append(triggersExcludingWebhooks, eventTrigger)
    -}
    +triggersExcludingWebhooks := filter(eventTriggers, func(et apidef.EventHandlerTriggerConfig) bool {
    +  return et.Handler != event.WebHookHandler
    +})
     
    Best practice
    Avoid modifying method receivers directly to ensure thread safety.

    To avoid potential data races or unexpected behavior in concurrent environments, consider
    not modifying the receiver directly in the Fill method but instead returning a new
    instance of Events.

    apidef/oas/event.go [132]

    -*e = events
    +return events
     

    @jeffy-mathew
    jeffy-mathew requested a review from titpetric May 8, 2024 16:35
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 8, 2024
    @github-actions

    github-actions Bot commented May 9, 2024

    Copy link
    Copy Markdown
    Contributor

    💥 CI tests failed 🙈

    git-state

    all ok

    Please look at the run or in the Checks tab.

    @jeffy-mathew jeffy-mathew changed the title [TT-12064] refactor oas events [TT-12064] refactor oas events, update contract May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @jeffy-mathew
    jeffy-mathew force-pushed the fix/TT-12064/refactor-OAS-events branch 2 times, most recently from f01b8ec to dfe9792 Compare May 9, 2024 10:07
    @jeffy-mathew
    jeffy-mathew force-pushed the fix/TT-12064/refactor-OAS-events branch from dfe9792 to e0a59b5 Compare May 9, 2024 10:09
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @github-actions

    github-actions Bot commented May 9, 2024

    Copy link
    Copy Markdown
    Contributor

    💥 CI tests failed 🙈

    git-state

    all ok

    Please look at the run or in the Checks tab.

    @jeffy-mathew
    jeffy-mathew force-pushed the fix/TT-12064/refactor-OAS-events branch from 083db91 to e731c72 Compare May 9, 2024 10:26
    Comment thread apidef/oas/header.go Outdated
    Comment thread apidef/oas/event.go Outdated
    Comment thread apidef/oas/event.go Outdated
    Comment thread apidef/oas/event.go
    Comment thread .gitignore Outdated
    ### **User description**
    <!-- Provide a general summary of your changes in the Title above -->
    
    ## Description
    
    <!-- Describe your changes in detail -->
    
    ## Related Issue
    Parent: https://tyktech.atlassian.net/browse/TT-11966
    Subtask: [TT-12064](https://tyktech.atlassian.net/browse/TT-12064)
    
    ## 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**
    enhancement
    
    
    ___
    
    ### **Description**
    - Introduced a new type `ReadableDuration` to handle duration fields in
    a user-friendly format.
    - Updated `WebhookEvent`, `RateLimit`, and related tests to use
    `ReadableDuration`.
    - Simplified duration parsing and formatting throughout the codebase
    using the new type.
    - Added comprehensive tests for the new `ReadableDuration` type.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Enhancement
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event.go</strong><dd><code>Use ReadableDuration for
    WebhookEvent CoolDownPeriod</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    </dd></summary>
    <hr>
    
    apidef/oas/event.go
    <li>Introduced <code>ReadableDuration</code> type for
    <code>CoolDownPeriod</code> in <code>WebhookEvent</code>.<br> <li>
    Simplified <code>GetWebhookConf</code> by directly using
    <code>Seconds()</code> method from
    <br><code>ReadableDuration</code>.<br> <li> Adjusted <code>Fill</code>
    method to use <code>ReadableDuration</code> for initializing
    <br><code>CoolDownPeriod</code>.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-528a9f5b311ff21c0b3a9b273e61398209ca8b51550327e4d437bba81e49d577">+10/-14</a>&nbsp;
    </td>
    </tr>                    
    
    <tr>
      <td>
        <details>
    <summary><strong>time.go</strong><dd><code>Define ReadableDuration Type
    Alias</code>&nbsp; &nbsp; &nbsp; &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/time.go
    <li>Created a new file to define <code>ReadableDuration</code> as an
    alias for <br><code>time.Duration</code>.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-56f5ed8764cba1fab5cc4615f02a769b2c9124e69b5972cb1b7921d1bded577b">+6/-0</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    
    <tr>
      <td>
        <details>
    <summary><strong>upstream.go</strong><dd><code>Implement
    ReadableDuration in Upstream RateLimit</code>&nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    apidef/oas/upstream.go
    <li>Replaced string type with <code>ReadableDuration</code> for
    <code>Per</code> in <code>RateLimit</code>.<br> <li> Simplified
    <code>Fill</code> and <code>ExtractTo</code> methods by using
    <code>ReadableDuration</code>.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-7b0941c7f37fe5a2a23047e0822a65519ca11c371660f36555b59a60f000e3f4">+4/-12</a>&nbsp;
    &nbsp; </td>
    </tr>                    
    
    <tr>
      <td>
        <details>
    <summary><strong>duration.go</strong><dd><code>Implement Custom
    ReadableDuration Type</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    internal/time/duration.go
    <li>Implemented <code>ReadableDuration</code> type with custom JSON
    marshal/unmarshal <br>methods.<br> <li> Added methods for parsing and
    formatting durations in a user-friendly <br>format.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-6e8ef3118f84cbcc935f27d5a3ad5f4eb86eb22728400e9322c9b796b9d8d855">+57/-0</a>&nbsp;
    &nbsp; </td>
    </tr>                    
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event_test.go</strong><dd><code>Update Event Tests to
    Use ReadableDuration</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    </dd></summary>
    <hr>
    
    apidef/oas/event_test.go
    - Updated test cases to use `ReadableDuration` for `CoolDownPeriod`.
    
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-a1f19f96579a470e73c131a0e37895da0c21d378f6aa48067608274564e62da7">+8/-7</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    
    <tr>
      <td>
        <details>
    <summary><strong>linter_test.go</strong><dd><code>Update Linter Tests
    for ReadableDuration Usage</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    apidef/oas/linter_test.go
    <li>Updated linter tests to use <code>ReadableDuration</code> for
    <code>CoolDownPeriod</code> and <br><code>Per</code>.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-b92239afd81e77a829fe7fe8410044dfd4dfda525d17dbf5f8811714a9c986d3">+3/-2</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    
    <tr>
      <td>
        <details>
    <summary><strong>upstream_test.go</strong><dd><code>Update Upstream
    Tests for ReadableDuration</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    </dd></summary>
    <hr>
    
    apidef/oas/upstream_test.go
    - Updated tests to use `ReadableDuration` for `Per` in `RateLimit`.
    
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-222cc254c0c6c09fa0cf50087860b837a0873e2aef3c84ec7d80b1014c149057">+2/-24</a>&nbsp;
    &nbsp; </td>
    </tr>                    
    
    <tr>
      <td>
        <details>
    <summary><strong>duration_test.go</strong><dd><code>Test Custom
    ReadableDuration Type</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    internal/time/duration_test.go
    <li>Added tests for marshaling and unmarshaling
    <code>ReadableDuration</code>.<br> <li> Tested parsing and formatting of
    various duration strings.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6265/files#diff-71942cdc77128266498b62e712f82d0c63bbb39d236fe9e6677f49080c28cea1">+59/-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
    @jeffy-mathew jeffy-mathew changed the title [TT-12064] refactor oas events, update contract [TT-11966/TT-12064] refactor oas events, update contract May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from github-actions Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from sonarqubecloud Bot May 9, 2024
    @TykTechnologies TykTechnologies deleted a comment from sonarqubecloud Bot May 9, 2024
    @sonarqubecloud

    sonarqubecloud Bot commented May 9, 2024

    Copy link
    Copy Markdown

    @jeffy-mathew
    jeffy-mathew enabled auto-merge (squash) May 9, 2024 15:30
    @jeffy-mathew
    jeffy-mathew merged commit f304528 into master May 9, 2024
    @jeffy-mathew
    jeffy-mathew deleted the fix/TT-12064/refactor-OAS-events branch May 9, 2024 15:35
    nerdydread pushed a commit that referenced this pull request Sep 6, 2024
    ### **User description**
    - refactor OAS events
    
    - update OAS schema
    - update Events contract
    - introduce ReadableTimeout to make use of shorthand notation for
    time.Duration in json contracts.
    
    ## Related Issue
    Parent task: https://tyktech.atlassian.net/browse/TT-11966
    Sub task: https://tyktech.atlassian.net/browse/TT-12064
    
    ## 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)
    - [x] 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**
    enhancement
    
    
    ___
    
    ### **Description**
    - Introduced new structures (`Event` and `WebhookEvent`) in `event.go`
    to handle API events according to OAS specifications.
    - Implemented JSON marshaling/unmarshaling for these structures to
    ensure compatibility with OAS.
    - Developed methods to convert and manage event configurations,
    facilitating integration with existing API definitions.
    - Added comprehensive unit tests in `event_test.go` to validate the
    functionality of the new event handling features.
    - Updated the OAS schema in `x-tyk-api-gateway.json` to reflect the new
    event handling capabilities, ensuring accurate API documentation and
    client generation.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Enhancement
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event.go</strong><dd><code>Implement Event Handling
    Structures and Methods</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    apidef/oas/event.go
    <li>Introduced new <code>Event</code> and <code>WebhookEvent</code>
    structures for handling API <br>events.<br> <li> Added JSON marshaling
    and unmarshaling for these structures to align <br>with the OAS API
    definition.<br> <li> Implemented methods to convert between internal
    event configurations <br>and the new OAS structure.<br> <li> Added
    comprehensive methods to manage event lists, including filling <br>from
    and extracting to API definitions.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6263/files#diff-528a9f5b311ff21c0b3a9b273e61398209ca8b51550327e4d437bba81e49d577">+193/-0</a>&nbsp;
    </td>
    </tr>                    
    
    <tr>
      <td>
        <details>
    <summary><strong>x-tyk-api-gateway.json</strong><dd><code>Update OAS
    Schema for Enhanced Event Definitions</code>&nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    apidef/oas/schema/x-tyk-api-gateway.json
    <li>Updated the OAS schema to include definitions for events with and
    <br>without IDs.<br> <li> Added new properties and required fields to
    the webhook event <br>definitions.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6263/files#diff-78828969c0c04cc1a776dfc93a8bad3c499a8c83e6169f83e96d090bed3e7dd0">+70/-51</a>&nbsp;
    </td>
    </tr>                    
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event_test.go</strong><dd><code>Unit Tests for Event
    Handling in OAS</code>&nbsp; &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/event_test.go
    <li>Added extensive unit tests for the new event handling features.<br>
    <li> Tests cover the conversion between internal and OAS event
    <br>configurations.<br> <li> Ensures correct behavior for both enabling
    and disabling events, and <br>for handling webhook-specific
    settings.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6263/files#diff-a1f19f96579a470e73c131a0e37895da0c21d378f6aa48067608274564e62da7">+222/-0</a>&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
    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