Skip to content

Commit

Permalink
fix(o2k): plugin sort if inso-compat is enabled (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jul 13, 2024
1 parent 95d22e8 commit a5c8c15
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
10 changes: 10 additions & 0 deletions cmd/openapi2kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,20 @@ func executeOpenapi2Kong(cmd *cobra.Command, _ []string) error {
}
}

var insoCompatibility bool
{
insoCompatibility, err = cmd.Flags().GetBool("inso-compatible")
if err != nil {
return fmt.Errorf("failed getting cli argument 'inso-compatible'; %w", err)
}
}

options := openapi2kong.O2kOptions{
Tags: entityTags,
DocName: docName,
OIDC: generateSecurity,
IgnoreSecurityErrors: ignoreSecurityErrors,
InsoCompat: insoCompatibility,
}

trackInfo := deckformat.HistoryNewEntry("openapi2kong")
Expand Down Expand Up @@ -127,4 +136,5 @@ directive from the file)`)
openapi2kongCmd.Flags().BoolP("generate-security", "", false, "generate OpenIDConnect plugins from the "+
"security directives")
openapi2kongCmd.Flags().BoolP("ignore-security-errors", "", false, "ignore errors for unsupported security schemes")
openapi2kongCmd.Flags().BoolP("inso-compatible", "", false, "generate the config in an Inso compatible way")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
"plugins": [
{
"config": {
"message": "The answer to life, the universe, and everything!",
"message": "For a moment, nothing happened. Then, after a second or so, nothing continued to happen.",
"status_code": 403
},
"consumer": "johndoe2",
"id": "aa56031e-7155-599f-a9e9-93e6b271ba58",
"consumer": "johndoe3",
"id": "ead16074-ccb0-52dd-9f56-4193529e8ffa",
"name": "request-termination",
"route": "simple-api-overview_uses-path-plugin",
"route": "simple-api-overview_uses-ops-plugin",
"tags": [
"OAS3_import",
"OAS3file_09a-plugins-with-consumers.yaml"
]
},
{
"config": {
"message": "For a moment, nothing happened. Then, after a second or so, nothing continued to happen.",
"message": "The answer to life, the universe, and everything!",
"status_code": 403
},
"consumer": "johndoe3",
"id": "ead16074-ccb0-52dd-9f56-4193529e8ffa",
"consumer": "johndoe2",
"id": "aa56031e-7155-599f-a9e9-93e6b271ba58",
"name": "request-termination",
"route": "simple-api-overview_uses-ops-plugin",
"route": "simple-api-overview_uses-path-plugin",
"tags": [
"OAS3_import",
"OAS3file_09a-plugins-with-consumers.yaml"
Expand Down Expand Up @@ -113,4 +113,4 @@
}
],
"upstreams": []
}
}
47 changes: 38 additions & 9 deletions openapi2kong/openapi2kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,14 @@ func insertPlugin(list *[]*map[string]interface{}, newPlugin *map[string]interfa
// getForeignKeyPlugins checks the pluginList for plugins that also have a foreign key
// for a consumer, and moves them to the docPlugins array. Returns update docPlugins and pluginList.
func getForeignKeyPlugins(
docPlugins *[]*map[string]interface{},
pluginList *[]*map[string]interface{},
foreignKey string, foreignValue string,
) (*[]*map[string]interface{}, *[]*map[string]interface{}) {
docPlugins *[]*map[string]interface{}, // the current list of doc-level plugins (may be nil)
pluginList *[]*map[string]interface{}, // the list of entity-level plugins to check for foreign keys (may be nil)
foreignKey string, // the owner entity type: eg. "service", or "route"
foreignValue string, // the owner entity name/id: the value (service/route name)
) (
*[]*map[string]interface{}, // updated slice of document level plugins
*[]*map[string]interface{}, // updated slice of entity level plugins
) {
var genericPlugins []*map[string]interface{}
if docPlugins == nil {
genericPlugins = make([]*map[string]interface{}, 0)
Expand Down Expand Up @@ -1119,13 +1123,38 @@ func Convert(content []byte, opts O2kOptions) (map[string]interface{}, error) {
result["services"] = services
result["upstreams"] = upstreams
if len(*foreignKeyPlugins) > 0 {

// getSortKey returns a string that can be used to sort the plugins by name, service, route, and consumer (all
// the foreign keys that are possible).
getSortKey := func(p *map[string]interface{}) string {
plugin := *p
sep := string([]byte{0})
key := plugin["name"].(string) + sep

if plugin["service"] != nil {
key = key + plugin["service"].(string) + sep
} else {
key = key + sep
}

if plugin["route"] != nil {
key = key + plugin["route"].(string) + sep
} else {
key = key + sep
}

if plugin["consumer"] != nil {
key = key + plugin["consumer"].(string) + sep
} else {
key = key + sep
}

return key
}

sort.Slice(*foreignKeyPlugins,
func(i, j int) bool {
p1 := *(*foreignKeyPlugins)[i]
p2 := *(*foreignKeyPlugins)[j]
k1 := p1["name"].(string) + p1["id"].(string)
k2 := p2["name"].(string) + p2["id"].(string)
return k1 < k2
return getSortKey((*foreignKeyPlugins)[i]) < getSortKey((*foreignKeyPlugins)[j])
})
result["plugins"] = foreignKeyPlugins
}
Expand Down

0 comments on commit a5c8c15

Please sign in to comment.