Skip to content

Commit

Permalink
Add tests to verify yaml conversions work
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffy-mathew committed Jan 8, 2025
1 parent cc71abc commit 1c4cbdf
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 1c4cbdf

Please sign in to comment.