openapi_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package openapi
  14. import (
  15. "encoding/json"
  16. "reflect"
  17. "testing"
  18. "github.com/go-openapi/spec"
  19. "k8s.io/apimachinery/pkg/util/diff"
  20. )
  21. func TestOpenAPIRoundtrip(t *testing.T) {
  22. dummyRef := func(name string) spec.Ref { return spec.MustCreateRef("#/definitions/dummy") }
  23. for name, value := range GetOpenAPIDefinitions(dummyRef) {
  24. t.Run(name, func(t *testing.T) {
  25. data, err := json.Marshal(value.Schema)
  26. if err != nil {
  27. t.Error(err)
  28. return
  29. }
  30. roundTripped := spec.Schema{}
  31. if err := json.Unmarshal(data, &roundTripped); err != nil {
  32. t.Error(err)
  33. return
  34. }
  35. if !reflect.DeepEqual(value.Schema, roundTripped) {
  36. t.Errorf("unexpected diff (a=expected,b=roundtripped):\n%s", diff.ObjectReflectDiff(value.Schema, roundTripped))
  37. return
  38. }
  39. })
  40. }
  41. }