From d14c3ed6f3164a241cebfb4b26960454c1c06613 Mon Sep 17 00:00:00 2001 From: Prashansa Kulshrestha Date: Mon, 19 Aug 2024 12:25:42 +0530 Subject: [PATCH] chore: corrections for line spaces, error handling, stray comments --- openapi2kong/jsonschema.go | 18 +++++------------- openapi2kong/openapi2kong.go | 16 +++++----------- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/openapi2kong/jsonschema.go b/openapi2kong/jsonschema.go index d0f28c6..809acc8 100644 --- a/openapi2kong/jsonschema.go +++ b/openapi2kong/jsonschema.go @@ -24,22 +24,15 @@ func dereferenceSchema(sr *base.SchemaProxy, seenBefore map[string]*base.SchemaP } s := sr.Schema() - - for _, schema := range s.AllOf { - dereferenceSchema(schema, seenBefore) - } - - for _, schema := range s.AnyOf { - dereferenceSchema(schema, seenBefore) - } - - for _, schema := range s.OneOf { - dereferenceSchema(schema, seenBefore) + allSchemas := [][]*base.SchemaProxy{s.AllOf, s.AnyOf, s.OneOf} + for _, schemas := range allSchemas { + for _, schema := range schemas { + dereferenceSchema(schema, seenBefore) + } } schemaMap := s.Properties schema := schemaMap.First() - for schema != nil { dereferenceSchema(schema.Value(), seenBefore) schema = schema.Next() @@ -70,7 +63,6 @@ func extractSchema(s *base.SchemaProxy) string { finalSchema := make(map[string]interface{}) if s.IsReference() { - // Add ref key and string finalSchema["$ref"] = s.GetReference() } else { // copy the primary schema, if no ref string is present diff --git a/openapi2kong/openapi2kong.go b/openapi2kong/openapi2kong.go index 7275426..98f07ff 100644 --- a/openapi2kong/openapi2kong.go +++ b/openapi2kong/openapi2kong.go @@ -71,14 +71,12 @@ func getKongTags(doc v3.Document, tagsProvided []string) ([]string, error) { } kongTags, ok := doc.Extensions.Get("x-kong-tags") - if !ok { // there is no extension by the name "x-kong-tag", so return an empty array return make([]string, 0), nil } resultArray := make([]string, len(kongTags.Content)) - for i, v := range kongTags.Content { var tagsValue interface{} err := yaml.Unmarshal([]byte(v.Value), &tagsValue) @@ -176,13 +174,9 @@ func getXKongComponents(doc v3.Document) (*map[string]interface{}, error) { var xKong interface{} _ = yaml.Unmarshal(xKongComponentsBytes, &xKong) - - switch val := xKong.(type) { - case map[string]interface{}: - components = val - - default: - return nil, fmt.Errorf("expected '/components/x-kong' to be a YAML object") + components, err = jsonbasics.ToObject(xKong) + if err != nil { + return nil, fmt.Errorf("expected '/components/x-kong' to be a JSON/YAML object") } return &components, nil @@ -398,7 +392,7 @@ func getPluginsList( var pluginConfig map[string]interface{} err = json.Unmarshal(jsonstr, &pluginConfig) if err != nil { - return nil, fmt.Errorf(fmt.Sprintf("failed to parse JSON object for '%s': %%w", extensionName), err) + return nil, fmt.Errorf("failed to parse JSON object for '%s': %w", extensionName, err) } pluginConfig["name"] = pluginName @@ -692,7 +686,7 @@ func Convert(content []byte, opts O2kOptions) (map[string]interface{}, error) { return nil, fmt.Errorf("failed to create plugins list from document root: %w", err) } - // // get the OIDC stuff from top level, bail out if the requirements are unsupported + // get the OIDC stuff from top level, bail out if the requirements are unsupported if opts.OIDC { docOIDCdefaults, err = getOIDCdefaults(doc.Security, doc, nil, opts.IgnoreSecurityErrors) if err != nil {