Remove unused servePath from GetOperationIDAndTags and GetDefinitionName
Kubernetes-commit: ef8ee84cd07dedf0a441d455f54b55a6468b4b3d
This commit is contained in:
parent
5b1bafe4d2
commit
bfb371141a
|
@ -59,7 +59,7 @@ func ToValidOperationID(s string, capitalizeFirstLetter bool) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOperationIDAndTags returns a customize operation ID and a list of tags for kubernetes API server's OpenAPI spec to prevent duplicate IDs.
|
// GetOperationIDAndTags returns a customize operation ID and a list of tags for kubernetes API server's OpenAPI spec to prevent duplicate IDs.
|
||||||
func GetOperationIDAndTags(servePath string, r *restful.Route) (string, []string, error) {
|
func GetOperationIDAndTags(r *restful.Route) (string, []string, error) {
|
||||||
op := r.Operation
|
op := r.Operation
|
||||||
path := r.Path
|
path := r.Path
|
||||||
var tags []string
|
var tags []string
|
||||||
|
@ -67,33 +67,29 @@ func GetOperationIDAndTags(servePath string, r *restful.Route) (string, []string
|
||||||
if strings.HasPrefix(path, "/apis/extensions/v1beta1/namespaces/{namespace}/") && strings.HasSuffix(op, "ScaleScale") {
|
if strings.HasPrefix(path, "/apis/extensions/v1beta1/namespaces/{namespace}/") && strings.HasSuffix(op, "ScaleScale") {
|
||||||
op = op[:len(op)-10] + strings.Title(strings.Split(path[48:], "/")[0]) + "Scale"
|
op = op[:len(op)-10] + strings.Title(strings.Split(path[48:], "/")[0]) + "Scale"
|
||||||
}
|
}
|
||||||
switch servePath {
|
prefix, exists := verbs.GetPrefix(op)
|
||||||
case "/swagger.json":
|
if !exists {
|
||||||
prefix, exists := verbs.GetPrefix(op)
|
return op, tags, fmt.Errorf("operation names should start with a verb. Cannot determine operation verb from %v", op)
|
||||||
if !exists {
|
|
||||||
return op, tags, fmt.Errorf("operation names should start with a verb. Cannot determine operation verb from %v", op)
|
|
||||||
}
|
|
||||||
op = op[len(prefix):]
|
|
||||||
parts := strings.Split(strings.Trim(path, "/"), "/")
|
|
||||||
// Assume /api is /apis/core, remove this when we actually server /api/... on /apis/core/...
|
|
||||||
if len(parts) >= 1 && parts[0] == "api" {
|
|
||||||
parts = append([]string{"apis", "core"}, parts[1:]...)
|
|
||||||
}
|
|
||||||
if len(parts) >= 2 && parts[0] == "apis" {
|
|
||||||
prefix = prefix + ToValidOperationID(strings.TrimSuffix(parts[1], ".k8s.io"), prefix != "")
|
|
||||||
tag := ToValidOperationID(strings.TrimSuffix(parts[1], ".k8s.io"), false)
|
|
||||||
if len(parts) > 2 {
|
|
||||||
prefix = prefix + ToValidOperationID(parts[2], prefix != "")
|
|
||||||
tag = tag + "_" + ToValidOperationID(parts[2], false)
|
|
||||||
}
|
|
||||||
tags = append(tags, tag)
|
|
||||||
} else if len(parts) >= 1 {
|
|
||||||
tags = append(tags, ToValidOperationID(parts[0], false))
|
|
||||||
}
|
|
||||||
return prefix + ToValidOperationID(op, prefix != ""), tags, nil
|
|
||||||
default:
|
|
||||||
return op, tags, nil
|
|
||||||
}
|
}
|
||||||
|
op = op[len(prefix):]
|
||||||
|
parts := strings.Split(strings.Trim(path, "/"), "/")
|
||||||
|
// Assume /api is /apis/core, remove this when we actually server /api/... on /apis/core/...
|
||||||
|
if len(parts) >= 1 && parts[0] == "api" {
|
||||||
|
parts = append([]string{"apis", "core"}, parts[1:]...)
|
||||||
|
}
|
||||||
|
if len(parts) >= 2 && parts[0] == "apis" {
|
||||||
|
trimmed := strings.TrimSuffix(parts[1], ".k8s.io")
|
||||||
|
prefix = prefix + ToValidOperationID(trimmed, prefix != "")
|
||||||
|
tag := ToValidOperationID(trimmed, false)
|
||||||
|
if len(parts) > 2 {
|
||||||
|
prefix = prefix + ToValidOperationID(parts[2], prefix != "")
|
||||||
|
tag = tag + "_" + ToValidOperationID(parts[2], false)
|
||||||
|
}
|
||||||
|
tags = append(tags, tag)
|
||||||
|
} else if len(parts) >= 1 {
|
||||||
|
tags = append(tags, ToValidOperationID(parts[0], false))
|
||||||
|
}
|
||||||
|
return prefix + ToValidOperationID(op, prefix != ""), tags, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type groupVersionKinds []v1.GroupVersionKind
|
type groupVersionKinds []v1.GroupVersionKind
|
||||||
|
@ -161,7 +157,7 @@ func NewDefinitionNamer(s *runtime.Scheme) DefinitionNamer {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDefinitionName returns the name and tags for a given definition
|
// GetDefinitionName returns the name and tags for a given definition
|
||||||
func (d *DefinitionNamer) GetDefinitionName(servePath string, name string) (string, spec.Extensions) {
|
func (d *DefinitionNamer) GetDefinitionName(name string) (string, spec.Extensions) {
|
||||||
if groupVersionKinds, ok := d.typeGroupVersionKinds[name]; ok {
|
if groupVersionKinds, ok := d.typeGroupVersionKinds[name]; ok {
|
||||||
return friendlyName(name), spec.Extensions{
|
return friendlyName(name), spec.Extensions{
|
||||||
extensionGVK: []v1.GroupVersionKind(groupVersionKinds),
|
extensionGVK: []v1.GroupVersionKind(groupVersionKinds),
|
||||||
|
|
|
@ -71,7 +71,7 @@ func TestGetDefinitionName(t *testing.T) {
|
||||||
s := runtime.NewScheme()
|
s := runtime.NewScheme()
|
||||||
s.AddKnownTypeWithName(testType.GroupVersionKind(), &testType)
|
s.AddKnownTypeWithName(testType.GroupVersionKind(), &testType)
|
||||||
namer := NewDefinitionNamer(s)
|
namer := NewDefinitionNamer(s)
|
||||||
n, e := namer.GetDefinitionName("", typePkgName)
|
n, e := namer.GetDefinitionName(typePkgName)
|
||||||
assertEqual(t, typeFriendlyName, n)
|
assertEqual(t, typeFriendlyName, n)
|
||||||
assertEqual(t, e["x-kubernetes-group-version-kind"], []v1.GroupVersionKind{
|
assertEqual(t, e["x-kubernetes-group-version-kind"], []v1.GroupVersionKind{
|
||||||
{
|
{
|
||||||
|
@ -80,7 +80,7 @@ func TestGetDefinitionName(t *testing.T) {
|
||||||
Kind: "TestType",
|
Kind: "TestType",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
n, e2 := namer.GetDefinitionName("", "test.com/another.Type")
|
n, e2 := namer.GetDefinitionName("test.com/another.Type")
|
||||||
assertEqual(t, "com.test.another.Type", n)
|
assertEqual(t, "com.test.another.Type", n)
|
||||||
assertEqual(t, e2, spec.Extensions(nil))
|
assertEqual(t, e2, spec.Extensions(nil))
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,6 @@ type openAPI struct {
|
||||||
swaggerPbGz []byte
|
swaggerPbGz []byte
|
||||||
lastModified time.Time
|
lastModified time.Time
|
||||||
protocolList []string
|
protocolList []string
|
||||||
servePath string
|
|
||||||
definitions map[string]openapi.OpenAPIDefinition
|
definitions map[string]openapi.OpenAPIDefinition
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +78,6 @@ func RegisterOpenAPIService(servePath string, webServices []*restful.WebService,
|
||||||
|
|
||||||
o := openAPI{
|
o := openAPI{
|
||||||
config: config,
|
config: config,
|
||||||
servePath: servePath,
|
|
||||||
swagger: &spec.Swagger{
|
swagger: &spec.Swagger{
|
||||||
SwaggerProps: spec.SwaggerProps{
|
SwaggerProps: spec.SwaggerProps{
|
||||||
Swagger: OpenAPIVersion,
|
Swagger: OpenAPIVersion,
|
||||||
|
@ -132,17 +130,17 @@ func RegisterOpenAPIService(servePath string, webServices []*restful.WebService,
|
||||||
|
|
||||||
func (o *openAPI) init(webServices []*restful.WebService) error {
|
func (o *openAPI) init(webServices []*restful.WebService) error {
|
||||||
if o.config.GetOperationIDAndTags == nil {
|
if o.config.GetOperationIDAndTags == nil {
|
||||||
o.config.GetOperationIDAndTags = func(_ string, r *restful.Route) (string, []string, error) {
|
o.config.GetOperationIDAndTags = func(r *restful.Route) (string, []string, error) {
|
||||||
return r.Operation, nil, nil
|
return r.Operation, nil, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if o.config.GetDefinitionName == nil {
|
if o.config.GetDefinitionName == nil {
|
||||||
o.config.GetDefinitionName = func(_, name string) (string, spec.Extensions) {
|
o.config.GetDefinitionName = func(name string) (string, spec.Extensions) {
|
||||||
return name[strings.LastIndex(name, "/")+1:], nil
|
return name[strings.LastIndex(name, "/")+1:], nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
o.definitions = o.config.GetDefinitions(func(name string) spec.Ref {
|
o.definitions = o.config.GetDefinitions(func(name string) spec.Ref {
|
||||||
defName, _ := o.config.GetDefinitionName(o.servePath, name)
|
defName, _ := o.config.GetDefinitionName(name)
|
||||||
return spec.MustCreateRef("#/definitions/" + openapi.EscapeJsonPointer(defName))
|
return spec.MustCreateRef("#/definitions/" + openapi.EscapeJsonPointer(defName))
|
||||||
})
|
})
|
||||||
if o.config.CommonResponses == nil {
|
if o.config.CommonResponses == nil {
|
||||||
|
@ -210,7 +208,7 @@ func getCanonicalizeTypeName(t reflect.Type) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *openAPI) buildDefinitionRecursively(name string) error {
|
func (o *openAPI) buildDefinitionRecursively(name string) error {
|
||||||
uniqueName, extensions := o.config.GetDefinitionName(o.servePath, name)
|
uniqueName, extensions := o.config.GetDefinitionName(name)
|
||||||
if _, ok := o.swagger.Definitions[uniqueName]; ok {
|
if _, ok := o.swagger.Definitions[uniqueName]; ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -252,7 +250,7 @@ func (o *openAPI) buildDefinitionForType(sample interface{}) (string, error) {
|
||||||
if err := o.buildDefinitionRecursively(name); err != nil {
|
if err := o.buildDefinitionRecursively(name); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defName, _ := o.config.GetDefinitionName(o.servePath, name)
|
defName, _ := o.config.GetDefinitionName(name)
|
||||||
return "#/definitions/" + openapi.EscapeJsonPointer(defName), nil
|
return "#/definitions/" + openapi.EscapeJsonPointer(defName), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,7 +354,7 @@ func (o *openAPI) buildOperations(route restful.Route, inPathCommonParamsMap map
|
||||||
ret.Extensions.Add(k, v)
|
ret.Extensions.Add(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ret.ID, ret.Tags, err = o.config.GetOperationIDAndTags(o.servePath, &route); err != nil {
|
if ret.ID, ret.Tags, err = o.config.GetOperationIDAndTags(&route); err != nil {
|
||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -200,7 +200,7 @@ func getConfig(fullMethods bool) (*openapi.Config, *restful.Container) {
|
||||||
"k8s.io/apiserver/pkg/server/openapi/go_default_test.TestOutput": *TestOutput{}.OpenAPIDefinition(),
|
"k8s.io/apiserver/pkg/server/openapi/go_default_test.TestOutput": *TestOutput{}.OpenAPIDefinition(),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
GetDefinitionName: func(_ string, name string) (string, spec.Extensions) {
|
GetDefinitionName: func(name string) (string, spec.Extensions) {
|
||||||
friendlyName := name[strings.LastIndex(name, "/")+1:]
|
friendlyName := name[strings.LastIndex(name, "/")+1:]
|
||||||
if strings.HasPrefix(friendlyName, "go_default_test") {
|
if strings.HasPrefix(friendlyName, "go_default_test") {
|
||||||
friendlyName = "openapi" + friendlyName[len("go_default_test"):]
|
friendlyName = "openapi" + friendlyName[len("go_default_test"):]
|
||||||
|
|
Loading…
Reference in New Issue