fn.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. import { fromJS } from "immutable"
  2. import { createXMLExample, sampleFromSchema, memoizedCreateXMLExample, memoizedSampleFromSchema } from "corePlugins/samples/fn"
  3. describe("sampleFromSchema", () => {
  4. it("handles Immutable.js objects for nested schemas", function () {
  5. let definition = fromJS({
  6. "type": "object",
  7. "properties": {
  8. "json": {
  9. "type": "object",
  10. "example": {
  11. "a": "string"
  12. },
  13. "properties": {
  14. "a": {
  15. "type": "string"
  16. }
  17. }
  18. }
  19. }
  20. })
  21. let expected = {
  22. json: {
  23. a: "string"
  24. }
  25. }
  26. expect(sampleFromSchema(definition, { includeReadOnly: false })).toEqual(expected)
  27. })
  28. it("returns object with no readonly fields for parameter", function () {
  29. let definition = {
  30. type: "object",
  31. properties: {
  32. id: {
  33. type: "integer"
  34. },
  35. readOnlyDog: {
  36. readOnly: true,
  37. type: "string"
  38. }
  39. },
  40. xml: {
  41. name: "animals"
  42. }
  43. }
  44. let expected = {
  45. id: 0
  46. }
  47. expect(sampleFromSchema(definition, { includeReadOnly: false })).toEqual(expected)
  48. })
  49. it("returns object with readonly fields for parameter, with includeReadOnly", function () {
  50. let definition = {
  51. type: "object",
  52. properties: {
  53. id: {
  54. type: "integer"
  55. },
  56. readOnlyDog: {
  57. readOnly: true,
  58. type: "string"
  59. }
  60. },
  61. xml: {
  62. name: "animals"
  63. }
  64. }
  65. let expected = {
  66. id: 0,
  67. readOnlyDog: "string"
  68. }
  69. expect(sampleFromSchema(definition, { includeReadOnly: true })).toEqual(expected)
  70. })
  71. it("returns object without deprecated fields for parameter", function () {
  72. let definition = {
  73. type: "object",
  74. properties: {
  75. id: {
  76. type: "integer"
  77. },
  78. deprecatedProperty: {
  79. deprecated: true,
  80. type: "string"
  81. }
  82. },
  83. xml: {
  84. name: "animals"
  85. }
  86. }
  87. let expected = {
  88. id: 0
  89. }
  90. expect(sampleFromSchema(definition)).toEqual(expected)
  91. })
  92. it("returns object without writeonly fields for parameter", function () {
  93. let definition = {
  94. type: "object",
  95. properties: {
  96. id: {
  97. type: "integer"
  98. },
  99. writeOnlyDog: {
  100. writeOnly: true,
  101. type: "string"
  102. }
  103. },
  104. xml: {
  105. name: "animals"
  106. }
  107. }
  108. let expected = {
  109. id: 0
  110. }
  111. expect(sampleFromSchema(definition)).toEqual(expected)
  112. })
  113. it("returns object with writeonly fields for parameter, with includeWriteOnly", function () {
  114. let definition = {
  115. type: "object",
  116. properties: {
  117. id: {
  118. type: "integer"
  119. },
  120. writeOnlyDog: {
  121. writeOnly: true,
  122. type: "string"
  123. }
  124. },
  125. xml: {
  126. name: "animals"
  127. }
  128. }
  129. let expected = {
  130. id: 0,
  131. writeOnlyDog: "string"
  132. }
  133. expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
  134. })
  135. it("returns object without any $$ref fields at the root schema level", function () {
  136. let definition = {
  137. type: "object",
  138. properties: {
  139. message: {
  140. type: "string"
  141. }
  142. },
  143. example: {
  144. value: {
  145. message: "Hello, World!"
  146. },
  147. $$ref: "#/components/examples/WelcomeExample"
  148. },
  149. $$ref: "#/components/schemas/Welcome"
  150. }
  151. let expected = {
  152. "value": {
  153. "message": "Hello, World!"
  154. }
  155. }
  156. expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
  157. })
  158. it("returns object without any $$ref fields at nested schema levels", function () {
  159. let definition = {
  160. type: "object",
  161. properties: {
  162. message: {
  163. type: "string"
  164. }
  165. },
  166. example: {
  167. a: {
  168. value: {
  169. message: "Hello, World!"
  170. },
  171. $$ref: "#/components/examples/WelcomeExample"
  172. }
  173. },
  174. $$ref: "#/components/schemas/Welcome"
  175. }
  176. let expected = {
  177. a: {
  178. "value": {
  179. "message": "Hello, World!"
  180. }
  181. }
  182. }
  183. expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
  184. })
  185. it("returns object with any $$ref fields that appear to be user-created", function () {
  186. let definition = {
  187. type: "object",
  188. properties: {
  189. message: {
  190. type: "string"
  191. }
  192. },
  193. example: {
  194. $$ref: {
  195. value: {
  196. message: "Hello, World!"
  197. },
  198. $$ref: "#/components/examples/WelcomeExample"
  199. }
  200. },
  201. $$ref: "#/components/schemas/Welcome"
  202. }
  203. let expected = {
  204. $$ref: {
  205. "value": {
  206. "message": "Hello, World!"
  207. }
  208. }
  209. }
  210. expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
  211. })
  212. it("returns example value for date-time property", () => {
  213. let definition = {
  214. type: "string",
  215. format: "date-time"
  216. }
  217. // 0-20 chops off milliseconds
  218. // necessary because test latency can cause failures
  219. // it would be better to mock Date globally and expect a string - KS 11/18
  220. let expected = new Date().toISOString().substring(0, 20)
  221. expect(sampleFromSchema(definition)).toContain(expected)
  222. })
  223. it("returns example value for date property", () => {
  224. let definition = {
  225. type: "string",
  226. format: "date"
  227. }
  228. let expected = new Date().toISOString().substring(0, 10)
  229. expect(sampleFromSchema(definition)).toEqual(expected)
  230. })
  231. it("returns a UUID for a string with format=uuid", () => {
  232. let definition = {
  233. type: "string",
  234. format: "uuid"
  235. }
  236. let expected = "3fa85f64-5717-4562-b3fc-2c963f66afa6"
  237. expect(sampleFromSchema(definition)).toEqual(expected)
  238. })
  239. it("returns a hostname for a string with format=hostname", () => {
  240. let definition = {
  241. type: "string",
  242. format: "hostname"
  243. }
  244. let expected = "example.com"
  245. expect(sampleFromSchema(definition)).toEqual(expected)
  246. })
  247. it("returns an IPv4 address for a string with format=ipv4", () => {
  248. let definition = {
  249. type: "string",
  250. format: "ipv4"
  251. }
  252. let expected = "198.51.100.42"
  253. expect(sampleFromSchema(definition)).toEqual(expected)
  254. })
  255. it("returns an IPv6 address for a string with format=ipv6", () => {
  256. let definition = {
  257. type: "string",
  258. format: "ipv6"
  259. }
  260. let expected = "2001:0db8:5b96:0000:0000:426f:8e17:642a"
  261. expect(sampleFromSchema(definition)).toEqual(expected)
  262. })
  263. describe("for array type", () => {
  264. it("returns array with sample of array type", () => {
  265. let definition = {
  266. type: "array",
  267. items: {
  268. type: "integer"
  269. }
  270. }
  271. let expected = [ 0 ]
  272. expect(sampleFromSchema(definition)).toEqual(expected)
  273. })
  274. it("returns string for example for array that has example of type string", () => {
  275. let definition = {
  276. type: "array",
  277. items: {
  278. type: "string"
  279. },
  280. example: "dog"
  281. }
  282. let expected = "dog"
  283. expect(sampleFromSchema(definition)).toEqual(expected)
  284. })
  285. it("returns array of examples for array that has examples", () => {
  286. let definition = {
  287. type: "array",
  288. items: {
  289. type: "string",
  290. },
  291. example: [ "dog", "cat" ]
  292. }
  293. let expected = [ "dog", "cat" ]
  294. expect(sampleFromSchema(definition)).toEqual(expected)
  295. })
  296. it("returns array of samples for oneOf type", () => {
  297. let definition = {
  298. type: "array",
  299. items: {
  300. type: "string",
  301. oneOf: [
  302. {
  303. type: "integer"
  304. }
  305. ]
  306. }
  307. }
  308. let expected = [ 0 ]
  309. expect(sampleFromSchema(definition)).toEqual(expected)
  310. })
  311. it("returns array of samples for oneOf types", () => {
  312. let definition = {
  313. type: "array",
  314. items: {
  315. type: "string",
  316. oneOf: [
  317. {
  318. type: "string"
  319. },
  320. {
  321. type: "integer"
  322. }
  323. ]
  324. }
  325. }
  326. let expected = [ "string", 0 ]
  327. expect(sampleFromSchema(definition)).toEqual(expected)
  328. })
  329. it("returns array of samples for oneOf examples", () => {
  330. let definition = {
  331. type: "array",
  332. items: {
  333. type: "string",
  334. oneOf: [
  335. {
  336. type: "string",
  337. example: "dog"
  338. },
  339. {
  340. type: "integer",
  341. example: 1
  342. }
  343. ]
  344. }
  345. }
  346. let expected = [ "dog", 1 ]
  347. expect(sampleFromSchema(definition)).toEqual(expected)
  348. })
  349. it("returns array of samples for anyOf type", () => {
  350. let definition = {
  351. type: "array",
  352. items: {
  353. type: "string",
  354. anyOf: [
  355. {
  356. type: "integer"
  357. }
  358. ]
  359. }
  360. }
  361. let expected = [ 0 ]
  362. expect(sampleFromSchema(definition)).toEqual(expected)
  363. })
  364. it("returns array of samples for anyOf types", () => {
  365. let definition = {
  366. type: "array",
  367. items: {
  368. type: "string",
  369. anyOf: [
  370. {
  371. type: "string"
  372. },
  373. {
  374. type: "integer"
  375. }
  376. ]
  377. }
  378. }
  379. let expected = [ "string", 0 ]
  380. expect(sampleFromSchema(definition)).toEqual(expected)
  381. })
  382. it("returns array of samples for anyOf examples", () => {
  383. let definition = {
  384. type: "array",
  385. items: {
  386. type: "string",
  387. anyOf: [
  388. {
  389. type: "string",
  390. example: "dog"
  391. },
  392. {
  393. type: "integer",
  394. example: 1
  395. }
  396. ]
  397. }
  398. }
  399. let expected = [ "dog", 1 ]
  400. expect(sampleFromSchema(definition)).toEqual(expected)
  401. })
  402. it("returns null for a null example", () => {
  403. let definition = {
  404. "type": "object",
  405. "properties": {
  406. "foo": {
  407. "type": "string",
  408. "nullable": true,
  409. "example": null
  410. }
  411. }
  412. }
  413. let expected = {
  414. foo: null
  415. }
  416. expect(sampleFromSchema(definition)).toEqual(expected)
  417. })
  418. it("returns null for a null object-level example", () => {
  419. let definition = {
  420. "type": "object",
  421. "properties": {
  422. "foo": {
  423. "type": "string",
  424. "nullable": true
  425. }
  426. },
  427. "example": {
  428. "foo": null
  429. }
  430. }
  431. let expected = {
  432. foo: null
  433. }
  434. expect(sampleFromSchema(definition)).toEqual(expected)
  435. })
  436. })
  437. it("should use overrideExample when defined", () => {
  438. const definition = {
  439. type: "object",
  440. properties: {
  441. foo: {
  442. type: "string"
  443. }
  444. },
  445. example: {
  446. foo: null
  447. }
  448. }
  449. const expected = {
  450. foo: "override"
  451. }
  452. expect(sampleFromSchema(definition, {}, expected)).toEqual(expected)
  453. })
  454. })
  455. describe("createXMLExample", function () {
  456. let sut = createXMLExample
  457. describe("simple types with xml property", function () {
  458. it("returns tag <newtagname>string</newtagname> when passing type string and xml:{name: \"newtagname\"}", function () {
  459. let definition = {
  460. type: "string",
  461. xml: {
  462. name: "newtagname"
  463. }
  464. }
  465. expect(sut(definition)).toEqual("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<newtagname>string</newtagname>")
  466. })
  467. it("returns tag <test:newtagname>string</test:newtagname> when passing type string and xml:{name: \"newtagname\", prefix:\"test\"}", function () {
  468. let definition = {
  469. type: "string",
  470. xml: {
  471. name: "newtagname",
  472. prefix: "test"
  473. }
  474. }
  475. expect(sut(definition)).toEqual("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test:newtagname>string</test:newtagname>")
  476. })
  477. it("returns tag <test:tagname xmlns:sample=\"http://swagger.io/schema/sample\">string</test:tagname> when passing type string and xml:{\"namespace\": \"http://swagger.io/schema/sample\", \"prefix\": \"sample\"}", function () {
  478. let definition = {
  479. type: "string",
  480. xml: {
  481. namespace: "http://swagger.io/schema/sample",
  482. prefix: "sample",
  483. name: "name"
  484. }
  485. }
  486. expect(sut(definition)).toEqual("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<sample:name xmlns:sample=\"http://swagger.io/schema/sample\">string</sample:name>")
  487. })
  488. it("returns tag <test:tagname >string</test:tagname> when passing type string and xml:{\"namespace\": \"http://swagger.io/schema/sample\"}", function () {
  489. let definition = {
  490. type: "string",
  491. xml: {
  492. namespace: "http://swagger.io/schema/sample",
  493. name: "name"
  494. }
  495. }
  496. expect(sut(definition)).toEqual("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<name xmlns=\"http://swagger.io/schema/sample\">string</name>")
  497. })
  498. it("returns tag <newtagname>test</newtagname> when passing default value", function () {
  499. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<newtagname>test</newtagname>"
  500. let definition = {
  501. type: "string",
  502. "default": "test",
  503. xml: {
  504. name: "newtagname"
  505. }
  506. }
  507. expect(sut(definition)).toEqual(expected)
  508. })
  509. it("returns default value when enum provided", function () {
  510. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<newtagname>one</newtagname>"
  511. let definition = {
  512. type: "string",
  513. "default": "one",
  514. "enum": ["two", "one"],
  515. xml: {
  516. name: "newtagname"
  517. }
  518. }
  519. expect(sut(definition)).toEqual(expected)
  520. })
  521. it("returns example value when provided", function () {
  522. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<newtagname>two</newtagname>"
  523. let definition = {
  524. type: "string",
  525. "default": "one",
  526. "example": "two",
  527. "enum": ["two", "one"],
  528. xml: {
  529. name: "newtagname"
  530. }
  531. }
  532. expect(sut(definition)).toEqual(expected)
  533. })
  534. it("sets first enum if provided", function () {
  535. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<newtagname>one</newtagname>"
  536. let definition = {
  537. type: "string",
  538. "enum": ["one", "two"],
  539. xml: {
  540. name: "newtagname"
  541. }
  542. }
  543. expect(sut(definition)).toEqual(expected)
  544. })
  545. })
  546. describe("array", function () {
  547. it("returns tag <tagname>string</tagname> when passing string items", function () {
  548. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tagname>string</tagname>"
  549. let definition = {
  550. type: "array",
  551. items: {
  552. type: "string"
  553. },
  554. xml: {
  555. name: "tagname"
  556. }
  557. }
  558. expect(sut(definition)).toEqual(expected)
  559. })
  560. it("returns tag <animal>string</animal> when passing string items with name", function () {
  561. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animal>string</animal>"
  562. let definition = {
  563. type: "array",
  564. items: {
  565. type: "string",
  566. xml: {
  567. name: "animal"
  568. }
  569. },
  570. xml: {
  571. name: "animals"
  572. }
  573. }
  574. expect(sut(definition)).toEqual(expected)
  575. })
  576. it("returns tag <animals><animal>string</animal></animals> when passing string items with name", function () {
  577. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<animal>string</animal>\n</animals>"
  578. let definition = {
  579. type: "array",
  580. items: {
  581. type: "string",
  582. xml: {
  583. name: "animal"
  584. }
  585. },
  586. xml: {
  587. wrapped: true,
  588. name: "animals"
  589. }
  590. }
  591. expect(sut(definition)).toEqual(expected)
  592. })
  593. it("return correct nested wrapped array", function () {
  594. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens>\n\t<dog>string</dog>\n</aliens>"
  595. let definition = {
  596. type: "array",
  597. items: {
  598. type: "array",
  599. items: {
  600. type: "string"
  601. },
  602. xml: {
  603. name: "dog"
  604. }
  605. },
  606. xml: {
  607. wrapped: true,
  608. name: "aliens"
  609. }
  610. }
  611. expect(sut(definition)).toEqual(expected)
  612. })
  613. it("return correct nested wrapped array with xml", function () {
  614. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens>\n\t<dogs>\n\t\t<dog>string</dog>\n\t</dogs>\n</aliens>"
  615. let definition = {
  616. type: "array",
  617. items: {
  618. type: "array",
  619. items: {
  620. type: "string",
  621. xml: {
  622. name: "dog"
  623. }
  624. },
  625. xml: {
  626. name: "dogs",
  627. wrapped: true
  628. }
  629. },
  630. xml: {
  631. wrapped: true,
  632. name: "aliens"
  633. }
  634. }
  635. expect(sut(definition)).toEqual(expected)
  636. })
  637. it("adds namespace to array", function () {
  638. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<dog xmlns=\"test\">string</dog>"
  639. let definition = {
  640. type: "array",
  641. items: {
  642. type: "string",
  643. xml: {
  644. name: "dog",
  645. namespace: "test"
  646. }
  647. },
  648. xml: {
  649. name: "aliens",
  650. namespace: "test_new"
  651. }
  652. }
  653. expect(sut(definition)).toEqual(expected)
  654. })
  655. it("adds prefix to array", function () {
  656. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test:dog>string</test:dog>"
  657. let definition = {
  658. type: "array",
  659. items: {
  660. type: "string",
  661. xml: {
  662. name: "dog",
  663. prefix: "test"
  664. }
  665. },
  666. xml: {
  667. name: "aliens",
  668. prefix: "test_new"
  669. }
  670. }
  671. expect(sut(definition)).toEqual(expected)
  672. })
  673. it("adds prefix to array with no xml in items", function () {
  674. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test:dog>string</test:dog>"
  675. let definition = {
  676. type: "array",
  677. items: {
  678. type: "string"
  679. },
  680. xml: {
  681. name: "dog",
  682. prefix: "test"
  683. }
  684. }
  685. expect(sut(definition)).toEqual(expected)
  686. })
  687. it("adds namespace to array with no xml in items", function () {
  688. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<dog xmlns=\"test\">string</dog>"
  689. let definition = {
  690. type: "array",
  691. items: {
  692. type: "string"
  693. },
  694. xml: {
  695. name: "dog",
  696. namespace: "test"
  697. }
  698. }
  699. expect(sut(definition)).toEqual(expected)
  700. })
  701. it("adds namespace to array with wrapped", function () {
  702. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens xmlns=\"test\">\n\t<dog>string</dog>\n</aliens>"
  703. let definition = {
  704. type: "array",
  705. items: {
  706. type: "string",
  707. xml: {
  708. name: "dog"
  709. }
  710. },
  711. xml: {
  712. wrapped: true,
  713. name: "aliens",
  714. namespace: "test"
  715. }
  716. }
  717. expect(sut(definition)).toEqual(expected)
  718. })
  719. it("adds prefix to array with wrapped", function () {
  720. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test:aliens>\n\t<dog>string</dog>\n</test:aliens>"
  721. let definition = {
  722. type: "array",
  723. items: {
  724. type: "string",
  725. xml: {
  726. name: "dog"
  727. }
  728. },
  729. xml: {
  730. wrapped: true,
  731. name: "aliens",
  732. prefix: "test"
  733. }
  734. }
  735. expect(sut(definition)).toEqual(expected)
  736. })
  737. it("returns wrapped array when type is not passed", function () {
  738. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<animal>string</animal>\n</animals>"
  739. let definition = {
  740. items: {
  741. type: "string",
  742. xml: {
  743. name: "animal"
  744. }
  745. },
  746. xml: {
  747. wrapped: true,
  748. name: "animals"
  749. }
  750. }
  751. expect(sut(definition)).toEqual(expected)
  752. })
  753. it("returns array with default values", function () {
  754. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animal>one</animal>\n<animal>two</animal>"
  755. let definition = {
  756. items: {
  757. type: "string",
  758. xml: {
  759. name: "animal"
  760. }
  761. },
  762. "default": ["one", "two"],
  763. xml: {
  764. name: "animals"
  765. }
  766. }
  767. expect(sut(definition)).toEqual(expected)
  768. })
  769. it("returns array with default values with wrapped=true", function () {
  770. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<animal>one</animal>\n\t<animal>two</animal>\n</animals>"
  771. let definition = {
  772. items: {
  773. type: "string",
  774. xml: {
  775. name: "animal"
  776. }
  777. },
  778. "default": ["one", "two"],
  779. xml: {
  780. wrapped: true,
  781. name: "animals"
  782. }
  783. }
  784. expect(sut(definition)).toEqual(expected)
  785. })
  786. it("returns array with default values", function () {
  787. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animal>one</animal>"
  788. let definition = {
  789. items: {
  790. type: "string",
  791. "enum": ["one", "two"],
  792. xml: {
  793. name: "animal"
  794. }
  795. },
  796. xml: {
  797. name: "animals"
  798. }
  799. }
  800. expect(sut(definition)).toEqual(expected)
  801. })
  802. it("returns array with default values with wrapped=true", function () {
  803. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<animal>1</animal>\n\t<animal>2</animal>\n</animals>"
  804. let definition = {
  805. items: {
  806. "enum": ["one", "two"],
  807. type: "string",
  808. xml: {
  809. name: "animal"
  810. }
  811. },
  812. "default": ["1", "2"],
  813. xml: {
  814. wrapped: true,
  815. name: "animals"
  816. }
  817. }
  818. expect(sut(definition)).toEqual(expected)
  819. })
  820. it("returns array with example values with ", function () {
  821. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<animal>1</animal>\n\t<animal>2</animal>\n</animals>"
  822. let definition = {
  823. type: "object",
  824. properties: {
  825. "animal": {
  826. "type": "array",
  827. "items": {
  828. "type": "string"
  829. },
  830. "example": [
  831. "1",
  832. "2"
  833. ]
  834. }
  835. },
  836. xml: {
  837. name: "animals"
  838. }
  839. }
  840. expect(sut(definition)).toEqual(expected)
  841. })
  842. it("returns array with example values with wrapped=true", function () {
  843. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<animal>1</animal>\n\t<animal>2</animal>\n</animals>"
  844. let definition = {
  845. type: "array",
  846. items: {
  847. type: "string",
  848. xml: {
  849. name: "animal"
  850. }
  851. },
  852. "example": [ "1", "2" ],
  853. xml: {
  854. wrapped: true,
  855. name: "animals"
  856. }
  857. }
  858. expect(sut(definition)).toEqual(expected)
  859. })
  860. it("returns array of objects with example values with wrapped=true", function () {
  861. let expected = `<?xml version="1.0" encoding="UTF-8"?>\n<users>\n\t<user>\n\t\t<id>1</id>\n\t\t<name>Arthur Dent</name>\n\t</user>\n\t<user>\n\t\t<id>2</id>\n\t\t<name>Ford Prefect</name>\n\t</user>\n</users>`
  862. let definition = {
  863. "type": "array",
  864. "items": {
  865. "type": "object",
  866. "properties": {
  867. "id": {
  868. "type": "integer"
  869. },
  870. "name": {
  871. "type": "string"
  872. }
  873. },
  874. "xml": {
  875. "name": "user"
  876. }
  877. },
  878. "xml": {
  879. "name": "users",
  880. "wrapped": true
  881. },
  882. "example": [
  883. {
  884. "id": 1,
  885. "name": "Arthur Dent"
  886. },
  887. {
  888. "id": 2,
  889. "name": "Ford Prefect"
  890. }
  891. ]
  892. }
  893. expect(sut(definition)).toEqual(expected)
  894. })
  895. })
  896. describe("object", function () {
  897. it("returns object with 2 properties", function () {
  898. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens>\n\t<alien>string</alien>\n\t<dog>0</dog>\n</aliens>"
  899. let definition = {
  900. type: "object",
  901. properties: {
  902. alien: {
  903. type: "string"
  904. },
  905. dog: {
  906. type: "integer"
  907. }
  908. },
  909. xml: {
  910. name: "aliens"
  911. }
  912. }
  913. expect(sut(definition)).toEqual(expected)
  914. })
  915. it("returns object with integer property and array property", function () {
  916. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<aliens>string</aliens>\n\t<dog>0</dog>\n</animals>"
  917. let definition = {
  918. type: "object",
  919. properties: {
  920. aliens: {
  921. type: "array",
  922. items: {
  923. type: "string"
  924. }
  925. },
  926. dog: {
  927. type: "integer"
  928. }
  929. },
  930. xml: {
  931. name: "animals"
  932. }
  933. }
  934. expect(sut(definition)).toEqual(expected)
  935. })
  936. it("returns nested objects", function () {
  937. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<aliens>\n\t\t<alien>string</alien>\n\t</aliens>\n\t<dog>string</dog>\n</animals>"
  938. let definition = {
  939. type: "object",
  940. properties: {
  941. aliens: {
  942. type: "object",
  943. properties: {
  944. alien: {
  945. type: "string",
  946. xml: {
  947. name: "alien"
  948. }
  949. }
  950. }
  951. },
  952. dog: {
  953. type: "string"
  954. }
  955. },
  956. xml: {
  957. name: "animals"
  958. }
  959. }
  960. expect(sut(definition)).toEqual(expected)
  961. })
  962. it("returns object with no readonly fields for parameter", function () {
  963. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<id>0</id>\n</animals>"
  964. let definition = {
  965. type: "object",
  966. properties: {
  967. id: {
  968. type: "integer"
  969. },
  970. dog: {
  971. readOnly: true,
  972. type: "string"
  973. }
  974. },
  975. xml: {
  976. name: "animals"
  977. }
  978. }
  979. expect(sut(definition, { includeReadOnly: false })).toEqual(expected)
  980. })
  981. it("returns object with readonly fields for parameter, with includeReadOnly", function () {
  982. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<id>0</id>\n\t<dog>string</dog>\n</animals>"
  983. let definition = {
  984. type: "object",
  985. properties: {
  986. id: {
  987. type: "integer"
  988. },
  989. dog: {
  990. readOnly: true,
  991. type: "string"
  992. }
  993. },
  994. xml: {
  995. name: "animals"
  996. }
  997. }
  998. expect(sut(definition, { includeReadOnly: true })).toEqual(expected)
  999. })
  1000. it("returns object without writeonly fields for parameter", function () {
  1001. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<id>0</id>\n</animals>"
  1002. let definition = {
  1003. type: "object",
  1004. properties: {
  1005. id: {
  1006. type: "integer"
  1007. },
  1008. dog: {
  1009. writeOnly: true,
  1010. type: "string"
  1011. }
  1012. },
  1013. xml: {
  1014. name: "animals"
  1015. }
  1016. }
  1017. expect(sut(definition)).toEqual(expected)
  1018. })
  1019. it("returns object with writeonly fields for parameter, with includeWriteOnly", function () {
  1020. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<id>0</id>\n\t<dog>string</dog>\n</animals>"
  1021. let definition = {
  1022. type: "object",
  1023. properties: {
  1024. id: {
  1025. type: "integer"
  1026. },
  1027. dog: {
  1028. writeOnly: true,
  1029. type: "string"
  1030. }
  1031. },
  1032. xml: {
  1033. name: "animals"
  1034. }
  1035. }
  1036. expect(sut(definition, { includeWriteOnly: true })).toEqual(expected)
  1037. })
  1038. it("returns object with passed property as attribute", function () {
  1039. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals id=\"0\">\n\t<dog>string</dog>\n</animals>"
  1040. let definition = {
  1041. type: "object",
  1042. properties: {
  1043. id: {
  1044. type: "integer",
  1045. xml: {
  1046. attribute: true
  1047. }
  1048. },
  1049. dog: {
  1050. type: "string"
  1051. }
  1052. },
  1053. xml: {
  1054. name: "animals"
  1055. }
  1056. }
  1057. expect(sut(definition)).toEqual(expected)
  1058. })
  1059. it("returns object with passed property as attribute with custom name", function () {
  1060. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals test=\"0\">\n\t<dog>string</dog>\n</animals>"
  1061. let definition = {
  1062. type: "object",
  1063. properties: {
  1064. id: {
  1065. type: "integer",
  1066. xml: {
  1067. attribute: true,
  1068. name: "test"
  1069. }
  1070. },
  1071. dog: {
  1072. type: "string"
  1073. }
  1074. },
  1075. xml: {
  1076. name: "animals"
  1077. }
  1078. }
  1079. expect(sut(definition)).toEqual(expected)
  1080. })
  1081. it("returns object with example values in attribute", function () {
  1082. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user id=\"42\">\n\t<role>admin</role>\n</user>"
  1083. let definition = {
  1084. type: "object",
  1085. properties: {
  1086. id: {
  1087. type: "integer",
  1088. xml: {
  1089. attribute: true
  1090. }
  1091. },
  1092. role:{
  1093. type: "string"
  1094. }
  1095. },
  1096. xml: {
  1097. name: "user"
  1098. },
  1099. example: {
  1100. id: 42,
  1101. role: "admin"
  1102. }
  1103. }
  1104. expect(sut(definition)).toEqual(expected)
  1105. })
  1106. it("returns object with enum values in attribute", function () {
  1107. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user id=\"one\">\n\t<role>string</role>\n</user>"
  1108. let definition = {
  1109. type: "object",
  1110. properties: {
  1111. id: {
  1112. type: "string",
  1113. "enum": ["one", "two"],
  1114. xml: {
  1115. attribute: true
  1116. }
  1117. },
  1118. role:{
  1119. type: "string"
  1120. }
  1121. },
  1122. xml: {
  1123. name: "user"
  1124. }
  1125. }
  1126. expect(sut(definition)).toEqual(expected)
  1127. })
  1128. it("returns object with default values in attribute", function () {
  1129. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user id=\"one\">\n\t<role>string</role>\n</user>"
  1130. let definition = {
  1131. type: "object",
  1132. properties: {
  1133. id: {
  1134. type: "string",
  1135. "default": "one",
  1136. xml: {
  1137. attribute: true
  1138. }
  1139. },
  1140. role:{
  1141. type: "string"
  1142. }
  1143. },
  1144. xml: {
  1145. name: "user"
  1146. }
  1147. }
  1148. expect(sut(definition)).toEqual(expected)
  1149. })
  1150. it("returns object with default values in attribute", function () {
  1151. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user id=\"one\">\n\t<role>string</role>\n</user>"
  1152. let definition = {
  1153. type: "object",
  1154. properties: {
  1155. id: {
  1156. type: "string",
  1157. "example": "one",
  1158. xml: {
  1159. attribute: true
  1160. }
  1161. },
  1162. role:{
  1163. type: "string"
  1164. }
  1165. },
  1166. xml: {
  1167. name: "user"
  1168. }
  1169. }
  1170. expect(sut(definition)).toEqual(expected)
  1171. })
  1172. it("returns object with example value", function () {
  1173. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>\n\t<id>42</id>\n\t<role>admin</role>\n</user>"
  1174. let definition = {
  1175. type: "object",
  1176. properties: {
  1177. id: {
  1178. type: "integer"
  1179. },
  1180. role:{
  1181. type: "string"
  1182. }
  1183. },
  1184. xml: {
  1185. name: "user"
  1186. },
  1187. example: {
  1188. id: 42,
  1189. role: "admin"
  1190. }
  1191. }
  1192. expect(sut(definition)).toEqual(expected)
  1193. })
  1194. it("returns object with additional props", function () {
  1195. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<dog>string</dog>\n\t<additionalProp1>string</additionalProp1>\n\t<additionalProp2>string</additionalProp2>\n\t<additionalProp3>string</additionalProp3>\n</animals>"
  1196. let definition = {
  1197. type: "object",
  1198. properties: {
  1199. dog: {
  1200. type: "string"
  1201. }
  1202. },
  1203. additionalProperties: {
  1204. type: "string"
  1205. },
  1206. xml: {
  1207. name: "animals"
  1208. }
  1209. }
  1210. expect(sut(definition)).toEqual(expected)
  1211. })
  1212. it("returns object with additional props =true", function () {
  1213. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<dog>string</dog>\n\t<additionalProp>Anything can be here</additionalProp>\n</animals>"
  1214. let definition = {
  1215. type: "object",
  1216. properties: {
  1217. dog: {
  1218. type: "string"
  1219. }
  1220. },
  1221. additionalProperties: true,
  1222. xml: {
  1223. name: "animals"
  1224. }
  1225. }
  1226. expect(sut(definition)).toEqual(expected)
  1227. })
  1228. it("returns object with 2 properties with no type passed but properties", function () {
  1229. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens>\n\t<alien>string</alien>\n\t<dog>0</dog>\n</aliens>"
  1230. let definition = {
  1231. properties: {
  1232. alien: {
  1233. type: "string"
  1234. },
  1235. dog: {
  1236. type: "integer"
  1237. }
  1238. },
  1239. xml: {
  1240. name: "aliens"
  1241. }
  1242. }
  1243. expect(sut(definition)).toEqual(expected)
  1244. })
  1245. it("returns object with additional props with no type passed", function () {
  1246. let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<additionalProp1>string</additionalProp1>\n\t<additionalProp2>string</additionalProp2>\n\t<additionalProp3>string</additionalProp3>\n</animals>"
  1247. let definition = {
  1248. additionalProperties: {
  1249. type: "string"
  1250. },
  1251. xml: {
  1252. name: "animals"
  1253. }
  1254. }
  1255. expect(sut(definition)).toEqual(expected)
  1256. })
  1257. it("should use overrideExample when defined", () => {
  1258. const expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bar>\n\t<foo>override</foo>\n</bar>"
  1259. const definition = {
  1260. type: "object",
  1261. properties: {
  1262. foo: {
  1263. type: "string",
  1264. xml: {
  1265. name: "foo"
  1266. }
  1267. }
  1268. },
  1269. example: {
  1270. foo: null
  1271. },
  1272. xml: {
  1273. name: "bar"
  1274. }
  1275. }
  1276. const overrideExample = {
  1277. foo: "override"
  1278. }
  1279. expect(sut(definition, {}, overrideExample)).toEqual(expected)
  1280. })
  1281. })
  1282. })
  1283. describe("memoizedSampleFromSchema", () => {
  1284. it("should sequentially update memoized overrideExample", () => {
  1285. const definition = {
  1286. type: "object",
  1287. properties: {
  1288. foo: {
  1289. type: "string"
  1290. }
  1291. },
  1292. example: {
  1293. foo: null
  1294. }
  1295. }
  1296. const expected = {
  1297. foo: "override"
  1298. }
  1299. expect(memoizedSampleFromSchema(definition, {}, expected)).toEqual(expected)
  1300. const updatedExpected = {
  1301. foo: "cat"
  1302. }
  1303. expect(memoizedSampleFromSchema(definition, {}, updatedExpected)).toEqual(updatedExpected)
  1304. })
  1305. })
  1306. describe("memoizedCreateXMLExample", () => {
  1307. it("should sequentially update memoized overrideExample", () => {
  1308. const expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bar>\n\t<foo>override</foo>\n</bar>"
  1309. const definition = {
  1310. type: "object",
  1311. properties: {
  1312. foo: {
  1313. type: "string",
  1314. xml: {
  1315. name: "foo"
  1316. }
  1317. }
  1318. },
  1319. example: {
  1320. foo: null
  1321. },
  1322. xml: {
  1323. name: "bar"
  1324. }
  1325. }
  1326. const overrideExample = {
  1327. foo: "override"
  1328. }
  1329. expect(memoizedCreateXMLExample(definition, {}, overrideExample)).toEqual(expected)
  1330. const updatedOverrideExample = {
  1331. foo: "cat"
  1332. }
  1333. const updatedExpected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bar>\n\t<foo>cat</foo>\n</bar>"
  1334. expect(memoizedCreateXMLExample(definition, {}, updatedOverrideExample)).toEqual(updatedExpected)
  1335. })
  1336. })