tolerate more than one gvklist item

Some third-party resources could be part of more than one api group.
Allow this to be the case when adding openapi models to openapi data.
pull/6/head
juanvallejo 2018-01-18 12:06:02 -05:00
parent abdff8a0e6
commit 13add66f1e
No known key found for this signature in database
GPG Key ID: 7D2C958002D6448D
1 changed files with 38 additions and 35 deletions

View File

@ -56,11 +56,13 @@ func NewOpenAPIData(doc *openapi_v2.Document) (Resources, error) {
if model == nil { if model == nil {
panic("ListModels returns a model that can't be looked-up.") panic("ListModels returns a model that can't be looked-up.")
} }
gvk := parseGroupVersionKind(model) gvkList := parseGroupVersionKind(model)
for _, gvk := range gvkList {
if len(gvk.Kind) > 0 { if len(gvk.Kind) > 0 {
resources[gvk] = modelName resources[gvk] = modelName
} }
} }
}
return &document{ return &document{
resources: resources, resources: resources,
@ -77,48 +79,49 @@ func (d *document) LookupResource(gvk schema.GroupVersionKind) proto.Schema {
} }
// Get and parse GroupVersionKind from the extension. Returns empty if it doesn't have one. // Get and parse GroupVersionKind from the extension. Returns empty if it doesn't have one.
func parseGroupVersionKind(s proto.Schema) schema.GroupVersionKind { func parseGroupVersionKind(s proto.Schema) []schema.GroupVersionKind {
extensions := s.GetExtensions() extensions := s.GetExtensions()
gvkListResult := []schema.GroupVersionKind{}
// Get the extensions // Get the extensions
gvkExtension, ok := extensions[groupVersionKindExtensionKey] gvkExtension, ok := extensions[groupVersionKindExtensionKey]
if !ok { if !ok {
return schema.GroupVersionKind{} return []schema.GroupVersionKind{}
} }
// gvk extension must be a list of 1 element. // gvk extension must be a list of at least 1 element.
gvkList, ok := gvkExtension.([]interface{}) gvkList, ok := gvkExtension.([]interface{})
if !ok { if !ok {
return schema.GroupVersionKind{} return []schema.GroupVersionKind{}
} }
if len(gvkList) != 1 {
return schema.GroupVersionKind{}
}
gvk := gvkList[0]
for _, gvk := range gvkList {
// gvk extension list must be a map with group, version, and // gvk extension list must be a map with group, version, and
// kind fields // kind fields
gvkMap, ok := gvk.(map[interface{}]interface{}) gvkMap, ok := gvk.(map[interface{}]interface{})
if !ok { if !ok {
return schema.GroupVersionKind{} continue
} }
group, ok := gvkMap["group"].(string) group, ok := gvkMap["group"].(string)
if !ok { if !ok {
return schema.GroupVersionKind{} continue
} }
version, ok := gvkMap["version"].(string) version, ok := gvkMap["version"].(string)
if !ok { if !ok {
return schema.GroupVersionKind{} continue
} }
kind, ok := gvkMap["kind"].(string) kind, ok := gvkMap["kind"].(string)
if !ok { if !ok {
return schema.GroupVersionKind{} continue
} }
return schema.GroupVersionKind{ gvkListResult = append(gvkListResult, schema.GroupVersionKind{
Group: group, Group: group,
Version: version, Version: version,
Kind: kind, Kind: kind,
})
} }
return gvkListResult
} }