From 1c4cbdf17da632cc37aa3908361fe12fd51a320a Mon Sep 17 00:00:00 2001 From: Jeffy Mathew Date: Tue, 7 Jan 2025 09:42:55 +0100 Subject: [PATCH] Add tests to verify yaml conversions work --- apidef/oas/oas_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 4 ++++ 3 files changed, 60 insertions(+) diff --git a/apidef/oas/oas_test.go b/apidef/oas/oas_test.go index c9beeee57dc..9e0fd17a7ea 100644 --- a/apidef/oas/oas_test.go +++ b/apidef/oas/oas_test.go @@ -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" @@ -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) +} diff --git a/go.mod b/go.mod index e20de39d9b2..7fafc85b413 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 374644330f6..bbd979c7a49 100644 --- a/go.sum +++ b/go.sum @@ -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=