k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/parameters_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 parameter = Parameter{ 27 VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{ 28 "x-framework": "swagger-go", 29 }}, 30 Refable: Refable{Ref: MustCreateRef("Dog")}, 31 CommonValidations: CommonValidations{ 32 Maximum: float64Ptr(100), 33 ExclusiveMaximum: true, 34 ExclusiveMinimum: true, 35 Minimum: float64Ptr(5), 36 MaxLength: int64Ptr(100), 37 MinLength: int64Ptr(5), 38 Pattern: "\\w{1,5}\\w+", 39 MaxItems: int64Ptr(100), 40 MinItems: int64Ptr(5), 41 UniqueItems: true, 42 MultipleOf: float64Ptr(5), 43 Enum: []interface{}{"hello", "world"}, 44 }, 45 SimpleSchema: SimpleSchema{ 46 Type: "string", 47 Format: "date", 48 CollectionFormat: "csv", 49 Items: &Items{ 50 Refable: Refable{Ref: MustCreateRef("Cat")}, 51 }, 52 Default: "8", 53 }, 54 ParamProps: ParamProps{ 55 Name: "param-name", 56 In: "header", 57 Required: true, 58 Schema: &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}}, 59 Description: "the description of this parameter", 60 }, 61 } 62 63 var parameterJSON = `{ 64 "items": { 65 "$ref": "Cat" 66 }, 67 "x-framework": "swagger-go", 68 "$ref": "Dog", 69 "description": "the description of this parameter", 70 "maximum": 100, 71 "minimum": 5, 72 "exclusiveMaximum": true, 73 "exclusiveMinimum": true, 74 "maxLength": 100, 75 "minLength": 5, 76 "pattern": "\\w{1,5}\\w+", 77 "maxItems": 100, 78 "minItems": 5, 79 "uniqueItems": true, 80 "multipleOf": 5, 81 "enum": ["hello", "world"], 82 "type": "string", 83 "format": "date", 84 "name": "param-name", 85 "in": "header", 86 "required": true, 87 "schema": { 88 "type": "string" 89 }, 90 "collectionFormat": "csv", 91 "default": "8" 92 }` 93 94 func TestIntegrationParameter(t *testing.T) { 95 var actual Parameter 96 if assert.NoError(t, json.Unmarshal([]byte(parameterJSON), &actual)) { 97 assert.EqualValues(t, actual, parameter) 98 } 99 100 assertParsesJSON(t, parameterJSON, parameter) 101 } 102 103 func TestParameterRoundtrip(t *testing.T) { 104 cases := []jsontesting.RoundTripTestCase{ 105 { 106 // Show at least one field from each embededd struct sitll allows 107 // roundtrips successfully 108 Name: "UnmarshalEmbedded", 109 JSON: `{ 110 "$ref": "/components/ref/to/something.foo", 111 "maxLength": 100, 112 "type": "string", 113 "x-framework": "swagger-go", 114 "description": "a really cool description" 115 }`, 116 Object: &Parameter{ 117 Refable{MustCreateRef("/components/ref/to/something.foo")}, 118 CommonValidations{ 119 MaxLength: int64Ptr(100), 120 }, 121 SimpleSchema{ 122 Type: "string", 123 }, 124 VendorExtensible{Extensions{ 125 "x-framework": "swagger-go", 126 }}, 127 ParamProps{ 128 Description: "a really cool description", 129 }, 130 }, 131 }, { 132 Name: "BasicCase", 133 JSON: parameterJSON, 134 Object: ¶meter, 135 }, 136 } 137 138 for _, tcase := range cases { 139 t.Run(tcase.Name, func(t *testing.T) { 140 require.NoError(t, tcase.RoundTripTest(&Parameter{})) 141 }) 142 } 143 }