Update OpenAPI and fix openAPI tests to handle unexported jsonreferences

Co-authored-by: Alexander Zielensk <alexzielenski@gmail.com>
This commit is contained in:
Joe Betz 2024-10-25 13:51:03 -04:00
parent a0f419fe56
commit 700e3b5664
6 changed files with 40 additions and 4 deletions

2
go.mod
View File

@ -30,6 +30,7 @@ require (
github.com/emicklei/go-restful/v3 v3.11.0
github.com/fsnotify/fsnotify v1.7.0
github.com/go-logr/logr v1.4.2
github.com/go-openapi/jsonreference v0.20.2
github.com/godbus/dbus/v5 v5.1.0
github.com/gogo/protobuf v1.3.2
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
@ -153,7 +154,6 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect

View File

@ -18,10 +18,12 @@ package openapi
import (
"encoding/json"
"reflect"
"testing"
"github.com/go-openapi/jsonreference"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/handler"
"k8s.io/kube-openapi/pkg/validation/spec"
@ -51,8 +53,16 @@ func TestOpenAPIRoundtrip(t *testing.T) {
delete(roundTripped.Extensions, common.ExtensionV2Schema)
delete(value.Schema.Extensions, common.ExtensionV2Schema)
if !reflect.DeepEqual(value.Schema, roundTripped) {
t.Errorf("unexpected diff (a=expected,b=roundtripped):\n%s", cmp.Diff(value.Schema, roundTripped))
opts := []cmp.Option{
cmpopts.EquateEmpty(),
// jsonreference.Ref contains unexported fields. Compare
// by string representation provides a consistent
cmp.Comparer(func(x, y jsonreference.Ref) bool {
return x.String() == y.String()
}),
}
if !cmp.Equal(value.Schema, roundTripped, opts...) {
t.Errorf("unexpected diff (a=expected,b=roundtripped):\n%s", cmp.Diff(value.Schema, roundTripped, opts...))
return
}
})

View File

@ -56,3 +56,7 @@ func (g *groupversion) Schema(contentType string) ([]byte, error) {
return cachedInfo.data, cachedInfo.err
}
func (c *groupversion) ServerRelativeURL() string {
return c.delegate.ServerRelativeURL()
}

View File

@ -27,6 +27,12 @@ const ContentTypeOpenAPIV3PB = "application/com.github.proto-openapi.spec.v3@v1.
type GroupVersion interface {
Schema(contentType string) ([]byte, error)
// ServerRelativeURL. Returns the path and parameters used to fetch the schema.
// You should use the Schema method to fetch it, but this value can be used
// to key the current version of the schema in a cache since it contains a
// hash string which changes upon schema update.
ServerRelativeURL() string
}
type groupversion struct {
@ -68,3 +74,9 @@ func (g *groupversion) Schema(contentType string) ([]byte, error) {
return path.Do(context.TODO()).Raw()
}
// URL used for fetching the schema. The URL includes a hash and can be used
// to key the current version of the schema in a cache.
func (g *groupversion) ServerRelativeURL() string {
return g.item.ServerRelativeURL
}

View File

@ -77,3 +77,8 @@ func (f FakeGroupVersion) Schema(contentType string) ([]byte, error) {
}
return f.GVSpec, nil
}
// ServerRelativeURL returns an empty string.
func (f FakeGroupVersion) ServerRelativeURL() string {
panic("unimplemented")
}

View File

@ -94,3 +94,8 @@ func (f *fileGroupVersion) Schema(contentType string) ([]byte, error) {
}
return fs.ReadFile(f.f, f.filename)
}
// ServerRelativeURL returns an empty string.
func (f *fileGroupVersion) ServerRelativeURL() string {
return f.filename
}