github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/scan/responses_test.go (about) 1 //go:build !go1.11 2 // +build !go1.11 3 4 // Copyright 2015 go-swagger maintainers 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package scan 19 20 import ( 21 goparser "go/parser" 22 "log" 23 "testing" 24 25 "github.com/go-openapi/spec" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestParseResponses(t *testing.T) { 30 docFile := "../fixtures/goparsing/classification/operations/responses.go" 31 fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments) 32 if err != nil { 33 log.Fatal(err) 34 } 35 36 rp := newResponseParser(classificationProg) 37 responses := make(map[string]spec.Response) 38 err = rp.Parse(fileTree, responses) 39 if err != nil { 40 t.Fatal(err) 41 } 42 assert.Len(t, responses, 9) 43 cr, ok := responses["complexerOne"] 44 assert.True(t, ok) 45 assert.Len(t, cr.Headers, 7) 46 for k, header := range cr.Headers { 47 switch k { 48 case "id": 49 assert.Equal(t, "integer", header.Type) 50 assert.Equal(t, "int64", header.Format) 51 case "name": 52 assert.Equal(t, "string", header.Type) 53 assert.Equal(t, "", header.Format) 54 case "age": 55 assert.Equal(t, "integer", header.Type) 56 assert.Equal(t, "int32", header.Format) 57 case "notes": 58 assert.Equal(t, "string", header.Type) 59 assert.Equal(t, "", header.Format) 60 case "extra": 61 assert.Equal(t, "string", header.Type) 62 assert.Equal(t, "", header.Format) 63 case "createdAt": 64 assert.Equal(t, "string", header.Type) 65 assert.Equal(t, "date-time", header.Format) 66 case "NoTagName": 67 assert.Equal(t, "string", header.Type) 68 assert.Equal(t, "", header.Format) 69 default: 70 assert.Fail(t, "unknown header: "+k) 71 } 72 } 73 74 cpr, ok := responses["complexerPointerOne"] 75 assert.True(t, ok) 76 assert.Len(t, cpr.Headers, 4) 77 for k, header := range cpr.Headers { 78 switch k { 79 case "id": 80 assert.Equal(t, "integer", header.Type) 81 assert.Equal(t, "int64", header.Format) 82 case "name": 83 assert.Equal(t, "string", header.Type) 84 assert.Equal(t, "", header.Format) 85 case "age": 86 assert.Equal(t, "integer", header.Type) 87 assert.Equal(t, "int32", header.Format) 88 case "extra": 89 assert.Equal(t, "integer", header.Type) 90 assert.Equal(t, "int64", header.Format) 91 default: 92 assert.Fail(t, "unknown header: "+k) 93 } 94 } 95 96 sos, ok := responses["simpleOnes"] 97 assert.True(t, ok) 98 assert.Len(t, sos.Headers, 1) 99 100 sosf, ok := responses["simpleOnesFunc"] 101 assert.True(t, ok) 102 assert.Len(t, sosf.Headers, 1) 103 104 res, ok := responses["someResponse"] 105 assert.True(t, ok) 106 assert.Len(t, res.Headers, 7) 107 108 for k, header := range res.Headers { 109 switch k { 110 case "id": 111 assert.Equal(t, "ID of this some response instance.\nids in this application start at 11 and are smaller than 1000", header.Description) 112 assert.Equal(t, "integer", header.Type) 113 assert.Equal(t, "int64", header.Format) 114 //assert.Equal(t, "ID", header.Extensions["x-go-name"]) 115 assert.EqualValues(t, 1000, *header.Maximum) 116 assert.True(t, header.ExclusiveMaximum) 117 assert.EqualValues(t, 10, *header.Minimum) 118 assert.True(t, header.ExclusiveMinimum) 119 assert.Equal(t, 11, header.Default, "ID default value is incorrect") 120 121 case "score": 122 assert.Equal(t, "The Score of this model", header.Description) 123 assert.Equal(t, "integer", header.Type) 124 assert.Equal(t, "int32", header.Format) 125 //assert.Equal(t, "Score", header.Extensions["x-go-name"]) 126 assert.EqualValues(t, 45, *header.Maximum) 127 assert.False(t, header.ExclusiveMaximum) 128 assert.EqualValues(t, 3, *header.Minimum) 129 assert.False(t, header.ExclusiveMinimum) 130 assert.Equal(t, 27, header.Example) 131 132 case "x-hdr-name": 133 assert.Equal(t, "Name of this some response instance", header.Description) 134 assert.Equal(t, "string", header.Type) 135 //assert.Equal(t, "Name", header.Extensions["x-go-name"]) 136 assert.EqualValues(t, 4, *header.MinLength) 137 assert.EqualValues(t, 50, *header.MaxLength) 138 assert.Equal(t, "[A-Za-z0-9-.]*", header.Pattern) 139 140 case "active": 141 assert.Equal(t, "Active state of the record", header.Description) 142 assert.Equal(t, "boolean", header.Type) 143 assert.Equal(t, true, header.Default) 144 145 case "created": 146 assert.Equal(t, "Created holds the time when this entry was created", header.Description) 147 assert.Equal(t, "string", header.Type) 148 assert.Equal(t, "date-time", header.Format) 149 //assert.Equal(t, "Created", header.Extensions["x-go-name"]) 150 151 case "foo_slice": 152 assert.Equal(t, "a FooSlice has foos which are strings", header.Description) 153 //assert.Equal(t, "FooSlice", header.Extensions["x-go-name"]) 154 assert.Equal(t, "array", header.Type) 155 assert.True(t, header.UniqueItems) 156 assert.Equal(t, "pipe", header.CollectionFormat) 157 assert.NotNil(t, header.Items, "foo_slice should have had an items property") 158 assert.EqualValues(t, 3, *header.MinItems, "'foo_slice' should have had 3 min items") 159 assert.EqualValues(t, 10, *header.MaxItems, "'foo_slice' should have had 10 max items") 160 itprop := header.Items 161 assert.EqualValues(t, 3, *itprop.MinLength, "'foo_slice.items.minLength' should have been 3") 162 assert.EqualValues(t, 10, *itprop.MaxLength, "'foo_slice.items.maxLength' should have been 10") 163 assert.EqualValues(t, "\\w+", itprop.Pattern, "'foo_slice.items.pattern' should have \\w+") 164 assert.Equal(t, "foo", itprop.Example) 165 166 case "bar_slice": 167 assert.Equal(t, "a BarSlice has bars which are strings", header.Description) 168 assert.Equal(t, "array", header.Type) 169 assert.True(t, header.UniqueItems) 170 assert.Equal(t, "pipe", header.CollectionFormat) 171 assert.NotNil(t, header.Items, "bar_slice should have had an items property") 172 assert.EqualValues(t, 3, *header.MinItems, "'bar_slice' should have had 3 min items") 173 assert.EqualValues(t, 10, *header.MaxItems, "'bar_slice' should have had 10 max items") 174 itprop := header.Items 175 if assert.NotNil(t, itprop) { 176 assert.EqualValues(t, 4, *itprop.MinItems, "'bar_slice.items.minItems' should have been 4") 177 assert.EqualValues(t, 9, *itprop.MaxItems, "'bar_slice.items.maxItems' should have been 9") 178 itprop2 := itprop.Items 179 if assert.NotNil(t, itprop2) { 180 assert.EqualValues(t, 5, *itprop2.MinItems, "'bar_slice.items.items.minItems' should have been 5") 181 assert.EqualValues(t, 8, *itprop2.MaxItems, "'bar_slice.items.items.maxItems' should have been 8") 182 itprop3 := itprop2.Items 183 if assert.NotNil(t, itprop3) { 184 assert.EqualValues(t, 3, *itprop3.MinLength, "'bar_slice.items.items.items.minLength' should have been 3") 185 assert.EqualValues(t, 10, *itprop3.MaxLength, "'bar_slice.items.items.items.maxLength' should have been 10") 186 assert.EqualValues(t, "\\w+", itprop3.Pattern, "'bar_slice.items.items.items.pattern' should have \\w+") 187 } 188 } 189 } 190 191 default: 192 assert.Fail(t, "unknown property: "+k) 193 } 194 } 195 196 assert.NotNil(t, res.Schema) 197 aprop := res.Schema 198 assert.Equal(t, "array", aprop.Type[0]) 199 assert.NotNil(t, aprop.Items) 200 assert.NotNil(t, aprop.Items.Schema) 201 itprop := aprop.Items.Schema 202 assert.Len(t, itprop.Properties, 4) 203 assert.Len(t, itprop.Required, 3) 204 assertProperty(t, itprop, "integer", "id", "int32", "ID") 205 iprop, ok := itprop.Properties["id"] 206 assert.True(t, ok) 207 assert.Equal(t, "ID of this some response instance.\nids in this application start at 11 and are smaller than 1000", iprop.Description) 208 assert.EqualValues(t, 1000, *iprop.Maximum) 209 assert.True(t, iprop.ExclusiveMaximum, "'id' should have had an exclusive maximum") 210 assert.NotNil(t, iprop.Minimum) 211 assert.EqualValues(t, 10, *iprop.Minimum) 212 assert.True(t, iprop.ExclusiveMinimum, "'id' should have had an exclusive minimum") 213 214 assertRef(t, itprop, "pet", "Pet", "#/definitions/pet") 215 iprop, ok = itprop.Properties["pet"] 216 assert.True(t, ok) 217 // if itprop.Ref.String() == "" { 218 // assert.Equal(t, "The Pet to add to this NoModel items bucket.\nPets can appear more than once in the bucket", iprop.Description) 219 // } 220 221 assertProperty(t, itprop, "integer", "quantity", "int16", "Quantity") 222 iprop, ok = itprop.Properties["quantity"] 223 assert.True(t, ok) 224 assert.Equal(t, "The amount of pets to add to this bucket.", iprop.Description) 225 assert.EqualValues(t, 1, *iprop.Minimum) 226 assert.EqualValues(t, 10, *iprop.Maximum) 227 228 assertProperty(t, itprop, "string", "notes", "", "Notes") 229 iprop, ok = itprop.Properties["notes"] 230 assert.True(t, ok) 231 assert.Equal(t, "Notes to add to this item.\nThis can be used to add special instructions.", iprop.Description) 232 233 res, ok = responses["resp"] 234 assert.True(t, ok) 235 assert.NotNil(t, res.Schema) 236 assert.Equal(t, "#/definitions/user", res.Schema.Ref.String()) 237 }