github.com/6543-forks/go-swagger@v0.26.0/generator/types_test.go (about) 1 package generator 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "log" 7 "os" 8 "strconv" 9 "testing" 10 11 "github.com/go-openapi/loads" 12 "github.com/go-openapi/spec" 13 "github.com/stretchr/testify/require" 14 ) 15 16 type externalTypeFixture struct { 17 schema string 18 expected *externalTypeDefinition 19 knownDefs struct{ tpe, pkg, alias string } 20 resolved resolvedType 21 } 22 23 func makeResolveExternalTypes() []externalTypeFixture { 24 return []externalTypeFixture{ 25 { 26 schema: `{ 27 "type": "object", 28 "x-go-type": { 29 "type": "Mytype", 30 "import": { 31 "package": "github.com/fredbi/mymodels", 32 "alias": "external" 33 }, 34 "hints": { 35 "kind": "map" 36 }, 37 "embedded": false 38 } 39 }`, 40 expected: &externalTypeDefinition{ 41 Type: "Mytype", 42 Import: struct { 43 Package string 44 Alias string 45 }{ 46 Package: "github.com/fredbi/mymodels", 47 Alias: "external", 48 }, 49 Hints: struct { 50 Kind string 51 Nullable bool 52 }{ 53 Kind: "map", 54 }, 55 Embedded: false, 56 }, 57 knownDefs: struct{ tpe, pkg, alias string }{ 58 tpe: "external.Mytype", 59 pkg: "github.com/fredbi/mymodels", 60 alias: "external", 61 }, 62 resolved: resolvedType{ 63 GoType: "external.Mytype", 64 IsMap: true, 65 SwaggerType: "object", 66 IsEmptyOmitted: true, 67 Pkg: "github.com/fredbi/mymodels", 68 PkgAlias: "external", 69 }, 70 }, 71 { 72 schema: `{ 73 "type": "object", 74 "x-go-type": { 75 "type": "Mytype", 76 "import": { 77 "package": "github.com/fredbi/mymodels", 78 "alias": "external" 79 }, 80 "hints": { 81 "kind": "map" 82 }, 83 "embedded": true 84 } 85 }`, 86 expected: &externalTypeDefinition{ 87 Type: "Mytype", 88 Import: struct { 89 Package string 90 Alias string 91 }{ 92 Package: "github.com/fredbi/mymodels", 93 Alias: "external", 94 }, 95 Hints: struct { 96 Kind string 97 Nullable bool 98 }{ 99 Kind: "map", 100 }, 101 Embedded: true, 102 }, 103 knownDefs: struct{ tpe, pkg, alias string }{ 104 tpe: "A", 105 pkg: "", 106 alias: "", 107 }, 108 resolved: resolvedType{ 109 GoType: "A", 110 IsMap: true, 111 SwaggerType: "object", 112 IsEmptyOmitted: true, 113 }, 114 }, 115 { 116 schema: `{ 117 "type": "object", 118 "x-go-type": { 119 "type": "Mytype", 120 "import": { 121 "package": "github.com/fredbi/mymodels" 122 }, 123 "hints": { 124 "kind": "array", 125 "nullable": true 126 } 127 } 128 }`, 129 expected: &externalTypeDefinition{ 130 Type: "Mytype", 131 Import: struct { 132 Package string 133 Alias string 134 }{ 135 Package: "github.com/fredbi/mymodels", 136 Alias: "mymodels", 137 }, 138 Hints: struct { 139 Kind string 140 Nullable bool 141 }{ 142 Kind: "array", 143 Nullable: true, 144 }, 145 Embedded: false, 146 }, 147 knownDefs: struct{ tpe, pkg, alias string }{tpe: "mymodels.Mytype", pkg: "github.com/fredbi/mymodels", alias: "mymodels"}, 148 resolved: resolvedType{ 149 GoType: "mymodels.Mytype", 150 IsArray: true, 151 SwaggerType: "array", 152 IsEmptyOmitted: false, 153 Pkg: "github.com/fredbi/mymodels", 154 PkgAlias: "mymodels", 155 }, 156 }, 157 { 158 schema: `{ 159 "type": "object", 160 "x-go-type": { 161 "type": "Mytype", 162 "import": { 163 "package": "github.com/fredbi/mymodels" 164 }, 165 "hints": { 166 "kind": "map" 167 } 168 } 169 }`, 170 expected: &externalTypeDefinition{ 171 Type: "Mytype", 172 Import: struct { 173 Package string 174 Alias string 175 }{ 176 Package: "github.com/fredbi/mymodels", 177 Alias: "mymodels", 178 }, 179 Hints: struct { 180 Kind string 181 Nullable bool 182 }{ 183 Kind: "map", 184 }, 185 }, 186 knownDefs: struct{ tpe, pkg, alias string }{tpe: "mymodels.Mytype", pkg: "github.com/fredbi/mymodels", alias: "mymodels"}, 187 resolved: resolvedType{ 188 GoType: "mymodels.Mytype", 189 IsMap: true, 190 SwaggerType: "object", 191 IsEmptyOmitted: true, 192 Pkg: "github.com/fredbi/mymodels", 193 PkgAlias: "mymodels", 194 }, 195 }, 196 { 197 schema: `{ 198 "type": "object", 199 "x-go-type": { 200 "type": "Mytype", 201 "import": { 202 "package": "github.com/fredbi/mymodels" 203 }, 204 "hints": { 205 "kind": "tuple" 206 } 207 } 208 }`, 209 expected: &externalTypeDefinition{ 210 Type: "Mytype", 211 Import: struct { 212 Package string 213 Alias string 214 }{ 215 Package: "github.com/fredbi/mymodels", 216 Alias: "mymodels", 217 }, 218 Hints: struct { 219 Kind string 220 Nullable bool 221 }{ 222 Kind: "tuple", 223 }, 224 }, 225 knownDefs: struct{ tpe, pkg, alias string }{tpe: "mymodels.Mytype", pkg: "github.com/fredbi/mymodels", alias: "mymodels"}, 226 resolved: resolvedType{ 227 GoType: "mymodels.Mytype", 228 IsTuple: true, 229 SwaggerType: "array", 230 IsEmptyOmitted: true, 231 Pkg: "github.com/fredbi/mymodels", 232 PkgAlias: "mymodels", 233 }, 234 }, 235 { 236 schema: `{ 237 "type": "number", 238 "x-go-type": { 239 "type": "Mytype", 240 "import": { 241 "package": "github.com/fredbi/mymodels" 242 }, 243 "hints": { 244 "kind": "primitive" 245 } 246 } 247 }`, 248 expected: &externalTypeDefinition{ 249 Type: "Mytype", 250 Import: struct { 251 Package string 252 Alias string 253 }{ 254 Package: "github.com/fredbi/mymodels", 255 Alias: "mymodels", 256 }, 257 Hints: struct { 258 Kind string 259 Nullable bool 260 }{ 261 Kind: "primitive", 262 }, 263 }, 264 knownDefs: struct{ tpe, pkg, alias string }{tpe: "mymodels.Mytype", pkg: "github.com/fredbi/mymodels", alias: "mymodels"}, 265 resolved: resolvedType{ 266 GoType: "mymodels.Mytype", 267 IsPrimitive: true, 268 SwaggerType: "", 269 IsEmptyOmitted: true, 270 Pkg: "github.com/fredbi/mymodels", 271 PkgAlias: "mymodels", 272 }, 273 }, 274 } 275 } 276 277 func TestShortCircuitResolveExternal(t *testing.T) { 278 log.SetOutput(ioutil.Discard) 279 defer func() { 280 log.SetOutput(os.Stdout) 281 }() 282 283 for i, toPin := range makeResolveExternalTypes() { 284 fixture := toPin 285 t.Run(strconv.Itoa(i), func(t *testing.T) { 286 jazonDoc := fixture.schema 287 doc, err := loads.Embedded([]byte(jazonDoc), []byte(jazonDoc)) 288 require.NoErrorf(t, err, "fixture %d", i) 289 290 r := newTypeResolver("models", doc) 291 var schema spec.Schema 292 err = json.Unmarshal([]byte(jazonDoc), &schema) 293 require.NoErrorf(t, err, "fixture %d", i) 294 295 extType, ok := hasExternalType(schema.Extensions) 296 require.Truef(t, ok, "fixture %d", i) 297 298 require.EqualValuesf(t, fixture.expected, extType, "fixture %d", i) 299 300 tpe, pkg, alias := knownDefGoType("A", schema, r.goTypeName) 301 require.EqualValuesf(t, fixture.knownDefs, struct{ tpe, pkg, alias string }{tpe, pkg, alias}, "fixture %d", i) 302 303 resolved := r.shortCircuitResolveExternal(tpe, pkg, alias, extType, &schema) 304 305 resolved.Extensions = nil // don't assert this 306 require.EqualValuesf(t, fixture.resolved, resolved, "fixture %d", i) 307 }) 308 } 309 }