Skip to content

Commit

Permalink
chore: replaced kin-openapi library with libopenapi (#194)
Browse files Browse the repository at this point in the history
* chore: replaced kin-openapi library with libopenapi

deck file openapi2kong command uses kin-openapi
for all its OpenAPI requirements. This library
does not support OpenAPI 3.1, which is a
requirement from the users.
Thus, this change updates the library to libopenapi
which can help us to adopt OpenAPI 3.1

For APIOps #1324
Kong/deck#1324

* chore: lint check corrections

* chore: corrections for line spaces, error handling, stray comments
  • Loading branch information
Prashansa-K authored Aug 22, 2024
1 parent e464df7 commit 66febca
Show file tree
Hide file tree
Showing 11 changed files with 516 additions and 385 deletions.
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/kong/go-apiops
go 1.21

require (
github.com/getkin/kin-openapi v0.108.0
github.com/go-logr/logr v1.4.2
github.com/go-logr/stdr v1.2.2
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/kong/go-slugify v1.0.0
github.com/onsi/ginkgo/v2 v2.20.0
github.com/onsi/gomega v1.34.1
github.com/pb33f/libopenapi v0.16.13
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
github.com/vmware-labs/yaml-jsonpath v0.3.2
Expand All @@ -21,25 +21,21 @@ require (
)

require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dprotaso/go-yit v0.0.0-20191028211022-135eb7262960 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.19.5 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/mozillazg/go-unidecode v0.2.0 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/tools v0.24.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
58 changes: 34 additions & 24 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions namespace/namespace_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func ApplyNamespaceHost(
deckfile *yaml.Node, // the deckFile to operate on
selectors yamlbasics.SelectorSet, // the selectors to use to select the routes
hosts []string, // the hosts to add to the routes
//nolint:predeclared
clear bool, // if true, clear the hosts field before adding the hosts
allowEmptySelection bool, // if true, do not return an error if no routes are selected
) error {
Expand Down Expand Up @@ -54,6 +55,8 @@ func ApplyNamespaceHost(

// updateRouteHosts updates the hosts field of the provided routes.
// If clear is true, the hosts field is cleared before adding the hosts.
//
//nolint:predeclared
func updateRouteHosts(routes yamlbasics.NodeSet, hosts []string, clear bool) error {
for _, route := range routes {
if err := yamlbasics.CheckType(route, yamlbasics.TypeObject); err != nil {
Expand Down
67 changes: 45 additions & 22 deletions openapi2kong/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,84 @@ import (
"encoding/json"
"strings"

"github.com/getkin/kin-openapi/openapi3"
"github.com/pb33f/libopenapi/datamodel/high/base"
)

// dereferenceSchema walks the schema and adds every subschema to the seenBefore map.
// This is safe to recursive schemas.
func dereferenceSchema(sr *openapi3.SchemaRef, seenBefore map[string]*openapi3.Schema) {
func dereferenceSchema(sr *base.SchemaProxy, seenBefore map[string]*base.SchemaProxy) {
if sr == nil {
return
}

if sr.Ref != "" {
if seenBefore[sr.Ref] != nil {
srRef := sr.GetReference()

if srRef != "" {
if seenBefore[srRef] != nil {
return
}
seenBefore[sr.Ref] = sr.Value
seenBefore[srRef] = sr
}

s := sr.Value

for _, list := range []openapi3.SchemaRefs{s.AllOf, s.AnyOf, s.OneOf} {
for _, s2 := range list {
dereferenceSchema(s2, seenBefore)
s := sr.Schema()
allSchemas := [][]*base.SchemaProxy{s.AllOf, s.AnyOf, s.OneOf}
for _, schemas := range allSchemas {
for _, schema := range schemas {
dereferenceSchema(schema, seenBefore)
}
}
for _, s2 := range s.Properties {
dereferenceSchema(s2, seenBefore)

schemaMap := s.Properties
schema := schemaMap.First()
for schema != nil {
dereferenceSchema(schema.Value(), seenBefore)
schema = schema.Next()
}
for _, ref := range []*openapi3.SchemaRef{s.Not, s.AdditionalProperties, s.Items} {
dereferenceSchema(ref, seenBefore)

dereferenceSchema(s.Not, seenBefore)

if s.AdditionalProperties != nil && s.AdditionalProperties.IsA() {
dereferenceSchema(s.AdditionalProperties.A, seenBefore)
}

if s.Items != nil && s.Items.IsA() {
dereferenceSchema(s.Items.A, seenBefore)
}
}

// extractSchema will extract a schema, including all sub-schemas/references and
// return it as a single JSONschema string. All components will be moved under the
// "#/definitions/" key.
func extractSchema(s *openapi3.SchemaRef) string {
if s == nil || s.Value == nil {
func extractSchema(s *base.SchemaProxy) string {
if s == nil || s.Schema() == nil {
return ""
}

seenBefore := make(map[string]*openapi3.Schema)
seenBefore := make(map[string]*base.SchemaProxy)
dereferenceSchema(s, seenBefore)

var finalSchema map[string]interface{}
// copy the primary schema
jConf, _ := s.MarshalJSON()
_ = json.Unmarshal(jConf, &finalSchema)
finalSchema := make(map[string]interface{})

if s.IsReference() {
finalSchema["$ref"] = s.GetReference()
} else {
// copy the primary schema, if no ref string is present
jConf, _ := s.Schema().MarshalJSON()
_ = json.Unmarshal(jConf, &finalSchema)
}

// inject subschema's referenced
if len(seenBefore) > 0 {
definitions := make(map[string]interface{})
for key, schema := range seenBefore {
// copy the subschema
var copySchema map[string]interface{}
jConf, _ := schema.MarshalJSON()

if schema.Schema() == nil {
continue
}

jConf, _ := schema.Schema().MarshalJSON()
_ = json.Unmarshal(jConf, &copySchema)

// store under new key
Expand Down
Loading

0 comments on commit 66febca

Please sign in to comment.