OpenAPISpecWriterTest.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Faker\Factory;
  4. use Illuminate\Support\Arr;
  5. use Knuckles\Camel\Camel;
  6. use Knuckles\Camel\Output\OutputEndpointData;
  7. use Knuckles\Scribe\Tests\BaseUnitTest;
  8. use Knuckles\Scribe\Tools\DocumentationConfig;
  9. use Knuckles\Scribe\Writing\OpenAPISpecWriter;
  10. /**
  11. * See https://swagger.io/specification/
  12. */
  13. class OpenAPISpecWriterTest extends BaseUnitTest
  14. {
  15. protected $config = [
  16. 'title' => 'My Testy Testes API',
  17. 'description' => 'All about testy testes.',
  18. 'base_url' => 'http://api.api.dev',
  19. ];
  20. /** @test */
  21. public function follows_correct_spec_structure()
  22. {
  23. $endpointData1 = $this->createMockEndpointData();
  24. $endpointData2 = $this->createMockEndpointData();
  25. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  26. $results = $this->generate($groups);
  27. $this->assertEquals(OpenAPISpecWriter::SPEC_VERSION, $results['openapi']);
  28. $this->assertEquals($this->config['title'], $results['info']['title']);
  29. $this->assertEquals($this->config['description'], $results['info']['description']);
  30. $this->assertNotEmpty($results['info']['version']);
  31. $this->assertEquals($this->config['base_url'], $results['servers'][0]['url']);
  32. $this->assertIsArray($results['paths']);
  33. $this->assertGreaterThan(0, count($results['paths']));
  34. }
  35. /** @test */
  36. public function adds_endpoints_correctly_as_operations_under_paths()
  37. {
  38. $endpointData1 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['GET']]);
  39. $endpointData2 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['POST']]);
  40. $endpointData3 = $this->createMockEndpointData(['uri' => 'path1/path2']);
  41. $groups = [$this->createGroup([$endpointData1, $endpointData2, $endpointData3])];
  42. $results = $this->generate($groups);
  43. $this->assertIsArray($results['paths']);
  44. $this->assertCount(2, $results['paths']);
  45. $this->assertCount(2, $results['paths']['/path1']);
  46. $this->assertCount(1, $results['paths']['/path1/path2']);
  47. $this->assertArrayHasKey('get', $results['paths']['/path1']);
  48. $this->assertArrayHasKey('post', $results['paths']['/path1']);
  49. $this->assertArrayHasKey(strtolower($endpointData3->httpMethods[0]), $results['paths']['/path1/path2']);
  50. collect([$endpointData1, $endpointData2, $endpointData3])->each(function (OutputEndpointData $endpoint) use ($groups, $results) {
  51. $endpointSpec = $results['paths']['/' . $endpoint->uri][strtolower($endpoint->httpMethods[0])];
  52. $tags = $endpointSpec['tags'];
  53. $containingGroup = Arr::first($groups, function ($group) use ($endpoint) {
  54. return Camel::doesGroupContainEndpoint($group, $endpoint);
  55. });
  56. $this->assertEquals([$containingGroup['name']], $tags);
  57. $this->assertEquals($endpoint->metadata->title, $endpointSpec['summary']);
  58. $this->assertEquals($endpoint->metadata->description, $endpointSpec['description']);
  59. });
  60. }
  61. /** @test */
  62. public function adds_authentication_details_correctly_as_security_info()
  63. {
  64. $endpointData1 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['GET'], 'metadata.authenticated' => true]);
  65. $endpointData2 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['POST'], 'metadata.authenticated' => false]);
  66. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  67. $extraInfo = "When stuck trying to authenticate, have a coffee!";
  68. $config = array_merge($this->config, [
  69. 'auth' => [
  70. 'enabled' => true,
  71. 'in' => 'bearer',
  72. 'extra_info' => $extraInfo,
  73. ],
  74. ]);
  75. $writer = new OpenAPISpecWriter(new DocumentationConfig($config));
  76. $results = $writer->generateSpecContent($groups);
  77. $this->assertCount(1, $results['components']['securitySchemes']);
  78. $this->assertArrayHasKey('default', $results['components']['securitySchemes']);
  79. $this->assertEquals('http', $results['components']['securitySchemes']['default']['type']);
  80. $this->assertEquals('bearer', $results['components']['securitySchemes']['default']['scheme']);
  81. $this->assertEquals($extraInfo, $results['components']['securitySchemes']['default']['description']);
  82. $this->assertCount(1, $results['security']);
  83. $this->assertCount(1, $results['security'][0]);
  84. $this->assertArrayHasKey('default', $results['security'][0]);
  85. $this->assertArrayNotHasKey('security', $results['paths']['/path1']['get']);
  86. $this->assertArrayHasKey('security', $results['paths']['/path1']['post']);
  87. $this->assertCount(0, $results['paths']['/path1']['post']['security']);
  88. // Next try: auth with a query parameter
  89. $config = array_merge($this->config, [
  90. 'auth' => [
  91. 'enabled' => true,
  92. 'in' => 'query',
  93. 'name' => 'token',
  94. 'extra_info' => $extraInfo,
  95. ],
  96. ]);
  97. $writer = new OpenAPISpecWriter(new DocumentationConfig($config));
  98. $results = $writer->generateSpecContent($groups);
  99. $this->assertCount(1, $results['components']['securitySchemes']);
  100. $this->assertArrayHasKey('default', $results['components']['securitySchemes']);
  101. $this->assertEquals('apiKey', $results['components']['securitySchemes']['default']['type']);
  102. $this->assertEquals($extraInfo, $results['components']['securitySchemes']['default']['description']);
  103. $this->assertEquals($config['auth']['name'], $results['components']['securitySchemes']['default']['name']);
  104. $this->assertEquals('query', $results['components']['securitySchemes']['default']['in']);
  105. $this->assertCount(1, $results['security']);
  106. $this->assertCount(1, $results['security'][0]);
  107. $this->assertArrayHasKey('default', $results['security'][0]);
  108. $this->assertArrayNotHasKey('security', $results['paths']['/path1']['get']);
  109. $this->assertArrayHasKey('security', $results['paths']['/path1']['post']);
  110. $this->assertCount(0, $results['paths']['/path1']['post']['security']);
  111. }
  112. /** @test */
  113. public function adds_url_parameters_correctly_as_parameters_on_path_item_object()
  114. {
  115. $endpointData1 = $this->createMockEndpointData([
  116. 'httpMethods' => ['POST'],
  117. 'uri' => 'path1/{param}/{optionalParam?}',
  118. 'urlParameters.param' => [
  119. 'description' => 'Something',
  120. 'required' => true,
  121. 'example' => 56,
  122. 'type' => 'integer',
  123. 'name' => 'param',
  124. ],
  125. 'urlParameters.optionalParam' => [
  126. 'description' => 'Another',
  127. 'required' => false,
  128. 'example' => '69',
  129. 'type' => 'string',
  130. 'name' => 'optionalParam',
  131. ],
  132. ]);
  133. $endpointData2 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['POST']]);
  134. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  135. $results = $this->generate($groups);
  136. $this->assertArrayNotHasKey('parameters', $results['paths']['/path1']);
  137. $this->assertCount(2, $results['paths']['/path1/{param}/{optionalParam}']['parameters']);
  138. $this->assertEquals([
  139. 'in' => 'path',
  140. 'required' => true,
  141. 'name' => 'param',
  142. 'description' => 'Something',
  143. 'example' => 56,
  144. 'schema' => ['type' => 'integer'],
  145. ], $results['paths']['/path1/{param}/{optionalParam}']['parameters'][0]);
  146. $this->assertEquals([
  147. 'in' => 'path',
  148. 'required' => true,
  149. 'name' => 'optionalParam',
  150. 'description' => 'Optional parameter. Another',
  151. 'examples' => [
  152. 'omitted' => ['summary' => 'When the value is omitted', 'value' => ''],
  153. 'present' => [
  154. 'summary' => 'When the value is present', 'value' => '69'],
  155. ],
  156. 'schema' => ['type' => 'string'],
  157. ], $results['paths']['/path1/{param}/{optionalParam}']['parameters'][1]);
  158. }
  159. /** @test */
  160. public function adds_headers_correctly_as_parameters_on_operation_object()
  161. {
  162. $endpointData1 = $this->createMockEndpointData(['httpMethods' => ['POST'], 'uri' => 'path1', 'headers.Extra-Header' => 'Some-example']);
  163. $endpointData2 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['GET'], 'headers' => []]);
  164. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  165. $results = $this->generate($groups);
  166. $this->assertEquals([], $results['paths']['/path1']['get']['parameters']);
  167. $this->assertCount(1, $results['paths']['/path1']['post']['parameters']);
  168. $this->assertEquals([
  169. 'in' => 'header',
  170. 'name' => 'Extra-Header',
  171. 'description' => '',
  172. 'example' => 'Some-example',
  173. 'schema' => ['type' => 'string'],
  174. ], $results['paths']['/path1']['post']['parameters'][0]);
  175. }
  176. /** @test */
  177. public function adds_query_parameters_correctly_as_parameters_on_operation_object()
  178. {
  179. $endpointData1 = $this->createMockEndpointData([
  180. 'httpMethods' => ['GET'],
  181. 'uri' => '/path1',
  182. 'headers' => [], // Emptying headers so it doesn't interfere with parameters object
  183. 'queryParameters' => [
  184. 'param' => [
  185. 'description' => 'A query param',
  186. 'required' => false,
  187. 'example' => 'hahoho',
  188. 'type' => 'string',
  189. 'name' => 'param',
  190. 'nullable' => false
  191. ],
  192. ],
  193. ]);
  194. $endpointData2 = $this->createMockEndpointData(['headers' => [], 'httpMethods' => ['POST'], 'uri' => '/path1',]);
  195. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  196. $results = $this->generate($groups);
  197. $this->assertEquals([], $results['paths']['/path1']['post']['parameters']);
  198. $this->assertArrayHasKey('parameters', $results['paths']['/path1']['get']);
  199. $this->assertCount(1, $results['paths']['/path1']['get']['parameters']);
  200. $this->assertEquals([
  201. 'in' => 'query',
  202. 'required' => false,
  203. 'name' => 'param',
  204. 'description' => 'A query param',
  205. 'example' => 'hahoho',
  206. 'schema' => [
  207. 'type' => 'string',
  208. 'description' => 'A query param',
  209. 'example' => 'hahoho',
  210. 'nullable' => false
  211. ],
  212. ], $results['paths']['/path1']['get']['parameters'][0]);
  213. }
  214. /** @test */
  215. public function adds_body_parameters_correctly_as_requestBody_on_operation_object()
  216. {
  217. $endpointData1 = $this->createMockEndpointData([
  218. 'httpMethods' => ['POST'],
  219. 'uri' => '/path1',
  220. 'bodyParameters' => [
  221. 'stringParam' => [
  222. 'name' => 'stringParam',
  223. 'description' => 'String param',
  224. 'required' => false,
  225. 'example' => 'hahoho',
  226. 'type' => 'string',
  227. 'nullable' => false,
  228. ],
  229. 'integerParam' => [
  230. 'name' => 'integerParam',
  231. 'description' => 'Integer param',
  232. 'required' => true,
  233. 'example' => 99,
  234. 'type' => 'integer',
  235. 'nullable' => false,
  236. ],
  237. 'booleanParam' => [
  238. 'name' => 'booleanParam',
  239. 'description' => 'Boolean param',
  240. 'required' => true,
  241. 'example' => false,
  242. 'type' => 'boolean',
  243. 'nullable' => false,
  244. ],
  245. 'objectParam' => [
  246. 'name' => 'objectParam',
  247. 'description' => 'Object param',
  248. 'required' => false,
  249. 'example' => [],
  250. 'type' => 'object',
  251. 'nullable' => false,
  252. ],
  253. 'objectParam.field' => [
  254. 'name' => 'objectParam.field',
  255. 'description' => 'Object param field',
  256. 'required' => false,
  257. 'example' => 119.0,
  258. 'type' => 'number',
  259. 'nullable' => false,
  260. ],
  261. ],
  262. ]);
  263. $endpointData2 = $this->createMockEndpointData(['httpMethods' => ['GET'], 'uri' => '/path1']);
  264. $endpointData3 = $this->createMockEndpointData([
  265. 'httpMethods' => ['PUT'],
  266. 'uri' => '/path2',
  267. 'bodyParameters' => [
  268. 'fileParam' => [
  269. 'name' => 'fileParam',
  270. 'description' => 'File param',
  271. 'required' => false,
  272. 'example' => null,
  273. 'type' => 'file',
  274. ],
  275. 'numberArrayParam' => [
  276. 'name' => 'numberArrayParam',
  277. 'description' => 'Number array param',
  278. 'required' => false,
  279. 'example' => [186.9],
  280. 'type' => 'number[]',
  281. ],
  282. 'objectArrayParam' => [
  283. 'name' => 'objectArrayParam',
  284. 'description' => 'Object array param',
  285. 'required' => false,
  286. 'example' => [[]],
  287. 'type' => 'object[]',
  288. ],
  289. 'objectArrayParam[].field1' => [
  290. 'name' => 'objectArrayParam[].field1',
  291. 'description' => 'Object array param first field',
  292. 'required' => true,
  293. 'example' => ["hello"],
  294. 'type' => 'string[]',
  295. ],
  296. 'objectArrayParam[].field2' => [
  297. 'name' => 'objectArrayParam[].field2',
  298. 'description' => '',
  299. 'required' => false,
  300. 'example' => "hi",
  301. 'type' => 'string',
  302. ],
  303. ],
  304. ]);
  305. $groups = [$this->createGroup([$endpointData1, $endpointData2, $endpointData3])];
  306. $results = $this->generate($groups);
  307. $this->assertArrayNotHasKey('requestBody', $results['paths']['/path1']['get']);
  308. $this->assertArrayHasKey('requestBody', $results['paths']['/path1']['post']);
  309. $this->assertEquals([
  310. 'required' => true,
  311. 'content' => [
  312. 'application/json' => [
  313. 'schema' => [
  314. 'type' => 'object',
  315. 'properties' => [
  316. 'stringParam' => [
  317. 'description' => 'String param',
  318. 'example' => 'hahoho',
  319. 'type' => 'string',
  320. 'nullable' => false,
  321. ],
  322. 'booleanParam' => [
  323. 'description' => 'Boolean param',
  324. 'example' => false,
  325. 'type' => 'boolean',
  326. 'nullable' => false,
  327. ],
  328. 'integerParam' => [
  329. 'description' => 'Integer param',
  330. 'example' => 99,
  331. 'type' => 'integer',
  332. 'nullable' => false,
  333. ],
  334. 'objectParam' => [
  335. 'description' => 'Object param',
  336. 'example' => [],
  337. 'type' => 'object',
  338. 'nullable' => false,
  339. 'properties' => [
  340. 'field' => [
  341. 'description' => 'Object param field',
  342. 'example' => 119.0,
  343. 'type' => 'number',
  344. 'nullable' => false,
  345. ],
  346. ],
  347. ],
  348. ],
  349. 'required' => [
  350. 'integerParam',
  351. 'booleanParam',
  352. ],
  353. ],
  354. ],
  355. ],
  356. ], $results['paths']['/path1']['post']['requestBody']);
  357. $this->assertEquals([
  358. 'required' => false,
  359. 'content' => [
  360. 'multipart/form-data' => [
  361. 'schema' => [
  362. 'type' => 'object',
  363. 'properties' => [
  364. 'fileParam' => [
  365. 'description' => 'File param',
  366. 'type' => 'string',
  367. 'format' => 'binary',
  368. 'nullable' => false,
  369. ],
  370. 'numberArrayParam' => [
  371. 'description' => 'Number array param',
  372. 'example' => [186.9],
  373. 'type' => 'array',
  374. 'items' => [
  375. 'type' => 'number',
  376. ],
  377. ],
  378. 'objectArrayParam' => [
  379. 'description' => 'Object array param',
  380. 'example' => [[]],
  381. 'type' => 'array',
  382. 'items' => [
  383. 'type' => 'object',
  384. 'required' => ['field1'],
  385. 'properties' => [
  386. 'field1' => [
  387. 'type' => 'array',
  388. 'items' => [
  389. 'type' => 'string',
  390. ],
  391. 'description' => 'Object array param first field',
  392. 'example' => ["hello"],
  393. ],
  394. 'field2' => [
  395. 'type' => 'string',
  396. 'description' => '',
  397. 'example' => "hi",
  398. 'nullable' => false,
  399. ],
  400. ],
  401. ],
  402. ],
  403. ],
  404. ],
  405. ],
  406. ],
  407. ], $results['paths']['/path2']['put']['requestBody']);
  408. }
  409. /** @test */
  410. public function adds_responses_correctly_as_responses_on_operation_object()
  411. {
  412. $endpointData1 = $this->createMockEndpointData([
  413. 'httpMethods' => ['POST'],
  414. 'uri' => '/path1',
  415. 'responses' => [
  416. [
  417. 'status' => 204,
  418. 'description' => 'Successfully updated.',
  419. 'content' => '{"this": "should be ignored"}',
  420. ],
  421. [
  422. 'status' => 201,
  423. 'description' => '',
  424. 'content' => '{"this": "shouldn\'t be ignored", "and this": "too", "also this": "too", "sub level 0": { "sub level 1 key 1": "sl0_sl1k1", "sub level 1 key 2": [ { "sub level 2 key 1": "sl0_sl1k2_sl2k1", "sub level 2 key 2": { "sub level 3 key 1": "sl0_sl1k2_sl2k2_sl3k1" } } ], "sub level 1 key 3": { "sub level 2 key 1": "sl0_sl1k3_sl2k2", "sub level 2 key 2": { "sub level 3 key 1": "sl0_sl1k3_sl2k2_sl3k1", "sub level 3 key null": null, "sub level 3 key integer": 99 }, "sub level 2 key 3 required" : "sl0_sl1k3_sl2k3" } } }',
  425. ],
  426. ],
  427. 'responseFields' => [
  428. 'and this' => [
  429. 'name' => 'and this',
  430. 'type' => 'string',
  431. 'description' => 'Parameter description, ha!',
  432. ],
  433. 'also this' => [
  434. 'name' => 'also this',
  435. 'type' => 'string',
  436. 'description' => 'This response parameter is required.',
  437. 'required' => true,
  438. ],
  439. 'sub level 0.sub level 1 key 3.sub level 2 key 1' => [
  440. 'description' => 'This is a description of a nested object',
  441. ],
  442. 'sub level 0.sub level 1 key 3.sub level 2 key 3 required' => [
  443. 'description' => 'This is a description of a required nested object',
  444. 'required' => true,
  445. ],
  446. ],
  447. ]);
  448. $endpointData2 = $this->createMockEndpointData([
  449. 'httpMethods' => ['PUT'],
  450. 'uri' => '/path2',
  451. 'responses' => [
  452. [
  453. 'status' => 200,
  454. 'description' => '',
  455. 'content' => '<<binary>> The cropped image',
  456. ],
  457. ],
  458. ]);
  459. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  460. $results = $this->generate($groups);
  461. $this->assertCount(2, $results['paths']['/path1']['post']['responses']);
  462. $this->assertArraySubset([
  463. '204' => [
  464. 'description' => 'Successfully updated.',
  465. ],
  466. '201' => [
  467. 'content' => [
  468. 'application/json' => [
  469. 'schema' => [
  470. 'type' => 'object',
  471. 'properties' => [
  472. 'this' => [
  473. 'example' => "shouldn't be ignored",
  474. 'type' => 'string',
  475. ],
  476. 'and this' => [
  477. 'description' => 'Parameter description, ha!',
  478. 'example' => "too",
  479. 'type' => 'string',
  480. ],
  481. 'also this' => [
  482. 'description' => 'This response parameter is required.',
  483. 'example' => "too",
  484. 'type' => 'string',
  485. ],
  486. 'sub level 0' => [
  487. 'type' => 'object',
  488. 'properties' => [
  489. 'sub level 1 key 1' => [
  490. 'type' => 'string',
  491. 'example' => 'sl0_sl1k1'
  492. ],
  493. 'sub level 1 key 2' => [
  494. 'type' => 'array',
  495. 'example' => [
  496. [
  497. 'sub level 2 key 1' => 'sl0_sl1k2_sl2k1',
  498. 'sub level 2 key 2' => [
  499. 'sub level 3 key 1' => 'sl0_sl1k2_sl2k2_sl3k1'
  500. ]
  501. ]
  502. ],
  503. 'items' => [
  504. 'type' => 'object'
  505. ]
  506. ],
  507. 'sub level 1 key 3' => [
  508. 'type' => 'object',
  509. 'properties' => [
  510. 'sub level 2 key 1' => [
  511. 'type' => 'string',
  512. 'example' => 'sl0_sl1k3_sl2k2',
  513. 'description' => 'This is a description of a nested object'
  514. ],
  515. 'sub level 2 key 2' => [
  516. 'type' => 'object',
  517. 'properties' => [
  518. 'sub level 3 key 1' => [
  519. 'type' => 'string',
  520. 'example' => 'sl0_sl1k3_sl2k2_sl3k1'
  521. ],
  522. 'sub level 3 key null' => [
  523. 'type' => 'string',
  524. 'example' => null
  525. ],
  526. 'sub level 3 key integer' => [
  527. 'type' => 'integer',
  528. 'example' => 99
  529. ]
  530. ]
  531. ],
  532. 'sub level 2 key 3 required' => [
  533. 'type' => 'string',
  534. 'example' => 'sl0_sl1k3_sl2k3',
  535. 'description' => 'This is a description of a required nested object'
  536. ],
  537. ],
  538. 'required' => [
  539. 'sub level 2 key 3 required'
  540. ]
  541. ]
  542. ]
  543. ]
  544. ],
  545. 'required' => [
  546. 'also this'
  547. ]
  548. ],
  549. ],
  550. ],
  551. ],
  552. ], $results['paths']['/path1']['post']['responses']);
  553. $this->assertCount(1, $results['paths']['/path2']['put']['responses']);
  554. $this->assertEquals([
  555. '200' => [
  556. 'description' => 'The cropped image',
  557. 'content' => [
  558. 'application/octet-stream' => [
  559. 'schema' => [
  560. 'type' => 'string',
  561. 'format' => 'binary',
  562. ],
  563. ],
  564. ],
  565. ],
  566. ], $results['paths']['/path2']['put']['responses']);
  567. }
  568. /** @test */
  569. public function adds_required_fields_on_objects_wrapped_in_array()
  570. {
  571. $endpointData = $this->createMockEndpointData([
  572. 'httpMethods' => ['GEt'],
  573. 'uri' => '/path1',
  574. 'responses' => [
  575. [
  576. 'status' => 200,
  577. 'description' => 'List of entities',
  578. 'content' => '{"data":[{"name":"Resource name","uuid":"UUID","primary":true}]}',
  579. ],
  580. ],
  581. 'responseFields' => [
  582. 'data' => [
  583. 'name' => 'data',
  584. 'type' => 'array',
  585. 'description' => 'Data wrapper',
  586. ],
  587. 'data.name' => [
  588. 'name' => 'Resource name',
  589. 'type' => 'string',
  590. 'description' => 'Name of the resource object',
  591. 'required' => true,
  592. ],
  593. 'data.uuid' => [
  594. 'name' => 'Resource UUID',
  595. 'type' => 'string',
  596. 'description' => 'Unique ID for the resource',
  597. 'required' => true,
  598. ],
  599. 'data.primary' => [
  600. 'name' => 'Is primary',
  601. 'type' => 'bool',
  602. 'description' => 'Is primary resource',
  603. 'required' => true,
  604. ],
  605. ],
  606. ]);
  607. $groups = [$this->createGroup([$endpointData])];
  608. $results = $this->generate($groups);
  609. $this->assertArraySubset([
  610. '200' => [
  611. 'description' => 'List of entities',
  612. 'content' => [
  613. 'application/json' => [
  614. 'schema' => [
  615. 'type' => 'object',
  616. 'properties' => [
  617. 'data' => [
  618. 'type' => 'array',
  619. 'description' => 'Data wrapper',
  620. 'items' => [
  621. 'type' => 'object',
  622. 'properties' => [
  623. 'name' => [
  624. 'type' => 'string',
  625. 'description' => 'Name of the resource object',
  626. ],
  627. 'uuid' => [
  628. 'type' => 'string',
  629. 'description' => 'Unique ID for the resource',
  630. ],
  631. 'primary' => [
  632. 'type' => 'boolean',
  633. 'description' => 'Is primary resource',
  634. ],
  635. ],
  636. ],
  637. 'required' => [
  638. 'name',
  639. 'uuid',
  640. 'primary',
  641. ]
  642. ],
  643. ],
  644. ],
  645. ],
  646. ],
  647. ],
  648. ], $results['paths']['/path1']['get']['responses']);
  649. }
  650. /** @test */
  651. public function adds_multiple_responses_correctly_using_oneOf()
  652. {
  653. $endpointData1 = $this->createMockEndpointData([
  654. 'httpMethods' => ['POST'],
  655. 'uri' => '/path1',
  656. 'responses' => [
  657. [
  658. 'status' => 201,
  659. 'description' => 'This one',
  660. 'content' => '{"this": "one"}',
  661. ],
  662. [
  663. 'status' => 201,
  664. 'description' => 'No, that one.',
  665. 'content' => '{"that": "one"}',
  666. ],
  667. [
  668. 'status' => 200,
  669. 'description' => 'A separate one',
  670. 'content' => '{"the other": "one"}',
  671. ],
  672. ],
  673. ]);
  674. $groups = [$this->createGroup([$endpointData1])];
  675. $results = $this->generate($groups);
  676. $this->assertArraySubset([
  677. '200' => [
  678. 'description' => 'A separate one',
  679. 'content' => [
  680. 'application/json' => [
  681. 'schema' => [
  682. 'type' => 'object',
  683. 'properties' => [
  684. 'the other' => [
  685. 'example' => "one",
  686. 'type' => 'string',
  687. ],
  688. ],
  689. ],
  690. ],
  691. ],
  692. ],
  693. '201' => [
  694. 'description' => '',
  695. 'content' => [
  696. 'application/json' => [
  697. 'schema' => [
  698. 'oneOf' => [
  699. [
  700. 'type' => 'object',
  701. 'description' => 'This one',
  702. 'properties' => [
  703. 'this' => [
  704. 'example' => "one",
  705. 'type' => 'string',
  706. ],
  707. ],
  708. ],
  709. [
  710. 'type' => 'object',
  711. 'description' => 'No, that one.',
  712. 'properties' => [
  713. 'that' => [
  714. 'example' => "one",
  715. 'type' => 'string',
  716. ],
  717. ],
  718. ],
  719. ],
  720. ],
  721. ],
  722. ],
  723. ],
  724. ], $results['paths']['/path1']['post']['responses']);
  725. }
  726. /** @test */
  727. public function adds_more_than_two_answers_correctly_using_oneOf()
  728. {
  729. $endpointData1 = $this->createMockEndpointData([
  730. 'httpMethods' => ['POST'],
  731. 'uri' => '/path1',
  732. 'responses' => [
  733. [
  734. 'status' => 201,
  735. 'description' => 'This one',
  736. 'content' => '{"this": "one"}',
  737. ],
  738. [
  739. 'status' => 201,
  740. 'description' => 'No, that one.',
  741. 'content' => '{"that": "one"}',
  742. ],
  743. [
  744. 'status' => 201,
  745. 'description' => 'No, another one.',
  746. 'content' => '{"another": "one"}',
  747. ],
  748. [
  749. 'status' => 200,
  750. 'description' => 'A separate one',
  751. 'content' => '{"the other": "one"}',
  752. ],
  753. ],
  754. ]);
  755. $groups = [$this->createGroup([$endpointData1])];
  756. $results = $this->generate($groups);
  757. $this->assertArraySubset([
  758. '200' => [
  759. 'description' => 'A separate one',
  760. 'content' => [
  761. 'application/json' => [
  762. 'schema' => [
  763. 'type' => 'object',
  764. 'properties' => [
  765. 'the other' => [
  766. 'example' => "one",
  767. 'type' => 'string',
  768. ],
  769. ],
  770. ],
  771. ],
  772. ],
  773. ],
  774. '201' => [
  775. 'description' => '',
  776. 'content' => [
  777. 'application/json' => [
  778. 'schema' => [
  779. 'oneOf' => [
  780. [
  781. 'type' => 'object',
  782. 'description' => 'This one',
  783. 'properties' => [
  784. 'this' => [
  785. 'example' => "one",
  786. 'type' => 'string',
  787. ],
  788. ],
  789. ],
  790. [
  791. 'type' => 'object',
  792. 'description' => 'No, that one.',
  793. 'properties' => [
  794. 'that' => [
  795. 'example' => "one",
  796. 'type' => 'string',
  797. ],
  798. ],
  799. ],
  800. [
  801. 'type' => 'object',
  802. 'description' => 'No, another one.',
  803. 'properties' => [
  804. 'another' => [
  805. 'example' => "one",
  806. 'type' => 'string',
  807. ],
  808. ],
  809. ],
  810. ],
  811. ],
  812. ],
  813. ],
  814. ],
  815. ], $results['paths']['/path1']['post']['responses']);
  816. }
  817. /** @test */
  818. public function adds_enum_values_to_response_properties()
  819. {
  820. $endpointData = $this->createMockEndpointData([
  821. 'httpMethods' => ['GEt'],
  822. 'uri' => '/path1',
  823. 'responses' => [
  824. [
  825. 'status' => 200,
  826. 'description' => 'List of entities',
  827. 'content' => '{"data":[{"name":"Resource name","uuid":"UUID","primary":true}]}',
  828. ],
  829. ],
  830. 'responseFields' => [
  831. 'data' => [
  832. 'name' => 'data',
  833. 'type' => 'array',
  834. 'description' => 'Data wrapper',
  835. ],
  836. 'data.name' => [
  837. 'name' => 'Resource name',
  838. 'type' => 'string',
  839. 'description' => 'Name of the resource object',
  840. 'required' => true,
  841. ],
  842. 'data.uuid' => [
  843. 'name' => 'Resource UUID',
  844. 'type' => 'string',
  845. 'description' => 'Unique ID for the resource',
  846. 'required' => true,
  847. ],
  848. 'data.primary' => [
  849. 'name' => 'Is primary',
  850. 'type' => 'bool',
  851. 'description' => 'Is primary resource',
  852. 'required' => true,
  853. ],
  854. ],
  855. ]);
  856. $groups = [$this->createGroup([$endpointData])];
  857. $results = $this->generate($groups);
  858. $this->assertArraySubset([
  859. '200' => [
  860. 'description' => 'List of entities',
  861. 'content' => [
  862. 'application/json' => [
  863. 'schema' => [
  864. 'type' => 'object',
  865. 'properties' => [
  866. 'data' => [
  867. 'type' => 'array',
  868. 'description' => 'Data wrapper',
  869. 'items' => [
  870. 'type' => 'object',
  871. 'properties' => [
  872. 'name' => [
  873. 'type' => 'string',
  874. 'description' => 'Name of the resource object',
  875. ],
  876. 'uuid' => [
  877. 'type' => 'string',
  878. 'description' => 'Unique ID for the resource',
  879. ],
  880. 'primary' => [
  881. 'type' => 'boolean',
  882. 'description' => 'Is primary resource',
  883. ],
  884. ],
  885. ],
  886. 'required' => [
  887. 'name',
  888. 'uuid',
  889. 'primary',
  890. ]
  891. ],
  892. ],
  893. ],
  894. ],
  895. ],
  896. ],
  897. ], $results['paths']['/path1']['get']['responses']);
  898. }
  899. /** @test */
  900. public function lists_required_properties_in_request_body()
  901. {
  902. $endpointData = $this->createMockEndpointData([
  903. 'uri' => '/path',
  904. 'httpMethods' => ['POST'],
  905. 'bodyParameters' => [
  906. 'my_field' => [
  907. 'name' => 'my_field',
  908. 'description' => '',
  909. 'required' => true,
  910. 'example' => 'abc',
  911. 'type' => 'string',
  912. 'nullable' => false,
  913. ],
  914. 'other_field.nested_field' => [
  915. 'name' => 'nested_field',
  916. 'description' => '',
  917. 'required' => true,
  918. 'example' => 'abc',
  919. 'type' => 'string',
  920. 'nullable' => false,
  921. ],
  922. ],
  923. ]);
  924. $groups = [$this->createGroup([$endpointData])];
  925. $results = $this->generate($groups);
  926. $this->assertArraySubset([
  927. 'requestBody' => [
  928. 'content' => [
  929. 'application/json' => [
  930. 'schema' => [
  931. 'type' => 'object',
  932. 'properties' => [
  933. 'my_field' => [
  934. 'type' => 'string',
  935. ],
  936. 'other_field' => [
  937. 'type' => 'object',
  938. 'properties' => [
  939. 'nested_field' => [
  940. 'type' => 'string',
  941. ],
  942. ],
  943. 'required' => ['nested_field'],
  944. ],
  945. ],
  946. 'required' => ['my_field']
  947. ],
  948. ],
  949. ],
  950. ],
  951. ], $results['paths']['/path']['post']);
  952. }
  953. protected function createMockEndpointData(array $custom = []): OutputEndpointData
  954. {
  955. $faker = Factory::create();
  956. $path = '/' . $faker->word();
  957. $data = [
  958. 'uri' => $path,
  959. 'httpMethods' => $faker->randomElements(['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], 1),
  960. 'headers' => [
  961. 'Content-Type' => 'application/json',
  962. ],
  963. 'metadata' => [
  964. 'title' => $faker->sentence(),
  965. 'description' => $faker->randomElement([$faker->sentence(), '']),
  966. 'authenticated' => $faker->boolean(),
  967. ],
  968. 'urlParameters' => [], // Should be set by caller (along with custom path)
  969. 'queryParameters' => [],
  970. 'bodyParameters' => [],
  971. 'responses' => [
  972. [
  973. 'status' => 200,
  974. 'content' => '{"random": "json"}',
  975. 'description' => 'Okayy',
  976. ],
  977. ],
  978. 'responseFields' => [],
  979. ];
  980. foreach ($custom as $key => $value) {
  981. data_set($data, $key, $value);
  982. }
  983. return OutputEndpointData::create($data);
  984. }
  985. protected function createGroup(array $endpoints)
  986. {
  987. $faker = Factory::create();
  988. return [
  989. 'description' => '',
  990. 'name' => $faker->randomElement(['Endpoints', 'Group A', 'Group B']),
  991. 'endpoints' => $endpoints,
  992. ];
  993. }
  994. protected function generate(array $groups): array
  995. {
  996. $writer = new OpenAPISpecWriter(new DocumentationConfig($this->config));
  997. return $writer->generateSpecContent($groups);
  998. }
  999. }