Skip to content

Commit

Permalink
Add tests to verify yaml conversions work (#6819)
Browse files Browse the repository at this point in the history
### **User description**
<!-- Provide a general summary of your changes in the Title above -->

## Description
This PR adds a test to verify that no additional yaml struct tags aren't
required on OAS structs to support yaml while defining Tyk OAS APIs.

kin-openapi supports parsing yaml openapi specifications making use of
json struct tags.
[kin-openapi/go.mod at cea0a13b906a708102947f95b9b09d631ff60976 ·
getkin/kin-openapi](https://github.com/getkin/kin-openapi/blob/cea0a13b906a708102947f95b9b09d631ff60976/go.mod#L9)
it can be seen that kin-openapi is using [GitHub - oasdiff/yaml: A
better way to marshal and unmarshal YAML in
Golang](https://github.com/oasdiff/yaml) which explains
[here](https://github.com/oasdiff/yaml) that it converts yaml ↔︎ json
internally. This means we don’t have to have additional struct tags for
yaml.

## Related Issue
https://tyktech.atlassian.net/browse/TT-13783
## 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**
Tests, Enhancement


___

### **Description**
- Added a new test `TestYaml` to validate YAML and JSON conversions.

- Introduced dependency on `github.com/oasdiff/yaml` for YAML handling.

- Updated `go.mod` and `go.sum` to include new YAML-related
dependencies.

- Enhanced test coverage for OpenAPI and Tyk extension integration.


___



### **Changes walkthrough** 📝
<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Tests</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>oas_test.go</strong><dd><code>Added `TestYaml` for YAML
and JSON conversion validation</code>&nbsp; </dd></summary>
<hr>

apidef/oas/oas_test.go

<li>Added a new test <code>TestYaml</code> for YAML and JSON
conversion.<br> <li> Validated Tyk extension integration with OpenAPI
structures.<br> <li> Used <code>yaml.JSONToYAML</code> for JSON to YAML
conversion.<br> <li> Ensured equality between original and converted
objects.


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6819/files#diff-74029ee88132d30d6478c96a35f8bb2200e0c8e6f42f2c9b147dc6bb7ce74644">+54/-0</a>&nbsp;
&nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Dependencies</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>go.mod</strong><dd><code>Updated dependencies for YAML
handling</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; </dd></summary>
<hr>

go.mod

<li>Added <code>github.com/oasdiff/yaml</code> as a direct
dependency.<br> <li> Added <code>github.com/oasdiff/yaml3</code> as an
indirect dependency.


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6819/files#diff-33ef32bf6c23acb95f5902d7097b7a1d5128ca061167ec0716715b0b9eeaa5f6">+2/-0</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td>
    <details>
<summary><strong>go.sum</strong><dd><code>Updated checksums for new YAML
dependencies</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

go.sum

<li>Added checksums for <code>github.com/oasdiff/yaml</code> and
<br><code>github.com/oasdiff/yaml3</code>.<br> <li> Updated dependency
metadata for YAML-related libraries.


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6819/files#diff-3295df7234525439d778f1b282d146a4f1ff6b415248aaac074e8042d9f42d63">+4/-0</a>&nbsp;
&nbsp; &nbsp; </td>

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

___

> 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull
request to receive relevant information
  • Loading branch information
jeffy-mathew authored Jan 8, 2025
1 parent cc71abc commit 4304645
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
54 changes: 54 additions & 0 deletions apidef/oas/oas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"net/http"
"testing"

"github.com/oasdiff/yaml"

"github.com/TykTechnologies/storage/persistent/model"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -1310,3 +1314,53 @@ func TestAPIContext_getValidationOptionsFromConfig(t *testing.T) {
assert.Len(t, options, 0)
})
}

func TestYaml(t *testing.T) {
oasDoc := OAS{}
Fill(t, &oasDoc, 0)

tykExt := XTykAPIGateway{}
Fill(t, &tykExt, 0)
// json unmarshal workarounds
{
tykExt.Info.DBID = model.NewObjectID()
tykExt.Middleware.Global.PrePlugin = nil
tykExt.Middleware.Global.PostPlugin = nil
tykExt.Middleware.Global.PostAuthenticationPlugin = nil
tykExt.Middleware.Global.ResponsePlugin = nil

for k, v := range tykExt.Server.Authentication.SecuritySchemes {
intVal, ok := v.(int)
assert.True(t, ok)
tykExt.Server.Authentication.SecuritySchemes[k] = float64(intVal)
}

for k, v := range tykExt.Middleware.Global.PluginConfig.Data.Value {
intVal, ok := v.(int)
assert.True(t, ok)
tykExt.Middleware.Global.PluginConfig.Data.Value[k] = float64(intVal)
}
}

oasDoc.SetTykExtension(&tykExt)

jsonBody, err := json.Marshal(&oasDoc)
assert.NoError(t, err)

yamlBody, err := yaml.JSONToYAML(jsonBody)
assert.NoError(t, err)

yamlOAS, err := openapi3.NewLoader().LoadFromData(yamlBody)
assert.NoError(t, err)

yamlOASDoc := OAS{
T: *yamlOAS,
}

yamlOASExt := yamlOASDoc.GetTykExtension()
assert.Equal(t, tykExt, *yamlOASExt)

yamlOASDoc.SetTykExtension(nil)
oasDoc.SetTykExtension(nil)
assert.Equal(t, oasDoc, yamlOASDoc)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ require (
github.com/nats-io/nats.go v1.38.0
github.com/newrelic/go-agent/v3 v3.35.1
github.com/newrelic/go-agent/v3/integrations/nrgorilla v1.2.2
github.com/oasdiff/yaml v0.0.0-20241214135536-5f7845c759c8
github.com/testcontainers/testcontainers-go v0.34.0
github.com/testcontainers/testcontainers-go/modules/kafka v0.33.0
github.com/testcontainers/testcontainers-go/modules/nats v0.33.0
Expand Down Expand Up @@ -231,6 +232,7 @@ require (
github.com/nats-io/nkeys v0.4.9 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/nats-io/stan.go v0.10.4 // indirect
github.com/oasdiff/yaml3 v0.0.0-20241214160948-977117996672 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@ github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1/go.mod h1:mpRZBD8SJ55
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/oasdiff/yaml v0.0.0-20241214135536-5f7845c759c8 h1:9djga8U4+/TQzv5iMlZHZ/qbGQB9V2nlnk2bmiG+uBs=
github.com/oasdiff/yaml v0.0.0-20241214135536-5f7845c759c8/go.mod h1:7tFDb+Y51LcDpn26GccuUgQXUk6t0CXZsivKjyimYX8=
github.com/oasdiff/yaml3 v0.0.0-20241214160948-977117996672 h1:+273wgr7to5QhwOOBE5LwjdNDFAI+8cbJVfB0Zj75aI=
github.com/oasdiff/yaml3 v0.0.0-20241214160948-977117996672/go.mod h1:y5+oSEHCPT/DGrS++Wc/479ERge0zTFxaF8PbGKcg2o=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down

0 comments on commit 4304645

Please sign in to comment.