Skip to content

Commit a5c8c15

Browse files
authored
fix(o2k): plugin sort if inso-compat is enabled (#187)
1 parent 95d22e8 commit a5c8c15

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

cmd/openapi2kong.go

+10
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,20 @@ func executeOpenapi2Kong(cmd *cobra.Command, _ []string) error {
6969
}
7070
}
7171

72+
var insoCompatibility bool
73+
{
74+
insoCompatibility, err = cmd.Flags().GetBool("inso-compatible")
75+
if err != nil {
76+
return fmt.Errorf("failed getting cli argument 'inso-compatible'; %w", err)
77+
}
78+
}
79+
7280
options := openapi2kong.O2kOptions{
7381
Tags: entityTags,
7482
DocName: docName,
7583
OIDC: generateSecurity,
7684
IgnoreSecurityErrors: ignoreSecurityErrors,
85+
InsoCompat: insoCompatibility,
7786
}
7887

7988
trackInfo := deckformat.HistoryNewEntry("openapi2kong")
@@ -127,4 +136,5 @@ directive from the file)`)
127136
openapi2kongCmd.Flags().BoolP("generate-security", "", false, "generate OpenIDConnect plugins from the "+
128137
"security directives")
129138
openapi2kongCmd.Flags().BoolP("ignore-security-errors", "", false, "ignore errors for unsupported security schemes")
139+
openapi2kongCmd.Flags().BoolP("inso-compatible", "", false, "generate the config in an Inso compatible way")
130140
}

openapi2kong/oas3_testfiles/09a-plugins-with-consumers.expected.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
"plugins": [
44
{
55
"config": {
6-
"message": "The answer to life, the universe, and everything!",
6+
"message": "For a moment, nothing happened. Then, after a second or so, nothing continued to happen.",
77
"status_code": 403
88
},
9-
"consumer": "johndoe2",
10-
"id": "aa56031e-7155-599f-a9e9-93e6b271ba58",
9+
"consumer": "johndoe3",
10+
"id": "ead16074-ccb0-52dd-9f56-4193529e8ffa",
1111
"name": "request-termination",
12-
"route": "simple-api-overview_uses-path-plugin",
12+
"route": "simple-api-overview_uses-ops-plugin",
1313
"tags": [
1414
"OAS3_import",
1515
"OAS3file_09a-plugins-with-consumers.yaml"
1616
]
1717
},
1818
{
1919
"config": {
20-
"message": "For a moment, nothing happened. Then, after a second or so, nothing continued to happen.",
20+
"message": "The answer to life, the universe, and everything!",
2121
"status_code": 403
2222
},
23-
"consumer": "johndoe3",
24-
"id": "ead16074-ccb0-52dd-9f56-4193529e8ffa",
23+
"consumer": "johndoe2",
24+
"id": "aa56031e-7155-599f-a9e9-93e6b271ba58",
2525
"name": "request-termination",
26-
"route": "simple-api-overview_uses-ops-plugin",
26+
"route": "simple-api-overview_uses-path-plugin",
2727
"tags": [
2828
"OAS3_import",
2929
"OAS3file_09a-plugins-with-consumers.yaml"
@@ -113,4 +113,4 @@
113113
}
114114
],
115115
"upstreams": []
116-
}
116+
}

openapi2kong/openapi2kong.go

+38-9
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,14 @@ func insertPlugin(list *[]*map[string]interface{}, newPlugin *map[string]interfa
530530
// getForeignKeyPlugins checks the pluginList for plugins that also have a foreign key
531531
// for a consumer, and moves them to the docPlugins array. Returns update docPlugins and pluginList.
532532
func getForeignKeyPlugins(
533-
docPlugins *[]*map[string]interface{},
534-
pluginList *[]*map[string]interface{},
535-
foreignKey string, foreignValue string,
536-
) (*[]*map[string]interface{}, *[]*map[string]interface{}) {
533+
docPlugins *[]*map[string]interface{}, // the current list of doc-level plugins (may be nil)
534+
pluginList *[]*map[string]interface{}, // the list of entity-level plugins to check for foreign keys (may be nil)
535+
foreignKey string, // the owner entity type: eg. "service", or "route"
536+
foreignValue string, // the owner entity name/id: the value (service/route name)
537+
) (
538+
*[]*map[string]interface{}, // updated slice of document level plugins
539+
*[]*map[string]interface{}, // updated slice of entity level plugins
540+
) {
537541
var genericPlugins []*map[string]interface{}
538542
if docPlugins == nil {
539543
genericPlugins = make([]*map[string]interface{}, 0)
@@ -1119,13 +1123,38 @@ func Convert(content []byte, opts O2kOptions) (map[string]interface{}, error) {
11191123
result["services"] = services
11201124
result["upstreams"] = upstreams
11211125
if len(*foreignKeyPlugins) > 0 {
1126+
1127+
// getSortKey returns a string that can be used to sort the plugins by name, service, route, and consumer (all
1128+
// the foreign keys that are possible).
1129+
getSortKey := func(p *map[string]interface{}) string {
1130+
plugin := *p
1131+
sep := string([]byte{0})
1132+
key := plugin["name"].(string) + sep
1133+
1134+
if plugin["service"] != nil {
1135+
key = key + plugin["service"].(string) + sep
1136+
} else {
1137+
key = key + sep
1138+
}
1139+
1140+
if plugin["route"] != nil {
1141+
key = key + plugin["route"].(string) + sep
1142+
} else {
1143+
key = key + sep
1144+
}
1145+
1146+
if plugin["consumer"] != nil {
1147+
key = key + plugin["consumer"].(string) + sep
1148+
} else {
1149+
key = key + sep
1150+
}
1151+
1152+
return key
1153+
}
1154+
11221155
sort.Slice(*foreignKeyPlugins,
11231156
func(i, j int) bool {
1124-
p1 := *(*foreignKeyPlugins)[i]
1125-
p2 := *(*foreignKeyPlugins)[j]
1126-
k1 := p1["name"].(string) + p1["id"].(string)
1127-
k2 := p2["name"].(string) + p2["id"].(string)
1128-
return k1 < k2
1157+
return getSortKey((*foreignKeyPlugins)[i]) < getSortKey((*foreignKeyPlugins)[j])
11291158
})
11301159
result["plugins"] = foreignKeyPlugins
11311160
}

0 commit comments

Comments
 (0)