|
| 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