Skip to content

Commit 4304645

Browse files
authored
Add tests to verify yaml conversions work (TykTechnologies#6819)
### **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
1 parent cc71abc commit 4304645

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

apidef/oas/oas_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"net/http"
77
"testing"
88

9+
"github.com/oasdiff/yaml"
10+
11+
"github.com/TykTechnologies/storage/persistent/model"
12+
913
"github.com/getkin/kin-openapi/openapi3"
1014
"github.com/stretchr/testify/assert"
1115

@@ -1310,3 +1314,53 @@ func TestAPIContext_getValidationOptionsFromConfig(t *testing.T) {
13101314
assert.Len(t, options, 0)
13111315
})
13121316
}
1317+
1318+
func TestYaml(t *testing.T) {
1319+
oasDoc := OAS{}
1320+
Fill(t, &oasDoc, 0)
1321+
1322+
tykExt := XTykAPIGateway{}
1323+
Fill(t, &tykExt, 0)
1324+
// json unmarshal workarounds
1325+
{
1326+
tykExt.Info.DBID = model.NewObjectID()
1327+
tykExt.Middleware.Global.PrePlugin = nil
1328+
tykExt.Middleware.Global.PostPlugin = nil
1329+
tykExt.Middleware.Global.PostAuthenticationPlugin = nil
1330+
tykExt.Middleware.Global.ResponsePlugin = nil
1331+
1332+
for k, v := range tykExt.Server.Authentication.SecuritySchemes {
1333+
intVal, ok := v.(int)
1334+
assert.True(t, ok)
1335+
tykExt.Server.Authentication.SecuritySchemes[k] = float64(intVal)
1336+
}
1337+
1338+
for k, v := range tykExt.Middleware.Global.PluginConfig.Data.Value {
1339+
intVal, ok := v.(int)
1340+
assert.True(t, ok)
1341+
tykExt.Middleware.Global.PluginConfig.Data.Value[k] = float64(intVal)
1342+
}
1343+
}
1344+
1345+
oasDoc.SetTykExtension(&tykExt)
1346+
1347+
jsonBody, err := json.Marshal(&oasDoc)
1348+
assert.NoError(t, err)
1349+
1350+
yamlBody, err := yaml.JSONToYAML(jsonBody)
1351+
assert.NoError(t, err)
1352+
1353+
yamlOAS, err := openapi3.NewLoader().LoadFromData(yamlBody)
1354+
assert.NoError(t, err)
1355+
1356+
yamlOASDoc := OAS{
1357+
T: *yamlOAS,
1358+
}
1359+
1360+
yamlOASExt := yamlOASDoc.GetTykExtension()
1361+
assert.Equal(t, tykExt, *yamlOASExt)
1362+
1363+
yamlOASDoc.SetTykExtension(nil)
1364+
oasDoc.SetTykExtension(nil)
1365+
assert.Equal(t, oasDoc, yamlOASDoc)
1366+
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ require (
9494
github.com/nats-io/nats.go v1.38.0
9595
github.com/newrelic/go-agent/v3 v3.35.1
9696
github.com/newrelic/go-agent/v3/integrations/nrgorilla v1.2.2
97+
github.com/oasdiff/yaml v0.0.0-20241214135536-5f7845c759c8
9798
github.com/testcontainers/testcontainers-go v0.34.0
9899
github.com/testcontainers/testcontainers-go/modules/kafka v0.33.0
99100
github.com/testcontainers/testcontainers-go/modules/nats v0.33.0
@@ -231,6 +232,7 @@ require (
231232
github.com/nats-io/nkeys v0.4.9 // indirect
232233
github.com/nats-io/nuid v1.0.1 // indirect
233234
github.com/nats-io/stan.go v0.10.4 // indirect
235+
github.com/oasdiff/yaml3 v0.0.0-20241214160948-977117996672 // indirect
234236
github.com/opencontainers/go-digest v1.0.0 // indirect
235237
github.com/opencontainers/image-spec v1.1.0 // indirect
236238
github.com/perimeterx/marshmallow v1.1.5 // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,10 @@ github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1/go.mod h1:mpRZBD8SJ55
614614
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
615615
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
616616
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
617+
github.com/oasdiff/yaml v0.0.0-20241214135536-5f7845c759c8 h1:9djga8U4+/TQzv5iMlZHZ/qbGQB9V2nlnk2bmiG+uBs=
618+
github.com/oasdiff/yaml v0.0.0-20241214135536-5f7845c759c8/go.mod h1:7tFDb+Y51LcDpn26GccuUgQXUk6t0CXZsivKjyimYX8=
619+
github.com/oasdiff/yaml3 v0.0.0-20241214160948-977117996672 h1:+273wgr7to5QhwOOBE5LwjdNDFAI+8cbJVfB0Zj75aI=
620+
github.com/oasdiff/yaml3 v0.0.0-20241214160948-977117996672/go.mod h1:y5+oSEHCPT/DGrS++Wc/479ERge0zTFxaF8PbGKcg2o=
617621
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
618622
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
619623
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=

0 commit comments

Comments
 (0)