Skip to content

Commit f488ed4

Browse files
committed
openapi: add spec roundtrip test
1 parent 8da9b50 commit f488ed4

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

pkg/generated/openapi/BUILD

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package(default_visibility = ["//visibility:public"])
22

33
load("//build:code_generation.bzl", "gen_openapi", "openapi_deps")
4-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
4+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
55

66
gen_openapi(
77
outs = ["zz_generated.openapi.go"],
@@ -30,3 +30,13 @@ filegroup(
3030
srcs = [":package-srcs"],
3131
tags = ["automanaged"],
3232
)
33+
34+
go_test(
35+
name = "go_default_test",
36+
srcs = ["openapi_test.go"],
37+
embed = [":go_default_library"],
38+
deps = [
39+
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
40+
"//vendor/github.com/go-openapi/spec:go_default_library",
41+
],
42+
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package openapi
18+
19+
import (
20+
"encoding/json"
21+
"reflect"
22+
"testing"
23+
24+
"github.com/go-openapi/spec"
25+
26+
"k8s.io/apimachinery/pkg/util/diff"
27+
)
28+
29+
func TestOpenAPIRoundtrip(t *testing.T) {
30+
dummyRef := func(name string) spec.Ref { return spec.MustCreateRef("#/definitions/dummy") }
31+
for name, value := range GetOpenAPIDefinitions(dummyRef) {
32+
t.Run(name, func(t *testing.T) {
33+
data, err := json.Marshal(value.Schema)
34+
if err != nil {
35+
t.Error(err)
36+
return
37+
}
38+
39+
roundTripped := spec.Schema{}
40+
if err := json.Unmarshal(data, &roundTripped); err != nil {
41+
t.Error(err)
42+
return
43+
}
44+
45+
if !reflect.DeepEqual(value.Schema, roundTripped) {
46+
t.Errorf("unexpected diff (a=expected,b=roundtripped):\n%s", diff.ObjectReflectDiff(value.Schema, roundTripped))
47+
return
48+
}
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)