github.com/circl-dev/go-swagger@v0.31.0/examples/external-types/example-external-types.yaml (about) 1 --- 2 swagger: "2.0" 3 info: 4 version: "1.0" 5 title: "external types imports: external anonymous types" 6 description: | 7 This sample specification exercises external types, with both x-go-type in definitions and inlined. 8 9 It demonstrates how to use the x-go-type extension to plug external type definitions in the generated code, 10 for models (e.g. for properties, arrays or maps) or operations. 11 12 Notice that x-go-type works for schemas and is not supported for simple swagger types, 13 used for response headers and query & path parameters. 14 15 paths: 16 /test: 17 18 get: 19 responses: 20 default: 21 description: | 22 A reference to a type already defined in the models package 23 (defaults to <<target>/models, defined by CLI flag --model-package). 24 The response payload is defined as: *models.Zzz 25 26 schema: 27 $ref: "#/definitions/Zzz" 28 29 put: 30 parameters: 31 - in: body 32 name: myAlternate 33 required: true 34 description: | 35 A reference to a type defined in the "fred" package, aliased 36 as "alternate". 37 38 MyAlternate alternate.MyAlternateType 39 40 schema: 41 $ref: '#/definitions/MyCustom' 42 43 responses: 44 default: 45 description: | 46 A map of an aliased external package. Now the alias is "custom". 47 This response is defined as: map[string]custom.MyAlternateString 48 49 schema: 50 type: object 51 additionalProperties: 52 type: object 53 description: | 54 This uses the external type from an inline definition, without $ref 55 56 x-go-type: 57 type: MyAlternateString 58 import: 59 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 60 alias: custom 61 62 post: 63 64 parameters: 65 - in: body 66 name: customizedStrings 67 description: | 68 Defines a parameter as an array of external types. 69 The body parameter is defined as []custom.MyAlternateString 70 71 No definition is generated in models. 72 schema: 73 type: array 74 items: 75 type: string 76 x-go-type: 77 type: MyAlternateString 78 import: 79 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 80 alias: custom 81 82 responses: 83 default: 84 description: | 85 An inlined reference to an aliased external package. 86 The response is defined as map[string][]map[string]custom.MyAlternateString 87 88 No definition is generated in models. 89 schema: 90 type: object 91 additionalProperties: 92 type: array 93 items: 94 type: object 95 additionalProperties: 96 x-go-type: 97 type: MyAlternateString 98 import: 99 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 100 alias: custom 101 102 /stream: 103 get: 104 responses: 105 default: 106 description: | 107 Uses an external definition for an interface (e.g. io.Reader) 108 109 No validation is expected on binary format. 110 111 schema: 112 $ref: "#/definitions/MyReader" 113 114 definitions: 115 Zzz: 116 description: | 117 This demonstrates variations in generated code, depending on how properties are declared. 118 119 Some properties are directly based on some external type and some other define collections (slices, maps) 120 of these external types. 121 122 Notice the use of pointers for required properties, but not for slices or maps. 123 124 In addition, it demonstrates how pointer generation may be controlled with the nullable hint or the x-nullable extension. 125 126 type Zzz struct { 127 Beta []MyOtherType `json:"beta"` 128 Delta MyInteger `json:"delta,omitempty"` 129 Epsilon []custom.MyAlternateType `json:"epsilon"` 130 Gamma fred.MyAlternateInteger `json:"gamma,omitempty"` 131 Meta MyType `json:"meta,omitempty"` 132 133 NullableBeta []*MyOtherType `json:"nullableBeta"` 134 NullableDelta *MyInteger `json:"nullableDelta,omitempty"` 135 NullableEpsilon []*custom.MyAlternateType `json:"nullableEpsilon"` 136 NullableGamma *fred.MyAlternateInteger `json:"nullableGamma,omitempty"` 137 NullableMeta MyType `json:"nullableMeta,omitempty"` 138 139 ReqBeta []MyOtherType `json:"reqBeta"` 140 ReqDelta *MyInteger `json:"reqDelta"` 141 ReqEpsilon []custom.MyAlternateType `json:"reqEpsilon"` 142 ReqGamma *fred.MyAlternateInteger `json:"reqGamma"` 143 ReqMeta *MyType `json:"reqMeta"` 144 } 145 146 type: object 147 required: [ reqBeta, reqDelta, reqGamma, reqEpsilon, reqMeta ] 148 properties: 149 meta: 150 $ref: '#/definitions/MyType' # <- a property based on an external type definition (see below) 151 152 beta: 153 description: | 154 This property defines an array of external types (in the same package). 155 156 []MyOtherType 157 158 The maxItems validation is generated and the external validation is called for every item. 159 160 type: array 161 maxItems: 15 162 items: 163 type: object 164 x-go-type: 165 type: MyOtherType 166 167 delta: 168 description: | 169 A type is provided (integer), and the implementation is an external type in the same package. 170 171 The maximum validation remains documentary and is ignored by the generated code. 172 173 type: integer 174 maximum: 15 175 x-go-type: 176 type: MyInteger 177 178 gamma: 179 description: | 180 Property defined as an external type from package "fred" 181 182 x-go-type: 183 type: MyAlternateInteger 184 import: 185 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 186 187 epsilon: 188 type: array 189 items: 190 type: object 191 x-go-type: 192 type: MyAlternateType 193 import: 194 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 195 alias: custom 196 197 reqMeta: 198 $ref: '#/definitions/MyType' 199 200 reqBeta: 201 type: array 202 items: 203 type: object 204 x-go-type: 205 type: MyOtherType 206 207 reqDelta: 208 type: integer 209 x-go-type: 210 type: MyInteger 211 212 reqGamma: 213 x-go-type: 214 type: MyAlternateInteger 215 import: 216 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 217 218 reqEpsilon: 219 type: array 220 items: 221 type: object 222 x-go-type: 223 type: MyAlternateType 224 import: 225 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 226 alias: custom 227 228 nullableMeta: 229 $ref: '#/definitions/MyType' # <- a property based on an external type definition (see below) 230 x-nullable: true # <- this is ignored because it is next to a $ref 231 232 nullableBeta: 233 description: | 234 This property defines an array of external types (in the same package). 235 236 []MyOtherType 237 238 The maxItems validation is generated and the external validation is called for every item. 239 240 type: array 241 maxItems: 15 242 items: 243 type: object 244 x-go-type: 245 type: MyOtherType 246 hints: 247 nullable: true 248 249 nullableDelta: 250 description: | 251 A type is provided (integer), and the implementation is an external type in the same package. 252 253 The maximum validation remains documentary and is ignored by the generated code. 254 255 NullableDelta *MyInteger 256 257 type: integer 258 maximum: 15 259 x-go-type: 260 type: MyInteger 261 x-nullable: true 262 263 nullableGamma: 264 description: | 265 Property defined as an external type from package "fred", with a nullable hint for the 266 external type. 267 268 NullableGamma *fred.MyAlternateInteger `json:"nullableGamma,omitempty"` 269 270 x-go-type: 271 type: MyAlternateInteger 272 import: 273 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 274 hints: 275 nullable: true 276 277 nullableEpsilon: 278 description: | 279 In this example, items are made nullable. 280 281 NullableEpsilon []*custom.MyAlternateType `json:"nullableEpsilon"` 282 283 type: array 284 items: 285 type: object 286 x-go-type: 287 type: MyAlternateType 288 import: 289 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 290 alias: custom 291 x-nullable: true 292 293 294 MyType: 295 description: | 296 The generated code expects this type to be already defined in the models package 297 (default location when no package is specified). 298 299 type: object 300 x-go-type: 301 type: MyType 302 303 MyCustom: 304 description: | 305 The generated code expects this type to be already defined in the "fred" package. 306 An alias is used by the generated code. Aliases are convenient to avoid 307 conflicts with other imports or variables in the generated code. 308 309 type: object 310 x-go-type: 311 type: MyAlternateType 312 import: 313 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 314 alias: alternate 315 316 MyCustomArray: 317 description: | 318 This generate an array type in models, based on the external type. 319 320 []alternate.MyAlternateType 321 322 The validation method of the external type is called by the generated array. 323 324 type: array 325 items: 326 type: object 327 x-go-type: 328 type: MyAlternateType 329 import: 330 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 331 alias: alternate 332 333 MyCustomArrayNullable: 334 description: | 335 This generate an array type in models, based on the external type. 336 Notice the impact of the nullable hint (equivalent to x-nullable at the type level), to produce a slice of pointers. 337 338 []*alternate.MyAlternateType 339 340 type: array 341 items: 342 type: object 343 x-go-type: 344 type: MyAlternateType 345 import: 346 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 347 alias: alternate 348 hints: 349 nullable: true 350 351 MyCustomMap: 352 description: | 353 This generate a map type in models, based on the external type. 354 355 MyCustomMap map[string]map[string]alternate.MyAlternateType 356 357 The validation method of the external type is called by the generated map. 358 359 type: object 360 additionalProperties: 361 type: object 362 additionalProperties: 363 type: object 364 x-go-type: 365 type: MyAlternateType 366 import: 367 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 368 alias: alternate 369 370 MyCustomMapNullable: 371 description: | 372 This generate a map type in models, based on the external type. 373 Notice the impact of the x-nullable directive, to produce a map of pointers. 374 375 MapNullable map[string]map[string]*alternate.MyAlternateType 376 377 type: object 378 additionalProperties: 379 type: object 380 additionalProperties: 381 type: object 382 x-go-type: 383 type: MyAlternateType 384 import: 385 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 386 alias: alternate 387 x-nullable: true 388 389 MyReader: 390 description: | 391 This is an external type replacing the io.Reader type normally generated. 392 393 No validation is called on such a type. 394 395 type: string 396 format: binary 397 x-go-type: 398 type: MyStreamer 399 400 MyInterface: 401 description: | 402 This is an external type replacing the interface{} type normally generated. 403 404 No validation is called on such a type. 405 406 This demonstrates how to use hints in x-go-type: by default, the generator 407 assumes a struct with some Validate method. 408 409 By providing the "interface" hint, validation is disabled. Notice that we don't 410 generate pointers on interfaces. 411 412 If no hint is provided, the generate code won't compile is the MyAlternateInterface 413 does not provide a Validate method. 414 415 x-go-type: 416 type: MyAlternateInterface 417 import: 418 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 419 hints: 420 kind: interface 421 422 MyExtStruct: 423 description: | 424 This type is located in a package which requires name mangling. 425 426 x-go-type: 427 type: MyExtType 428 import: 429 package: "github.com/circl-dev/go-swagger/examples/external-types/go-ext" 430 431 MyExtCollection: 432 description: | 433 This type demonstrates the import generation with name mangling 434 435 type: array 436 items: 437 $ref: '#/definitions/MyExtStruct' 438 439 MyReaderObject: 440 description: 441 This object demonstrates several ways to refer to an external interface (here assumed akin to io.Reader). 442 443 MarshalBinary() methods are generated. No validation is expected on binary format. 444 445 type: object 446 properties: 447 reader1: 448 $ref: '#/definitions/MyReader' # <- reuse external definition from spec 449 reader2: 450 description: | 451 In line definition of the external type. 452 453 Notice that we have provided some information in the spec, so the generator 454 can infer we want it to be understood as an io.Reader, with no validation. 455 456 type: string 457 format: binary 458 x-go-type: 459 type: MyAlternateStreamer 460 import: 461 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 462 alias: alternate 463 reader3: 464 description: | 465 In line definition of the external type. 466 467 Notice that we have provided some information in the spec, as a hint in the extension 468 rather than in the type definition. 469 470 So this will be documented as an object, but the generated code knows this is a stream. 471 472 x-go-type: 473 type: MyAlternateStreamer 474 import: 475 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 476 hints: 477 kind: stream 478 479 MyInterfaceObject: 480 description: | 481 This object demonstrates several ways to refer to an external interface. 482 483 The generated code behaves as it is an interface{}: no pointers are generated, and no valication 484 is required. 485 486 type: object 487 required: [ iface2, iface3 ] 488 properties: 489 iface1: 490 $ref: '#/definitions/MyInterface' 491 iface2: 492 description: | 493 Demonstrates the impact of the "interface" hint: no validation is called on iface2, 494 and no pointer is generated in spite of the "required" directive. 495 496 The generated object validation checks for the "required" directive. 497 498 Without the hint, the generator assumes a Validatable object, with pointer, which may 499 not build, depending on how the external type is defined. 500 501 x-go-type: 502 type: MyAlternateInterface 503 import: 504 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 505 hints: 506 kind: interface 507 508 iface3: 509 description: | 510 Demonstrates the impact of the "noValidation" hint. 511 512 Notice how we avoid the generation of a pointer on the required json.RawMessage (which is a []byte) 513 with the "nullable" hint. 514 515 Notice that the "json" package is automatically deconflicted from other standard imports with a distinct alias. 516 517 x-go-type: 518 type: RawMessage 519 import: 520 package: "encoding/json" 521 hints: 522 nullable: false 523 noValidation: true 524 525 MyTuple: 526 description: | 527 Demonstrates references to some external type in the context of a tuple. 528 529 Notice that "additionalItems" is not a construct that pass swagger validation, 530 but is supported by go-swagger. 531 532 type: array 533 items: 534 - $ref: '#/definitions/MyType' 535 - type: object 536 description: | 537 Second element of the tuple, defined as follows. 538 539 P1 *fred.MyAlternateType `json:"-"` // custom serializer 540 541 x-go-type: 542 type: MyAlternateType 543 import: 544 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 545 additionalItems: 546 description: | 547 Additional items to a tuple, from an external type. 548 This defines the following field in the tuple 549 550 MyTupleItems []map[string]fred.MyAlternateType 551 552 type: object 553 additionalProperties: 554 x-go-type: 555 type: MyAlternateType 556 import: 557 package: "github.com/circl-dev/go-swagger/examples/external-types/fred" 558 559 EmbeddedTime: 560 description: | 561 562 This type demonstrates how we can embed an external type and wraps the validation. 563 564 This is especially useful if you want to reuse some types from the standard library, 565 such as `time.Time` or `json.RawMessage`. 566 567 x-go-type: 568 type: Time 569 import: 570 package: time 571 embedded: true 572 573 NoValidateExternal: 574 description: | 575 576 This type demonstrates how we can disable validation for an external type. 577 578 This is useful if you want to reuse some types from the standard library and don't 579 want to resort to an embedded type. 580 581 x-go-type: 582 type: Request 583 import: 584 package: net/http 585 hints: 586 noValidation: true 587 588 ObjectWithNoValidate: 589 description: | 590 A reference to the NoValidateExternal external type. 591 592 If the "noValidation" hint is omitted in the definition above, this code won't build because `http.Request` has no `Validate` method. 593 594 type: object 595 properties: 596 request: 597 $ref: '#/definitions/NoValidateExternal' 598 599 NoValidateExternal: 600 description: | 601 602 This type demonstrates how we can disable validation for an external type. 603 604 This is useful if you want to reuse some types from the standard library and don't 605 want to resort to an embedded type. 606 607 type: object 608 x-go-type: 609 type: Request 610 import: 611 package: net/http 612 hints: 613 noValidation: true 614 615 ObjectWithNoValidate: 616 description: | 617 A reference to the NoValidateExternal external type. 618 619 If the "noValidation" hint is omitted in the definition above, this code won't build because `http.Request` has no `Validate` method. 620 621 type: object 622 required: [ myMandatoryRequest ] 623 properties: 624 myRequest: 625 $ref: '#/definitions/NoValidateExternal' 626 myMandatoryRequest: 627 $ref: '#/definitions/NoValidateExternal' 628 629 ArrayWithNoValidate: 630 description: | 631 A slice of NoValidateExternal external types. 632 633 If the "noValidation" hint is omitted in the definition above, this code won't build because `http.Request` has no `Validate` method. 634 635 type: array 636 maxItems: 12 637 uniqueItems: true 638 items: 639 $ref: '#/definitions/NoValidateExternal' 640 641 MapWithNoValidate: 642 description: | 643 A map of NoValidateExternal external types. 644 645 If the "noValidation" hint is omitted in the definition above, this code won't build because `http.Request` has no `Validate` method. 646 647 type: object 648 additionalProperties: 649 $ref: '#/definitions/NoValidateExternal' 650 651 TupleWithNoValidate: 652 description: | 653 A tuple of NoValidateExternal external types. 654 655 If the "noValidation" hint is omitted in the definition above, this code won't build because `http.Request` has no `Validate` method. 656 657 Notice that "additionalItems" is not a construct that pass swagger validation, 658 but is supported by go-swagger. 659 660 type: array 661 items: 662 - $ref: '#/definitions/NoValidateExternal' 663 - $ref: '#/definitions/NoValidateExternal' 664 additionalItems: 665 $ref: '#/definitions/NoValidateExternal' 666 667 NoValidatePrimitive: 668 description: | 669 670 This type demonstrates how we can disable validation for an external primitive type. 671 672 type: integer 673 x-go-type: 674 type: Duration 675 import: 676 package: time 677 hints: 678 noValidation: true 679 680 MapOfPrimitives: 681 description: | 682 A map of NoValidatePrimitive external types. 683 684 If the "noValidation" hint is omitted in the definition above, this code won't build because `time.Duration` has no `Validate` method. 685 686 type: object 687 additionalProperties: 688 $ref: '#/definitions/NoValidatePrimitive' 689 690 # 691 # Currently unsupported constructs: inline definitions of embedded external types 692 # 693 # 694 # ArrayWithInlinedEmbedded: 695 # type: array 696 # items: 697 # x-go-type: 698 # type: Time 699 # import: 700 # package: time 701 # embedded: true 702 # 703 # MapWithInlinedEmbedded: 704 # type: object 705 # additionalProperties: 706 # x-go-type: 707 # type: Time 708 # import: 709 # package: time 710 # embedded: true 711 # 712 # ObjectWithInlinedEmbedded: 713 # type: object 714 # properties: 715 # p1: 716 # x-go-type: 717 # type: Time 718 # import: 719 # package: time 720 # embedded: true 721 #