k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/spec3/path_test.go (about)

     1  /*
     2  Copyright 2021 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 spec3_test
    18  
    19  import (
    20  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"github.com/stretchr/testify/require"
    26  	jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
    27  	"k8s.io/kube-openapi/pkg/spec3"
    28  	jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting"
    29  	"k8s.io/kube-openapi/pkg/validation/spec"
    30  )
    31  
    32  func TestPathRoundTrip(t *testing.T) {
    33  	cases := []jsontesting.RoundTripTestCase{
    34  		{
    35  			Name: "Basic Roundtrip",
    36  			Object: &spec3.Path{
    37  				spec.Refable{Ref: spec.MustCreateRef("Dog")},
    38  				spec3.PathProps{
    39  					Description: "foo",
    40  				},
    41  				spec.VendorExtensible{Extensions: spec.Extensions{
    42  					"x-framework": "go-swagger",
    43  				}},
    44  			},
    45  		},
    46  	}
    47  
    48  	for _, tcase := range cases {
    49  		t.Run(tcase.Name, func(t *testing.T) {
    50  			require.NoError(t, tcase.RoundTripTest(&spec3.Path{}))
    51  		})
    52  	}
    53  }
    54  
    55  func TestPathJSONSerialization(t *testing.T) {
    56  	cases := []struct {
    57  		name           string
    58  		target         *spec3.Path
    59  		expectedOutput string
    60  	}{
    61  		{
    62  			name: "basic",
    63  			target: &spec3.Path{
    64  				PathProps: spec3.PathProps{
    65  					Get: &spec3.Operation{
    66  						OperationProps: spec3.OperationProps{
    67  							Description: "Returns pets based on ID",
    68  							Summary:     "Find pets by ID",
    69  							OperationId: "getPetsById",
    70  							Responses: &spec3.Responses{
    71  								ResponsesProps: spec3.ResponsesProps{
    72  									StatusCodeResponses: map[int]*spec3.Response{
    73  										200: {
    74  											ResponseProps: spec3.ResponseProps{
    75  												Description: "Pet response",
    76  												Content: map[string]*spec3.MediaType{
    77  													"*/*": {
    78  														MediaTypeProps: spec3.MediaTypeProps{
    79  															Schema: &spec.Schema{
    80  																SchemaProps: spec.SchemaProps{
    81  																	Type: []string{"array"},
    82  																	Items: &spec.SchemaOrArray{
    83  																		Schema: &spec.Schema{
    84  																			SchemaProps: spec.SchemaProps{
    85  																				Ref: spec.MustCreateRef("#/components/schemas/Pet"),
    86  																			},
    87  																		},
    88  																	},
    89  																},
    90  															},
    91  														},
    92  													},
    93  												},
    94  											},
    95  										},
    96  									},
    97  								},
    98  							},
    99  						},
   100  					},
   101  					Parameters: []*spec3.Parameter{
   102  						{
   103  							ParameterProps: spec3.ParameterProps{
   104  								Name:        "id",
   105  								In:          "path",
   106  								Description: "ID of the pet to use",
   107  								Required:    true,
   108  								Schema: &spec.Schema{
   109  									SchemaProps: spec.SchemaProps{
   110  										Type: []string{"array"},
   111  										Items: &spec.SchemaOrArray{
   112  											Schema: &spec.Schema{
   113  												SchemaProps: spec.SchemaProps{
   114  													Ref: spec.MustCreateRef("#/components/schemas/Pet"),
   115  												},
   116  											},
   117  										},
   118  									},
   119  								},
   120  							},
   121  						},
   122  					},
   123  				},
   124  			},
   125  			expectedOutput: `{"get":{"summary":"Find pets by ID","description":"Returns pets based on ID","operationId":"getPetsById","responses":{"200":{"description":"Pet response","content":{"*/*":{"schema":{"type":"array","items":{"$ref":"#/components/schemas/Pet"}}}}}}},"parameters":[{"name":"id","in":"path","description":"ID of the pet to use","required":true,"schema":{"type":"array","items":{"$ref":"#/components/schemas/Pet"}}}]}`,
   126  		},
   127  	}
   128  	for _, tc := range cases {
   129  		t.Run(tc.name, func(t *testing.T) {
   130  			rawTarget, err := json.Marshal(tc.target)
   131  			if err != nil {
   132  				t.Fatal(err)
   133  			}
   134  			serializedTarget := string(rawTarget)
   135  			if !cmp.Equal(serializedTarget, tc.expectedOutput) {
   136  				t.Fatalf("%s", serializedTarget)
   137  				t.Fatalf("diff %s", cmp.Diff(serializedTarget, tc.expectedOutput))
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  func TestPathsNullUnmarshal(t *testing.T) {
   144  	nullByte := []byte(`null`)
   145  
   146  	expected := spec3.Paths{}
   147  	test := spec3.Paths{
   148  		Paths: map[string]*spec3.Path{"/path": {}},
   149  	}
   150  	jsonv2.Unmarshal(nullByte, &test)
   151  	if !reflect.DeepEqual(test, expected) {
   152  		t.Error("Expected unmarshal of null to reset the Paths struct")
   153  	}
   154  }