model_builder_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. package swagger
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. )
  7. type YesNo bool
  8. func (y YesNo) MarshalJSON() ([]byte, error) {
  9. if y {
  10. return []byte("yes"), nil
  11. }
  12. return []byte("no"), nil
  13. }
  14. // clear && go test -v -test.run TestRef_Issue190 ...swagger
  15. func TestRef_Issue190(t *testing.T) {
  16. type User struct {
  17. items []string
  18. }
  19. testJsonFromStruct(t, User{}, `{
  20. "swagger.User": {
  21. "id": "swagger.User",
  22. "required": [
  23. "items"
  24. ],
  25. "properties": {
  26. "items": {
  27. "type": "array",
  28. "items": {
  29. "type": "string"
  30. }
  31. }
  32. }
  33. }
  34. }`)
  35. }
  36. func TestWithoutAdditionalFormat(t *testing.T) {
  37. type mytime struct {
  38. time.Time
  39. }
  40. type usemytime struct {
  41. t mytime
  42. }
  43. testJsonFromStruct(t, usemytime{}, `{
  44. "swagger.usemytime": {
  45. "id": "swagger.usemytime",
  46. "required": [
  47. "t"
  48. ],
  49. "properties": {
  50. "t": {
  51. "type": "string"
  52. }
  53. }
  54. }
  55. }`)
  56. }
  57. func TestWithAdditionalFormat(t *testing.T) {
  58. type mytime struct {
  59. time.Time
  60. }
  61. type usemytime struct {
  62. t mytime
  63. }
  64. testJsonFromStructWithConfig(t, usemytime{}, `{
  65. "swagger.usemytime": {
  66. "id": "swagger.usemytime",
  67. "required": [
  68. "t"
  69. ],
  70. "properties": {
  71. "t": {
  72. "type": "string",
  73. "format": "date-time"
  74. }
  75. }
  76. }
  77. }`, &Config {
  78. SchemaFormatHandler: func(typeName string) string {
  79. switch typeName {
  80. case "swagger.mytime":
  81. return "date-time"
  82. }
  83. return ""
  84. },
  85. })
  86. }
  87. // clear && go test -v -test.run TestCustomMarshaller_Issue96 ...swagger
  88. func TestCustomMarshaller_Issue96(t *testing.T) {
  89. type Vote struct {
  90. What YesNo
  91. }
  92. testJsonFromStruct(t, Vote{}, `{
  93. "swagger.Vote": {
  94. "id": "swagger.Vote",
  95. "required": [
  96. "What"
  97. ],
  98. "properties": {
  99. "What": {
  100. "type": "string"
  101. }
  102. }
  103. }
  104. }`)
  105. }
  106. // clear && go test -v -test.run TestPrimitiveTypes ...swagger
  107. func TestPrimitiveTypes(t *testing.T) {
  108. type Prims struct {
  109. f float64
  110. t time.Time
  111. }
  112. testJsonFromStruct(t, Prims{}, `{
  113. "swagger.Prims": {
  114. "id": "swagger.Prims",
  115. "required": [
  116. "f",
  117. "t"
  118. ],
  119. "properties": {
  120. "f": {
  121. "type": "number",
  122. "format": "double"
  123. },
  124. "t": {
  125. "type": "string",
  126. "format": "date-time"
  127. }
  128. }
  129. }
  130. }`)
  131. }
  132. // clear && go test -v -test.run TestPrimitivePtrTypes ...swagger
  133. func TestPrimitivePtrTypes(t *testing.T) {
  134. type Prims struct {
  135. f *float64
  136. t *time.Time
  137. b *bool
  138. s *string
  139. i *int
  140. }
  141. testJsonFromStruct(t, Prims{}, `{
  142. "swagger.Prims": {
  143. "id": "swagger.Prims",
  144. "required": [
  145. "f",
  146. "t",
  147. "b",
  148. "s",
  149. "i"
  150. ],
  151. "properties": {
  152. "b": {
  153. "type": "boolean"
  154. },
  155. "f": {
  156. "type": "number",
  157. "format": "double"
  158. },
  159. "i": {
  160. "type": "integer",
  161. "format": "int32"
  162. },
  163. "s": {
  164. "type": "string"
  165. },
  166. "t": {
  167. "type": "string",
  168. "format": "date-time"
  169. }
  170. }
  171. }
  172. }`)
  173. }
  174. // clear && go test -v -test.run TestS1 ...swagger
  175. func TestS1(t *testing.T) {
  176. type S1 struct {
  177. Id string
  178. }
  179. testJsonFromStruct(t, S1{}, `{
  180. "swagger.S1": {
  181. "id": "swagger.S1",
  182. "required": [
  183. "Id"
  184. ],
  185. "properties": {
  186. "Id": {
  187. "type": "string"
  188. }
  189. }
  190. }
  191. }`)
  192. }
  193. // clear && go test -v -test.run TestS2 ...swagger
  194. func TestS2(t *testing.T) {
  195. type S2 struct {
  196. Ids []string
  197. }
  198. testJsonFromStruct(t, S2{}, `{
  199. "swagger.S2": {
  200. "id": "swagger.S2",
  201. "required": [
  202. "Ids"
  203. ],
  204. "properties": {
  205. "Ids": {
  206. "type": "array",
  207. "items": {
  208. "type": "string"
  209. }
  210. }
  211. }
  212. }
  213. }`)
  214. }
  215. // clear && go test -v -test.run TestS3 ...swagger
  216. func TestS3(t *testing.T) {
  217. type NestedS3 struct {
  218. Id string
  219. }
  220. type S3 struct {
  221. Nested NestedS3
  222. }
  223. testJsonFromStruct(t, S3{}, `{
  224. "swagger.NestedS3": {
  225. "id": "swagger.NestedS3",
  226. "required": [
  227. "Id"
  228. ],
  229. "properties": {
  230. "Id": {
  231. "type": "string"
  232. }
  233. }
  234. },
  235. "swagger.S3": {
  236. "id": "swagger.S3",
  237. "required": [
  238. "Nested"
  239. ],
  240. "properties": {
  241. "Nested": {
  242. "$ref": "swagger.NestedS3"
  243. }
  244. }
  245. }
  246. }`)
  247. }
  248. type sample struct {
  249. id string `swagger:"required"` // TODO
  250. items []item
  251. rootItem item `json:"root" description:"root desc"`
  252. }
  253. type item struct {
  254. itemName string `json:"name"`
  255. }
  256. // clear && go test -v -test.run TestSampleToModelAsJson ...swagger
  257. func TestSampleToModelAsJson(t *testing.T) {
  258. testJsonFromStruct(t, sample{items: []item{}}, `{
  259. "swagger.item": {
  260. "id": "swagger.item",
  261. "required": [
  262. "name"
  263. ],
  264. "properties": {
  265. "name": {
  266. "type": "string"
  267. }
  268. }
  269. },
  270. "swagger.sample": {
  271. "id": "swagger.sample",
  272. "required": [
  273. "id",
  274. "items",
  275. "root"
  276. ],
  277. "properties": {
  278. "id": {
  279. "type": "string"
  280. },
  281. "items": {
  282. "type": "array",
  283. "items": {
  284. "$ref": "swagger.item"
  285. }
  286. },
  287. "root": {
  288. "$ref": "swagger.item",
  289. "description": "root desc"
  290. }
  291. }
  292. }
  293. }`)
  294. }
  295. func TestJsonTags(t *testing.T) {
  296. type X struct {
  297. A string
  298. B string `json:"-"`
  299. C int `json:",string"`
  300. D int `json:","`
  301. }
  302. expected := `{
  303. "swagger.X": {
  304. "id": "swagger.X",
  305. "required": [
  306. "A",
  307. "C",
  308. "D"
  309. ],
  310. "properties": {
  311. "A": {
  312. "type": "string"
  313. },
  314. "C": {
  315. "type": "string"
  316. },
  317. "D": {
  318. "type": "integer",
  319. "format": "int32"
  320. }
  321. }
  322. }
  323. }`
  324. testJsonFromStruct(t, X{}, expected)
  325. }
  326. func TestJsonTagOmitempty(t *testing.T) {
  327. type X struct {
  328. A int `json:",omitempty"`
  329. B int `json:"C,omitempty"`
  330. }
  331. expected := `{
  332. "swagger.X": {
  333. "id": "swagger.X",
  334. "properties": {
  335. "A": {
  336. "type": "integer",
  337. "format": "int32"
  338. },
  339. "C": {
  340. "type": "integer",
  341. "format": "int32"
  342. }
  343. }
  344. }
  345. }`
  346. testJsonFromStruct(t, X{}, expected)
  347. }
  348. func TestJsonTagName(t *testing.T) {
  349. type X struct {
  350. A string `json:"B"`
  351. }
  352. expected := `{
  353. "swagger.X": {
  354. "id": "swagger.X",
  355. "required": [
  356. "B"
  357. ],
  358. "properties": {
  359. "B": {
  360. "type": "string"
  361. }
  362. }
  363. }
  364. }`
  365. testJsonFromStruct(t, X{}, expected)
  366. }
  367. func TestAnonymousStruct(t *testing.T) {
  368. type X struct {
  369. A struct {
  370. B int
  371. }
  372. }
  373. expected := `{
  374. "swagger.X": {
  375. "id": "swagger.X",
  376. "required": [
  377. "A"
  378. ],
  379. "properties": {
  380. "A": {
  381. "$ref": "swagger.X.A"
  382. }
  383. }
  384. },
  385. "swagger.X.A": {
  386. "id": "swagger.X.A",
  387. "required": [
  388. "B"
  389. ],
  390. "properties": {
  391. "B": {
  392. "type": "integer",
  393. "format": "int32"
  394. }
  395. }
  396. }
  397. }`
  398. testJsonFromStruct(t, X{}, expected)
  399. }
  400. func TestAnonymousPtrStruct(t *testing.T) {
  401. type X struct {
  402. A *struct {
  403. B int
  404. }
  405. }
  406. expected := `{
  407. "swagger.X": {
  408. "id": "swagger.X",
  409. "required": [
  410. "A"
  411. ],
  412. "properties": {
  413. "A": {
  414. "$ref": "swagger.X.A"
  415. }
  416. }
  417. },
  418. "swagger.X.A": {
  419. "id": "swagger.X.A",
  420. "required": [
  421. "B"
  422. ],
  423. "properties": {
  424. "B": {
  425. "type": "integer",
  426. "format": "int32"
  427. }
  428. }
  429. }
  430. }`
  431. testJsonFromStruct(t, X{}, expected)
  432. }
  433. func TestAnonymousArrayStruct(t *testing.T) {
  434. type X struct {
  435. A []struct {
  436. B int
  437. }
  438. }
  439. expected := `{
  440. "swagger.X": {
  441. "id": "swagger.X",
  442. "required": [
  443. "A"
  444. ],
  445. "properties": {
  446. "A": {
  447. "type": "array",
  448. "items": {
  449. "$ref": "swagger.X.A"
  450. }
  451. }
  452. }
  453. },
  454. "swagger.X.A": {
  455. "id": "swagger.X.A",
  456. "required": [
  457. "B"
  458. ],
  459. "properties": {
  460. "B": {
  461. "type": "integer",
  462. "format": "int32"
  463. }
  464. }
  465. }
  466. }`
  467. testJsonFromStruct(t, X{}, expected)
  468. }
  469. func TestAnonymousPtrArrayStruct(t *testing.T) {
  470. type X struct {
  471. A *[]struct {
  472. B int
  473. }
  474. }
  475. expected := `{
  476. "swagger.X": {
  477. "id": "swagger.X",
  478. "required": [
  479. "A"
  480. ],
  481. "properties": {
  482. "A": {
  483. "type": "array",
  484. "items": {
  485. "$ref": "swagger.X.A"
  486. }
  487. }
  488. }
  489. },
  490. "swagger.X.A": {
  491. "id": "swagger.X.A",
  492. "required": [
  493. "B"
  494. ],
  495. "properties": {
  496. "B": {
  497. "type": "integer",
  498. "format": "int32"
  499. }
  500. }
  501. }
  502. }`
  503. testJsonFromStruct(t, X{}, expected)
  504. }
  505. // go test -v -test.run TestEmbeddedStruct_Issue98 ...swagger
  506. func TestEmbeddedStruct_Issue98(t *testing.T) {
  507. type Y struct {
  508. A int
  509. }
  510. type X struct {
  511. Y
  512. }
  513. testJsonFromStruct(t, X{}, `{
  514. "swagger.X": {
  515. "id": "swagger.X",
  516. "required": [
  517. "A"
  518. ],
  519. "properties": {
  520. "A": {
  521. "type": "integer",
  522. "format": "int32"
  523. }
  524. }
  525. }
  526. }`)
  527. }
  528. type Dataset struct {
  529. Names []string
  530. }
  531. // clear && go test -v -test.run TestIssue85 ...swagger
  532. func TestIssue85(t *testing.T) {
  533. anon := struct{ Datasets []Dataset }{}
  534. testJsonFromStruct(t, anon, `{
  535. "struct { Datasets ||swagger.Dataset }": {
  536. "id": "struct { Datasets ||swagger.Dataset }",
  537. "required": [
  538. "Datasets"
  539. ],
  540. "properties": {
  541. "Datasets": {
  542. "type": "array",
  543. "items": {
  544. "$ref": "swagger.Dataset"
  545. }
  546. }
  547. }
  548. },
  549. "swagger.Dataset": {
  550. "id": "swagger.Dataset",
  551. "required": [
  552. "Names"
  553. ],
  554. "properties": {
  555. "Names": {
  556. "type": "array",
  557. "items": {
  558. "type": "string"
  559. }
  560. }
  561. }
  562. }
  563. }`)
  564. }
  565. type File struct {
  566. History []File
  567. HistoryPtrs []*File
  568. }
  569. // go test -v -test.run TestRecursiveStructure ...swagger
  570. func TestRecursiveStructure(t *testing.T) {
  571. testJsonFromStruct(t, File{}, `{
  572. "swagger.File": {
  573. "id": "swagger.File",
  574. "required": [
  575. "History",
  576. "HistoryPtrs"
  577. ],
  578. "properties": {
  579. "History": {
  580. "type": "array",
  581. "items": {
  582. "$ref": "swagger.File"
  583. }
  584. },
  585. "HistoryPtrs": {
  586. "type": "array",
  587. "items": {
  588. "$ref": "swagger.File"
  589. }
  590. }
  591. }
  592. }
  593. }`)
  594. }
  595. type A1 struct {
  596. B struct {
  597. Id int
  598. Comment string `json:"comment,omitempty"`
  599. }
  600. }
  601. // go test -v -test.run TestEmbeddedStructA1 ...swagger
  602. func TestEmbeddedStructA1(t *testing.T) {
  603. testJsonFromStruct(t, A1{}, `{
  604. "swagger.A1": {
  605. "id": "swagger.A1",
  606. "required": [
  607. "B"
  608. ],
  609. "properties": {
  610. "B": {
  611. "$ref": "swagger.A1.B"
  612. }
  613. }
  614. },
  615. "swagger.A1.B": {
  616. "id": "swagger.A1.B",
  617. "required": [
  618. "Id"
  619. ],
  620. "properties": {
  621. "Id": {
  622. "type": "integer",
  623. "format": "int32"
  624. },
  625. "comment": {
  626. "type": "string"
  627. }
  628. }
  629. }
  630. }`)
  631. }
  632. type A2 struct {
  633. C
  634. }
  635. type C struct {
  636. Id int `json:"B"`
  637. Comment string `json:"comment,omitempty"`
  638. Secure bool `json:"secure"`
  639. }
  640. // go test -v -test.run TestEmbeddedStructA2 ...swagger
  641. func TestEmbeddedStructA2(t *testing.T) {
  642. testJsonFromStruct(t, A2{}, `{
  643. "swagger.A2": {
  644. "id": "swagger.A2",
  645. "required": [
  646. "B",
  647. "secure"
  648. ],
  649. "properties": {
  650. "B": {
  651. "type": "integer",
  652. "format": "int32"
  653. },
  654. "comment": {
  655. "type": "string"
  656. },
  657. "secure": {
  658. "type": "boolean"
  659. }
  660. }
  661. }
  662. }`)
  663. }
  664. type A3 struct {
  665. B D
  666. }
  667. type D struct {
  668. Id int
  669. }
  670. // clear && go test -v -test.run TestStructA3 ...swagger
  671. func TestStructA3(t *testing.T) {
  672. testJsonFromStruct(t, A3{}, `{
  673. "swagger.A3": {
  674. "id": "swagger.A3",
  675. "required": [
  676. "B"
  677. ],
  678. "properties": {
  679. "B": {
  680. "$ref": "swagger.D"
  681. }
  682. }
  683. },
  684. "swagger.D": {
  685. "id": "swagger.D",
  686. "required": [
  687. "Id"
  688. ],
  689. "properties": {
  690. "Id": {
  691. "type": "integer",
  692. "format": "int32"
  693. }
  694. }
  695. }
  696. }`)
  697. }
  698. type A4 struct {
  699. D "json:,inline"
  700. }
  701. // clear && go test -v -test.run TestStructA4 ...swagger
  702. func TestEmbeddedStructA4(t *testing.T) {
  703. testJsonFromStruct(t, A4{}, `{
  704. "swagger.A4": {
  705. "id": "swagger.A4",
  706. "required": [
  707. "Id"
  708. ],
  709. "properties": {
  710. "Id": {
  711. "type": "integer",
  712. "format": "int32"
  713. }
  714. }
  715. }
  716. }`)
  717. }
  718. type A5 struct {
  719. D `json:"d"`
  720. }
  721. // clear && go test -v -test.run TestStructA5 ...swagger
  722. func TestEmbeddedStructA5(t *testing.T) {
  723. testJsonFromStruct(t, A5{}, `{
  724. "swagger.A5": {
  725. "id": "swagger.A5",
  726. "required": [
  727. "d"
  728. ],
  729. "properties": {
  730. "d": {
  731. "$ref": "swagger.D"
  732. }
  733. }
  734. },
  735. "swagger.D": {
  736. "id": "swagger.D",
  737. "required": [
  738. "Id"
  739. ],
  740. "properties": {
  741. "Id": {
  742. "type": "integer",
  743. "format": "int32"
  744. }
  745. }
  746. }
  747. }`)
  748. }
  749. type D2 struct {
  750. id int
  751. D []D
  752. }
  753. type A6 struct {
  754. D2 "json:,inline"
  755. }
  756. // clear && go test -v -test.run TestStructA4 ...swagger
  757. func TestEmbeddedStructA6(t *testing.T) {
  758. testJsonFromStruct(t, A6{}, `{
  759. "swagger.A6": {
  760. "id": "swagger.A6",
  761. "required": [
  762. "id",
  763. "D"
  764. ],
  765. "properties": {
  766. "D": {
  767. "type": "array",
  768. "items": {
  769. "$ref": "swagger.D"
  770. }
  771. },
  772. "id": {
  773. "type": "integer",
  774. "format": "int32"
  775. }
  776. }
  777. },
  778. "swagger.D": {
  779. "id": "swagger.D",
  780. "required": [
  781. "Id"
  782. ],
  783. "properties": {
  784. "Id": {
  785. "type": "integer",
  786. "format": "int32"
  787. }
  788. }
  789. }
  790. }`)
  791. }
  792. type ObjectId []byte
  793. type Region struct {
  794. Id ObjectId `bson:"_id" json:"id"`
  795. Name string `bson:"name" json:"name"`
  796. Type string `bson:"type" json:"type"`
  797. }
  798. // clear && go test -v -test.run TestRegion_Issue113 ...swagger
  799. func TestRegion_Issue113(t *testing.T) {
  800. testJsonFromStruct(t, []Region{}, `{
  801. "||swagger.Region": {
  802. "id": "||swagger.Region",
  803. "properties": {}
  804. },
  805. "swagger.Region": {
  806. "id": "swagger.Region",
  807. "required": [
  808. "id",
  809. "name",
  810. "type"
  811. ],
  812. "properties": {
  813. "id": {
  814. "type": "string"
  815. },
  816. "name": {
  817. "type": "string"
  818. },
  819. "type": {
  820. "type": "string"
  821. }
  822. }
  823. }
  824. }`)
  825. }
  826. // clear && go test -v -test.run TestIssue158 ...swagger
  827. func TestIssue158(t *testing.T) {
  828. type Address struct {
  829. Country string `json:"country,omitempty"`
  830. }
  831. type Customer struct {
  832. Name string `json:"name"`
  833. Address Address `json:"address"`
  834. }
  835. expected := `{
  836. "swagger.Address": {
  837. "id": "swagger.Address",
  838. "properties": {
  839. "country": {
  840. "type": "string"
  841. }
  842. }
  843. },
  844. "swagger.Customer": {
  845. "id": "swagger.Customer",
  846. "required": [
  847. "name",
  848. "address"
  849. ],
  850. "properties": {
  851. "address": {
  852. "$ref": "swagger.Address"
  853. },
  854. "name": {
  855. "type": "string"
  856. }
  857. }
  858. }
  859. }`
  860. testJsonFromStruct(t, Customer{}, expected)
  861. }
  862. func TestSlices(t *testing.T) {
  863. type Address struct {
  864. Country string `json:"country,omitempty"`
  865. }
  866. expected := `{
  867. "swagger.Address": {
  868. "id": "swagger.Address",
  869. "properties": {
  870. "country": {
  871. "type": "string"
  872. }
  873. }
  874. },
  875. "swagger.Customer": {
  876. "id": "swagger.Customer",
  877. "required": [
  878. "name",
  879. "addresses"
  880. ],
  881. "properties": {
  882. "addresses": {
  883. "type": "array",
  884. "items": {
  885. "$ref": "swagger.Address"
  886. }
  887. },
  888. "name": {
  889. "type": "string"
  890. }
  891. }
  892. }
  893. }`
  894. // both slices (with pointer value and with type value) should have equal swagger representation
  895. {
  896. type Customer struct {
  897. Name string `json:"name"`
  898. Addresses []Address `json:"addresses"`
  899. }
  900. testJsonFromStruct(t, Customer{}, expected)
  901. }
  902. {
  903. type Customer struct {
  904. Name string `json:"name"`
  905. Addresses []*Address `json:"addresses"`
  906. }
  907. testJsonFromStruct(t, Customer{}, expected)
  908. }
  909. }
  910. type Name struct {
  911. Value string
  912. }
  913. func (n Name) PostBuildModel(m *Model) *Model {
  914. m.Description = "titles must be upcase"
  915. return m
  916. }
  917. type TOC struct {
  918. Titles []Name
  919. }
  920. type Discography struct {
  921. Title Name
  922. TOC
  923. }
  924. // clear && go test -v -test.run TestEmbeddedStructPull204 ...swagger
  925. func TestEmbeddedStructPull204(t *testing.T) {
  926. b := Discography{}
  927. testJsonFromStruct(t, b, `
  928. {
  929. "swagger.Discography": {
  930. "id": "swagger.Discography",
  931. "required": [
  932. "Title",
  933. "Titles"
  934. ],
  935. "properties": {
  936. "Title": {
  937. "$ref": "swagger.Name"
  938. },
  939. "Titles": {
  940. "type": "array",
  941. "items": {
  942. "$ref": "swagger.Name"
  943. }
  944. }
  945. }
  946. },
  947. "swagger.Name": {
  948. "id": "swagger.Name",
  949. "required": [
  950. "Value"
  951. ],
  952. "properties": {
  953. "Value": {
  954. "type": "string"
  955. }
  956. }
  957. }
  958. }
  959. `)
  960. }
  961. type AddressWithMethod struct {
  962. Country string `json:"country,omitempty"`
  963. PostCode int `json:"postcode,omitempty"`
  964. }
  965. func (AddressWithMethod) SwaggerDoc() map[string]string {
  966. return map[string]string{
  967. "": "Address doc",
  968. "country": "Country doc",
  969. "postcode": "PostCode doc",
  970. }
  971. }
  972. func TestDocInMethodSwaggerDoc(t *testing.T) {
  973. expected := `{
  974. "swagger.AddressWithMethod": {
  975. "id": "swagger.AddressWithMethod",
  976. "description": "Address doc",
  977. "properties": {
  978. "country": {
  979. "type": "string",
  980. "description": "Country doc"
  981. },
  982. "postcode": {
  983. "type": "integer",
  984. "format": "int32",
  985. "description": "PostCode doc"
  986. }
  987. }
  988. }
  989. }`
  990. testJsonFromStruct(t, AddressWithMethod{}, expected)
  991. }
  992. type RefDesc struct {
  993. f1 *int64 `description:"desc"`
  994. }
  995. func TestPtrDescription(t *testing.T) {
  996. b := RefDesc{}
  997. expected := `{
  998. "swagger.RefDesc": {
  999. "id": "swagger.RefDesc",
  1000. "required": [
  1001. "f1"
  1002. ],
  1003. "properties": {
  1004. "f1": {
  1005. "type": "integer",
  1006. "format": "int64",
  1007. "description": "desc"
  1008. }
  1009. }
  1010. }
  1011. }`
  1012. testJsonFromStruct(t, b, expected)
  1013. }
  1014. type A struct {
  1015. B `json:",inline"`
  1016. C1 `json:"metadata,omitempty"`
  1017. }
  1018. type B struct {
  1019. SB string
  1020. }
  1021. type C1 struct {
  1022. SC string
  1023. }
  1024. func (A) SwaggerDoc() map[string]string {
  1025. return map[string]string{
  1026. "": "A struct",
  1027. "B": "B field", // We should not get anything from this
  1028. "metadata": "C1 field",
  1029. }
  1030. }
  1031. func (B) SwaggerDoc() map[string]string {
  1032. return map[string]string{
  1033. "": "B struct",
  1034. "SB": "SB field",
  1035. }
  1036. }
  1037. func (C1) SwaggerDoc() map[string]string {
  1038. return map[string]string{
  1039. "": "C1 struct",
  1040. "SC": "SC field",
  1041. }
  1042. }
  1043. func TestNestedStructDescription(t *testing.T) {
  1044. expected := `
  1045. {
  1046. "swagger.A": {
  1047. "id": "swagger.A",
  1048. "description": "A struct",
  1049. "required": [
  1050. "SB"
  1051. ],
  1052. "properties": {
  1053. "SB": {
  1054. "type": "string",
  1055. "description": "SB field"
  1056. },
  1057. "metadata": {
  1058. "$ref": "swagger.C1",
  1059. "description": "C1 field"
  1060. }
  1061. }
  1062. },
  1063. "swagger.C1": {
  1064. "id": "swagger.C1",
  1065. "description": "C1 struct",
  1066. "required": [
  1067. "SC"
  1068. ],
  1069. "properties": {
  1070. "SC": {
  1071. "type": "string",
  1072. "description": "SC field"
  1073. }
  1074. }
  1075. }
  1076. }
  1077. `
  1078. testJsonFromStruct(t, A{}, expected)
  1079. }
  1080. // This tests a primitive with type overrides in the struct tags
  1081. type FakeInt int
  1082. type E struct {
  1083. Id FakeInt `type:"integer"`
  1084. IP net.IP `type:"string"`
  1085. }
  1086. func TestOverridenTypeTagE1(t *testing.T) {
  1087. expected := `
  1088. {
  1089. "swagger.E": {
  1090. "id": "swagger.E",
  1091. "required": [
  1092. "Id",
  1093. "IP"
  1094. ],
  1095. "properties": {
  1096. "Id": {
  1097. "type": "integer"
  1098. },
  1099. "IP": {
  1100. "type": "string"
  1101. }
  1102. }
  1103. }
  1104. }
  1105. `
  1106. testJsonFromStruct(t, E{}, expected)
  1107. }