k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/path_item_test.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package spec 16 17 import ( 18 "encoding/json" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting" 24 ) 25 26 var pathItem = PathItem{ 27 Refable: Refable{Ref: MustCreateRef("Dog")}, 28 VendorExtensible: VendorExtensible{ 29 Extensions: map[string]interface{}{ 30 "x-framework": "go-swagger", 31 }, 32 }, 33 PathItemProps: PathItemProps{ 34 Get: &Operation{ 35 OperationProps: OperationProps{Description: "get operation description"}, 36 }, 37 Put: &Operation{ 38 OperationProps: OperationProps{Description: "put operation description"}, 39 }, 40 Post: &Operation{ 41 OperationProps: OperationProps{Description: "post operation description"}, 42 }, 43 Delete: &Operation{ 44 OperationProps: OperationProps{Description: "delete operation description"}, 45 }, 46 Options: &Operation{ 47 OperationProps: OperationProps{Description: "options operation description"}, 48 }, 49 Head: &Operation{ 50 OperationProps: OperationProps{Description: "head operation description"}, 51 }, 52 Patch: &Operation{ 53 OperationProps: OperationProps{Description: "patch operation description"}, 54 }, 55 Parameters: []Parameter{ 56 { 57 ParamProps: ParamProps{In: "path"}, 58 }, 59 }, 60 }, 61 } 62 63 const pathItemJSON = `{ 64 "$ref": "Dog", 65 "x-framework": "go-swagger", 66 "get": { "description": "get operation description" }, 67 "put": { "description": "put operation description" }, 68 "post": { "description": "post operation description" }, 69 "delete": { "description": "delete operation description" }, 70 "options": { "description": "options operation description" }, 71 "head": { "description": "head operation description" }, 72 "patch": { "description": "patch operation description" }, 73 "parameters": [{"in":"path"}] 74 }` 75 76 func TestIntegrationPathItem(t *testing.T) { 77 var actual PathItem 78 if assert.NoError(t, json.Unmarshal([]byte(pathItemJSON), &actual)) { 79 assert.EqualValues(t, actual, pathItem) 80 } 81 82 assertParsesJSON(t, pathItemJSON, pathItem) 83 } 84 85 func TestPathItemRoundTrip(t *testing.T) { 86 cases := []jsontesting.RoundTripTestCase{ 87 { 88 // Show at least one field from each embededd struct sitll allows 89 // roundtrips successfully 90 Name: "UnmarshalEmbedded", 91 JSON: `{ 92 "$ref": "/components/ref/to/something.foo", 93 "x-framework": "swagger-go", 94 "get": { 95 "description": "a cool operation" 96 } 97 }`, 98 Object: &PathItem{ 99 Refable{MustCreateRef("/components/ref/to/something.foo")}, 100 VendorExtensible{Extensions{"x-framework": "swagger-go"}}, 101 PathItemProps{Get: &Operation{OperationProps: OperationProps{Description: "a cool operation"}}}, 102 }, 103 }, { 104 Name: "BasicCase", 105 JSON: pathItemJSON, 106 Object: &pathItem, 107 }, 108 } 109 110 for _, tcase := range cases { 111 t.Run(tcase.Name, func(t *testing.T) { 112 require.NoError(t, tcase.RoundTripTest(&PathItem{})) 113 }) 114 } 115 }