utOpenAPIWriter.pp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. unit utOpenAPIWriter;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry, fpjson, fpjson.schema.schema, fpopenapi.objects, fpopenapi.writer;
  6. type
  7. { TTestOpenApiWriterBase }
  8. TTestOpenApiWriterBase = class(TTestCase)
  9. private
  10. FDocument: TJSONObject;
  11. FOpenAPI: TOpenAPI;
  12. procedure AddSource(Const aFmt : string; aArgs : Array of const);
  13. Procedure AddSource(Const aLine : string);
  14. procedure AddDecl(Const aFmt : string; aArgs : Array of const);
  15. protected
  16. procedure SetUp; override;
  17. procedure TearDown; override;
  18. Procedure WriteOpenAPI;
  19. procedure CheckJSON(const Msg: String; aJSON: TJSONStringType);
  20. procedure TestWrite(const Msg: String; aJSON: TJSONStringType);
  21. Public
  22. Property OpenApi : TOpenAPI Read FOpenAPI;
  23. Property Document : TJSONObject Read FDocument;
  24. published
  25. procedure TestHookUp;
  26. end;
  27. { TTestOpenApiWriter }
  28. TTestOpenApiWriterOpenAPI = class(TTestOpenApiWriterBase)
  29. Published
  30. procedure TestEmpty;
  31. Procedure TestOpenAPI;
  32. Procedure TestJSONSchemaDialect;
  33. end;
  34. TTestOpenApiWriterInfo = class(TTestOpenApiWriterBase)
  35. Published
  36. procedure TestVersion;
  37. procedure TestTitle;
  38. procedure TestSummary;
  39. procedure TestDescription;
  40. procedure TestTermsOfService;
  41. procedure TestContactEmail;
  42. procedure TestContactName;
  43. procedure TestContactURL;
  44. procedure TestLicenseUrl;
  45. procedure TestLicenseIdentifier;
  46. procedure TestLicenseName;
  47. end;
  48. TTestOpenApiWriterTags = class(TTestOpenApiWriterBase)
  49. Published
  50. Procedure TestName;
  51. Procedure TestDescription;
  52. Procedure TestExternalDocs;
  53. Procedure Test2Tags;
  54. end;
  55. TTestOpenApiWriterExternalDocs = class(TTestOpenApiWriterBase)
  56. Published
  57. procedure TestUrl;
  58. procedure TestDescription;
  59. end;
  60. { TTestOpenApiWriterServers }
  61. TTestOpenApiWriterServers = class(TTestOpenApiWriterBase)
  62. Published
  63. Procedure TestUrl;
  64. Procedure TestDescription;
  65. procedure TestVariablesDefault;
  66. procedure TestVariablesDescription;
  67. procedure TestVariablesEnum;
  68. Procedure Test2Url;
  69. end;
  70. { TTestOpenApiWriterPaths }
  71. TTestOpenApiWriterPathBase = class(TTestOpenApiWriterBase)
  72. Public
  73. Function AddPath(aPath : String) : TPathItem;
  74. end;
  75. TTestOpenApiWriterPaths = class(TTestOpenApiWriterPathBase)
  76. Published
  77. Procedure TestEmpty;
  78. Procedure TestRef;
  79. Procedure TestSummary;
  80. Procedure TestDescription;
  81. Procedure TestGetEmpty;
  82. Procedure TestGetSummary;
  83. Procedure TestGetDescription;
  84. Procedure TestGetOperationId;
  85. Procedure TestGetDeprecated;
  86. Procedure TestGetExternalDocsEmpty;
  87. Procedure TestGetExternalDocsURL;
  88. Procedure TestGetServers;
  89. end;
  90. { TTestOpenApiWriterPathRequestBody }
  91. TTestOpenApiWriterPathRequestBody = class(TTestOpenApiWriterPathBase)
  92. Public
  93. Function AddBody(aPath : String) : TRequestBody;
  94. Published
  95. procedure TestEmpty;
  96. procedure TestDescription;
  97. procedure TestRequired;
  98. procedure TestContent;
  99. procedure TestContentSchema;
  100. procedure TestContentExample;
  101. procedure TestContentExamples;
  102. procedure TestContentEncodingContentType;
  103. procedure TestContentEncodingExplode;
  104. procedure TestContentEncodingStyle;
  105. procedure TestContentEncodingAllowReserved;
  106. procedure TestContentEncodingHeaders;
  107. end;
  108. { TTestOpenApiWriterPathParameters }
  109. TTestOpenApiWriterPathParameters = class(TTestOpenApiWriterPathBase)
  110. Public
  111. Function AddParam(aPath : String) : TParameter;
  112. Published
  113. Procedure TestGetParametersEmpty;
  114. end;
  115. { TTestOpenApiWriterPathResponses }
  116. TTestOpenApiWriterPathResponses = class(TTestOpenApiWriterPathBase)
  117. Public
  118. Function AddResponse(aPath : String) : TResponse;
  119. Published
  120. procedure TestEmpty;
  121. procedure TestHeaders;
  122. procedure TestDescription;
  123. procedure TestContentEmpty;
  124. procedure TestContentSchema;
  125. procedure TestContentExample;
  126. procedure TestContentExamples;
  127. procedure TestContentEncodingEmpty;
  128. procedure TestContentEncodingContentType;
  129. end;
  130. { TTestOpenApiWriterPathResponsesLinks }
  131. TTestOpenApiWriterPathResponsesLinks = class(TTestOpenApiWriterPathBase)
  132. Public
  133. Function AddLink(aPath : String) : TLink;
  134. Published
  135. procedure TestEmpty;
  136. Procedure TestOperatonRef;
  137. Procedure TestOperatonId;
  138. Procedure TestParameters;
  139. Procedure TestRequestBody;
  140. Procedure TestDescription;
  141. Procedure TestServer;
  142. end;
  143. { TTestOpenApiWriterPathSecurity }
  144. TTestOpenApiWriterPathSecurity = class(TTestOpenApiWriterPathBase)
  145. Published
  146. Procedure TestEmpty;
  147. Procedure TestAPIKey;
  148. end;
  149. { TTestOpenApiWriterWebHooks }
  150. TTestOpenApiWriterWebHooks = class(TTestOpenApiWriterBase)
  151. Public
  152. function AddWebHook(const aname : string) : TPathItem;
  153. Published
  154. procedure TestEmpty;
  155. procedure TestSummary;
  156. // At the moment, no more tests needed: the items are identical to path items.
  157. end;
  158. { TTestOpenApiWriterComponents }
  159. TTestOpenApiWriterComponents = class(TTestOpenApiWriterBase)
  160. Public
  161. function AddComponentSchema(const aname: string): TJSONSchema;
  162. Published
  163. procedure TestSchemaEmpty;
  164. procedure TestSchemaContentEncoding;
  165. end;
  166. { TTestOpenApiWriterSecurity }
  167. TTestOpenApiWriterSecurity = class(TTestOpenApiWriterBase)
  168. Published
  169. procedure TestOne;
  170. end;
  171. (*
  172. property Components: TComponents Read Get_Components write Set_Components;
  173. property Security: TSecurityRequirementList Read Get_Security write Set_Security;
  174. *)
  175. implementation
  176. uses jsoncomparer;
  177. var
  178. _Sources : TStrings;
  179. _Decl : TStrings;
  180. _LastClass : String;
  181. function ChangeRead(S : String) : String;
  182. begin
  183. Result:=StringReplace(S,'Write','Reade',[rfReplaceAll,rfIgnoreCase]);
  184. end;
  185. procedure StartSources; forward;
  186. Procedure AddMethod(aClassName,aTestName : string; const aJSON : String);
  187. var
  188. lClass,lTest : String;
  189. J,S : TJSONObject;
  190. D : TJSONData;
  191. P,N : String;
  192. begin
  193. lClass:=ChangeRead(aClassName);
  194. lTest:=ChangeRead(aTestName);
  195. if lClass<>_LastClass then
  196. begin
  197. if _LastClass<>'' then
  198. _Decl.Add(' end;');
  199. _Decl.Add('');
  200. _Decl.Add(' %s = Class(TOpenAPITestRead)',[lClass]);
  201. _LastClass:=lClass;
  202. end;
  203. _Decl.Add(' Procedure %s;',[lTest]);
  204. _Sources.Add('');
  205. _Sources.Add('procedure %s.%s;',[lClass,lTest]);
  206. _Sources.Add('');
  207. _Sources.Add('begin');
  208. _Sources.Add(' TestRead(''%s'');',[aJSON]);
  209. J:=GetJSON(aJSON) as TJSONObject;
  210. P:='OpenAPI';
  211. S:=Nil;
  212. try
  213. if J.Count>0 then
  214. begin
  215. D:=J.Items[0];
  216. if D is TJSONObject then
  217. S:=TJSONObject(D);
  218. N:=J.Names[0];
  219. While S is TJSONObject do
  220. begin
  221. P:=P+'.'+N;
  222. _Sources.Add(' AssertNotNull(''%s'',%s);',[P,P]);
  223. if S.Count=0 then
  224. begin
  225. D:=Nil;
  226. S:=Nil;
  227. Continue;
  228. end;
  229. D:=S.Items[0];
  230. N:=S.Names[0];
  231. if (D is TJSONArray) and (TJSONArray(D).Count>0) then
  232. begin
  233. D:=TJSONArray(D)[0];
  234. N:=N+'[0]';
  235. end;
  236. if D is TJSONObject then
  237. S:=TJSONObject(D)
  238. else
  239. S:=Nil;
  240. end;
  241. P:=P+'.'+N;
  242. if assigned(D) then
  243. Case D.JSONType of
  244. jtString : _Sources.Add(' AssertEquals(''%s'',''%s'',%s);',[P,D.AsString,P]);
  245. jtBoolean : _Sources.Add(' AssertEquals(''%s'',%s,%s);',[P,D.AsString,P]);
  246. jtNumber : _Sources.Add(' AssertEquals(''%s'',%s,%s);',[P,D.AsString,P]);
  247. end;
  248. end;
  249. finally
  250. J.Free;
  251. end;
  252. _Sources.Add('end;');
  253. _Sources.Add('');
  254. end;
  255. procedure TTestOpenApiWriterBase.AddSource(const aFmt: string; aArgs: array of const);
  256. begin
  257. AddSource(Format(aFmt,aArgs));
  258. end;
  259. procedure TTestOpenApiWriterBase.AddSource(const aLine: string);
  260. begin
  261. _Sources.Add(aLine);
  262. end;
  263. procedure TTestOpenApiWriterBase.AddDecl(const aFmt: string; aArgs: array of const);
  264. begin
  265. _Decl.Add(aFmt,aArgs);
  266. end;
  267. procedure TTestOpenApiWriterBase.SetUp;
  268. begin
  269. FOpenAPI:=TOpenAPI.Create;
  270. if _Decl=Nil then
  271. StartSources;
  272. end;
  273. procedure TTestOpenApiWriterBase.TearDown;
  274. begin
  275. FreeAndNil(FOpenAPI);
  276. FreeAndNil(FDocument);
  277. end;
  278. procedure TTestOpenApiWriterBase.WriteOpenAPI;
  279. var
  280. W : TOpenAPIWriter;
  281. begin
  282. W:=TOpenAPIWriter.Create(Nil);
  283. try
  284. FDocument:=W.WriteToJSON(OpenAPI);
  285. finally
  286. W.Free;
  287. end;
  288. end;
  289. procedure TTestOpenApiWriterBase.CheckJSON(const Msg: String; aJSON: TJSONStringType);
  290. var
  291. Data : TJSONData;
  292. Obj : TJSONObject absolute data;
  293. Errors : TStrings;
  294. begin
  295. Errors:=Nil;
  296. Data:=GetJSON(aJSON,true) as TJSONObject;
  297. try
  298. AssertEquals('Json object',TJSONObject,Data.ClassType);
  299. Errors:=TStringList.Create;
  300. // Writeln('Comparing ',Obj.AsJSON,' === ',Document.AsJSON);
  301. TJSONComparer.Compare(Obj,Document,Errors);
  302. if (Errors.Count<>0) then
  303. Fail(Msg+':'#10+Errors.Text);
  304. finally
  305. Errors.Free;
  306. Data.Free;
  307. end;
  308. end;
  309. procedure TTestOpenApiWriterBase.TestWrite(const Msg: String; aJSON: TJSONStringType);
  310. begin
  311. AddMethod(ClassName,TestName,aJSON);
  312. WriteOpenAPI;
  313. CheckJSON(msg,aJSON);
  314. end;
  315. procedure TTestOpenApiWriterBase.TestHookUp;
  316. begin
  317. AssertNotNull('have openapi',OpenAPI);
  318. AssertNull('have no document',Document);
  319. end;
  320. procedure TTestopenApiWriterOpenApi.TestEmpty;
  321. begin
  322. WriteOpenApi;
  323. CheckJSON('Empty','{}');
  324. end;
  325. procedure TTestopenApiWriterOpenAPI.TestOpenAPI;
  326. begin
  327. OpenAPI.OpenAPI:='123';
  328. TestWrite('openapi version','{ "openapi" : "123" }');
  329. end;
  330. procedure TTestopenApiWriterOpenAPI.TestJSONSchemaDialect;
  331. begin
  332. OpenAPI.JSONSchemaDialect:='123';
  333. TestWrite('json schema dialect','{ "jsonSchemaDialect" : "123" }');
  334. end;
  335. procedure TTestopenApiWriterInfo.TestVersion;
  336. begin
  337. OpenAPI.Info.Version:='123';
  338. TestWrite('Version','{ "info" : { "version" : "123" } }');
  339. end;
  340. procedure TTestopenApiWriterInfo.TestTitle;
  341. begin
  342. OpenAPI.Info.title:='123';
  343. TestWrite('Title','{ "info" : { "title" : "123" } }');
  344. end;
  345. procedure TTestopenApiWriterInfo.TestSummary;
  346. begin
  347. OpenAPI.Info.summary:='123';
  348. TestWrite('Summary','{ "info" : { "summary" : "123" } }');
  349. end;
  350. procedure TTestopenApiWriterInfo.TestDescription;
  351. begin
  352. OpenAPI.Info.Description:='123';
  353. TestWrite('Description','{ "info" : { "description" : "123" } }');
  354. end;
  355. procedure TTestopenApiWriterInfo.TestTermsOfService;
  356. begin
  357. OpenAPI.Info.TermsOfService:='123';
  358. TestWrite('TermsOfService','{ "info" : { "termsOfService" : "123" } }');
  359. end;
  360. procedure TTestopenApiWriterInfo.TestContactEmail;
  361. begin
  362. OpenAPI.Info.Contact.Email:='123';
  363. TestWrite('Contact mail','{ "info" : { "contact" : { "email" : "123" } } }');
  364. end;
  365. procedure TTestopenApiWriterInfo.TestContactName;
  366. begin
  367. OpenAPI.Info.Contact.Name:='123';
  368. TestWrite('Contact name','{ "info" : { "contact" : { "name" : "123" } } }');
  369. end;
  370. procedure TTestopenApiWriterInfo.TestContactURL;
  371. begin
  372. OpenAPI.Info.Contact.URL:='123';
  373. TestWrite('Contact url','{ "info" : { "contact" : { "url" : "123" } } }');
  374. end;
  375. procedure TTestopenApiWriterInfo.TestLicenseUrl;
  376. begin
  377. OpenAPI.Info.License.Url:='123';
  378. TestWrite('license url','{ "info" : { "license" : { "url" : "123" } } }');
  379. end;
  380. procedure TTestopenApiWriterInfo.TestLicenseName;
  381. begin
  382. OpenAPI.Info.License.Name:='123';
  383. TestWrite('license name','{ "info" : { "license" : { "name" : "123" } } }');
  384. end;
  385. procedure TTestopenApiWriterInfo.TestLicenseIdentifier;
  386. begin
  387. OpenAPI.Info.License.Identifier:='123';
  388. TestWrite('license name','{ "info" : { "license" : { "identifier" : "123" } } }');
  389. end;
  390. procedure TTestopenApiWriterTags.TestName;
  391. var
  392. T : TTag;
  393. begin
  394. T:=TTag.Create(OpenAPI,'tags[0]');
  395. T.Name:='123';
  396. OpenAPI.Tags.Add(T);
  397. TestWrite('name','{ "tags" : [ { "name" : "123" } ] }');
  398. end;
  399. procedure TTestopenApiWriterTags.TestDescription;
  400. var
  401. T : TTag;
  402. begin
  403. T:=TTag.Create(OpenAPI,'tags[1]');
  404. T.Description:='123';
  405. OpenAPI.Tags.Add(T);
  406. TestWrite('description','{ "tags" : [ { "description" : "123" } ] }');
  407. end;
  408. procedure TTestopenApiWriterTags.TestExternalDocs;
  409. var
  410. T : TTag;
  411. begin
  412. T:=TTag.Create(OpenAPI,'tags[1]');
  413. T.ExternalDocs.Url:='123';
  414. OpenAPI.Tags.Add(T);
  415. TestWrite('externaldocs','{ "tags" : [ { "externalDocs" : { "url" :"123" } } ] }');
  416. end;
  417. procedure TTestopenApiWriterTags.Test2Tags;
  418. var
  419. T : TTag;
  420. begin
  421. T:=TTag.Create(OpenAPI,'tags[0]');
  422. T.Name:='123';
  423. OpenAPI.Tags.Add(T);
  424. T:=TTag.Create(OpenAPI,'tags[1]');
  425. T.Name:='456';
  426. OpenAPI.Tags.Add(T);
  427. TestWrite('multi','{ "tags" : [ { "name" : "123" }, { "name" : "456" } ] }');
  428. end;
  429. procedure TTestopenApiWriterExternalDocs.TestUrl;
  430. begin
  431. OpenAPI.ExternalDocs.Url:='123';
  432. TestWrite('Url','{ "externalDocs" : { "url" :"123" } }');
  433. end;
  434. procedure TTestopenApiWriterExternalDocs.TestDescription;
  435. begin
  436. OpenAPI.ExternalDocs.Description:='123';
  437. TestWrite('description','{ "externalDocs" : { "description" :"123" } }');
  438. end;
  439. { TTestOpenApiWriterServers }
  440. procedure TTestOpenApiWriterServers.TestUrl;
  441. var
  442. Server : TServer;
  443. begin
  444. Server:=TServer.Create(OpenAPI,'servers[0]');
  445. Server.Url:='123';
  446. OpenAPI.Servers.Add(Server);
  447. TestWrite('url','{ "servers" : [ { "url" :"123" } ] }');
  448. end;
  449. procedure TTestOpenApiWriterServers.TestDescription;
  450. var
  451. Server : TServer;
  452. begin
  453. Server:=TServer.Create(OpenAPI,'servers[0]');
  454. Server.description:='123';
  455. OpenAPI.Servers.Add(Server);
  456. TestWrite('url','{ "servers" : [ { "description" :"123" } ] }');
  457. end;
  458. procedure TTestOpenApiWriterServers.TestVariablesDefault;
  459. var
  460. Server : TServer;
  461. ServerVar : TServerVariable;
  462. begin
  463. Server:=TServer.Create(OpenAPI,'servers[0]');
  464. ServerVar:=Server.Variables.AddItem('user');
  465. ServerVar.Default:='123';
  466. OpenAPI.Servers.Add(Server);
  467. TestWrite('var.default','{ "servers" : [ { "variables" : { "user": { "default": "123" } } } ] }');
  468. end;
  469. procedure TTestOpenApiWriterServers.TestVariablesDescription;
  470. var
  471. Server : TServer;
  472. ServerVar : TServerVariable;
  473. begin
  474. Server:=TServer.Create(OpenAPI,'servers[0]');
  475. ServerVar:=Server.Variables.AddItem('user');
  476. ServerVar.Description:='123';
  477. OpenAPI.Servers.Add(Server);
  478. TestWrite('var.description','{ "servers" : [ { "variables" : { "user": { "description": "123" } } } ] }');
  479. end;
  480. procedure TTestOpenApiWriterServers.TestVariablesEnum;
  481. var
  482. Server : TServer;
  483. ServerVar : TServerVariable;
  484. begin
  485. Server:=TServer.Create(OpenAPI,'servers[0]');
  486. ServerVar:=Server.Variables.AddItem('user');
  487. ServerVar.Enum.Add('123');
  488. ServerVar.Enum.Add('456');
  489. OpenAPI.Servers.Add(Server);
  490. TestWrite('var.enum','{ "servers" : [ { "variables" : { "user": { "enum": [ "123", "456" ] } } } ] }');
  491. end;
  492. procedure TTestOpenApiWriterServers.Test2Url;
  493. var
  494. Server : TServer;
  495. begin
  496. Server:=TServer.Create(OpenAPI,'servers[0]');
  497. Server.Url:='123';
  498. OpenAPI.Servers.Add(Server);
  499. Server:=TServer.Create(OpenAPI,'servers[0]');
  500. Server.Url:='456';
  501. OpenAPI.Servers.Add(Server);
  502. TestWrite('url','{ "servers" : [ { "url" :"123" }, { "url" :"456" } ] }');
  503. end;
  504. { TTestOpenApiWriterPaths }
  505. function TTestOpenApiWriterPathBase.AddPath(aPath: String): TPathItem;
  506. begin
  507. Result:=OpenAPI.Paths.AddItem(aPath);
  508. end;
  509. procedure TTestOpenApiWriterPaths.TestEmpty;
  510. var
  511. P : TPathItem;
  512. begin
  513. P:=AddPath('api');
  514. AssertNotNull('Have path',P);
  515. TestWrite('Path','{ "paths" : { "api" : {} } }');
  516. end;
  517. procedure TTestOpenApiWriterPaths.TestRef;
  518. var
  519. P : TPathItem;
  520. begin
  521. P:=AddPath('api');
  522. P.Ref:='abc';
  523. TestWrite('Path ref','{ "paths" : { "api" : { "$ref": "abc"} } }');
  524. end;
  525. procedure TTestOpenApiWriterPaths.TestSummary;
  526. var
  527. P : TPathItem;
  528. begin
  529. P:=AddPath('api');
  530. P.Summary:='abc';
  531. TestWrite('Path summary','{ "paths" : { "api" : { "summary": "abc"} } }');
  532. end;
  533. procedure TTestOpenApiWriterPaths.TestDescription;
  534. var
  535. P : TPathItem;
  536. begin
  537. P:=AddPath('api');
  538. P.description:='abc';
  539. TestWrite('Path description','{ "paths" : { "api" : { "description": "abc"} } }');
  540. end;
  541. procedure TTestOpenApiWriterPaths.TestGetServers;
  542. var
  543. P : TPathItem;
  544. S : TServer;
  545. begin
  546. P:=AddPath('api');
  547. S:=TServer.Create(P,'servers[0]');
  548. P.Servers.Add(S);
  549. TestWrite('Path description','{ "paths" : { "api" : { "servers": [ {} ] } } }');
  550. end;
  551. procedure TTestOpenApiWriterPaths.TestGetEmpty;
  552. var
  553. P : TPathItem;
  554. begin
  555. P:=AddPath('api');
  556. AssertEquals('Empty','',P.Get.Summary);
  557. TestWrite('Path get','{ "paths" : { "api" : { "get": { } } } }');
  558. end;
  559. procedure TTestOpenApiWriterPaths.TestGetSummary;
  560. var
  561. P : TPathItem;
  562. begin
  563. P:=AddPath('api');
  564. P.Get.Summary:='123';
  565. TestWrite('Path get summary','{ "paths" : { "api" : { "get": { "summary" : "123" } } } }');
  566. end;
  567. procedure TTestOpenApiWriterPaths.TestGetDescription;
  568. var
  569. P : TPathItem;
  570. begin
  571. P:=AddPath('api');
  572. P.Get.Description:='123';
  573. TestWrite('Path get description','{ "paths" : { "api" : { "get": { "description" : "123" } } } }');
  574. end;
  575. procedure TTestOpenApiWriterPaths.TestGetOperationId;
  576. var
  577. P : TPathItem;
  578. begin
  579. P:=AddPath('api');
  580. P.Get.operationId:='123';
  581. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "operationId" : "123" } } } }');
  582. end;
  583. procedure TTestOpenApiWriterPaths.TestGetDeprecated;
  584. var
  585. P : TPathItem;
  586. begin
  587. P:=AddPath('api');
  588. P.Get.Deprecated:=True;
  589. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "deprecated" : true } } } }');
  590. end;
  591. procedure TTestOpenApiWriterPaths.TestGetExternalDocsEmpty;
  592. var
  593. P : TPathItem;
  594. begin
  595. P:=AddPath('api');
  596. AssertNotNull('Get creates',P.Get.ExternalDocs);
  597. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "externalDocs" : { } } } } }');
  598. end;
  599. procedure TTestOpenApiWriterPaths.TestGetExternalDocsURL;
  600. var
  601. P : TPathItem;
  602. begin
  603. P:=AddPath('api');
  604. P.Get.ExternalDocs.Url:='123';
  605. TestWrite('Path get externaldocs','{ "paths" : { "api" : { "get": { "externalDocs" : { "url" : "123" } } } } }');
  606. end;
  607. { TTestOpenApiWriterPathRequestBody }
  608. function TTestOpenApiWriterPathRequestBody.AddBody(aPath: String): TRequestBody;
  609. begin
  610. Result:=AddPath(aPath).Get.RequestBody;
  611. end;
  612. procedure TTestOpenApiWriterPathRequestBody.TestEmpty;
  613. var
  614. R : TRequestBody;
  615. begin
  616. R:=AddBody('api');
  617. AssertNotNull('Get creates',R);
  618. TestWrite('Path get requestbody','{ "paths" : { "api" : { "get": { "requestBody" : { } } } } }');
  619. end;
  620. procedure TTestOpenApiWriterPathRequestBody.TestDescription;
  621. var
  622. R : TRequestBody;
  623. begin
  624. R:=AddBody('api');
  625. R.Description:='123';
  626. TestWrite('Path get requestbody','{ "paths" : { "api" : { "get": { "requestBody" : { "description" : "123" } } } } }');
  627. end;
  628. procedure TTestOpenApiWriterPathRequestBody.TestRequired;
  629. var
  630. R : TRequestBody;
  631. begin
  632. R:=AddBody('api');
  633. R.Required:=True;
  634. TestWrite('Path get requestbody','{ "paths" : { "api" : { "get": { "requestBody" : { "required" : true} } } } }');
  635. end;
  636. procedure TTestOpenApiWriterPathRequestBody.TestContent;
  637. var
  638. R : TRequestBody;
  639. begin
  640. R:=AddBody('api');
  641. R.Content.AddItem('123');
  642. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { } } } } } } }');
  643. end;
  644. procedure TTestOpenApiWriterPathRequestBody.TestContentSchema;
  645. var
  646. R : TRequestBody;
  647. begin
  648. R:=AddBody('api');
  649. R.Content.AddItem('123').Schema:=TJSONSchema.Create();
  650. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "schema" : true } } } } } } }');
  651. end;
  652. procedure TTestOpenApiWriterPathRequestBody.TestContentExample;
  653. var
  654. R : TRequestBody;
  655. begin
  656. R:=AddBody('api');
  657. R.Content.AddItem('123').example:=TJSONObject.Create;
  658. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "example" : { } } } } } } } }');
  659. end;
  660. procedure TTestOpenApiWriterPathRequestBody.TestContentExamples;
  661. var
  662. R : TRequestBody;
  663. begin
  664. R:=AddBody('api');
  665. R.Content.AddItem('123').examples.AddItem('abc').Summary:='x';
  666. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "examples" : { "abc" : { "summary": "x"} } } } } } } } }');
  667. end;
  668. procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingContentType;
  669. var
  670. R : TRequestBody;
  671. begin
  672. R:=AddBody('api');
  673. R.Content.AddItem('123').Encoding.AddItem('abc').ContentType:='def';
  674. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "contentType": "def"} } } } } } } } }');
  675. end;
  676. procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingExplode;
  677. var
  678. R : TRequestBody;
  679. begin
  680. R:=AddBody('api');
  681. R.Content.AddItem('123').Encoding.AddItem('abc').Explode:=True;
  682. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "explode": true} } } } } } } } }');
  683. end;
  684. procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingStyle;
  685. var
  686. R : TRequestBody;
  687. begin
  688. R:=AddBody('api');
  689. R.Content.AddItem('123').Encoding.AddItem('abc').Style:='def';
  690. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "style": "def"} } } } } } } } }');
  691. end;
  692. procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingAllowReserved;
  693. var
  694. R : TRequestBody;
  695. begin
  696. R:=AddBody('api');
  697. R.Content.AddItem('123').Encoding.AddItem('abc').AllowReserved:=True;
  698. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "allowReserved": true} } } } } } } } }');
  699. end;
  700. procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingHeaders;
  701. var
  702. R : TRequestBody;
  703. begin
  704. R:=AddBody('api');
  705. R.Content.AddItem('123').Encoding.AddItem('abc').Headers.AddItem('def').Explode:=True;
  706. TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "headers": { "def" : { "explode" : true } } } } } } } } } } }');
  707. end;
  708. function TTestOpenApiWriterPathParameters.AddParam(aPath: String): TParameter;
  709. var
  710. P : TPathItem;
  711. begin
  712. P:=addPath(aPath);
  713. Result:=P.Parameters.AddParam(P);
  714. end;
  715. procedure TTestOpenApiWriterPathParameters.TestGetParametersEmpty;
  716. var
  717. P : TParameter;
  718. begin
  719. P:=AddParam('api');
  720. AssertNotNull('have p',p);
  721. TestWrite('Path param','{ "paths" : { "api" : { "parameters": [ {} ] } } }');
  722. end;
  723. function TTestOpenApiWriterPathResponses.AddResponse(aPath: String): TResponse;
  724. begin
  725. Result:=AddPath(aPath).Get.Responses.AddItem('default');
  726. end;
  727. procedure TTestOpenApiWriterPathResponses.TestEmpty;
  728. begin
  729. AssertNotNull('Have response',AddResponse('api'));
  730. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : {} } } } } }');
  731. end;
  732. procedure TTestOpenApiWriterPathResponses.TestHeaders;
  733. begin
  734. AddResponse('api').Headers.AddItem('x-content');
  735. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "headers" : { "x-content" : { } } } } } } } }');
  736. end;
  737. procedure TTestOpenApiWriterPathResponses.TestDescription;
  738. begin
  739. AddResponse('api').Description:='abc';
  740. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "description" : "abc" } } } } } }');
  741. end;
  742. procedure TTestOpenApiWriterPathResponses.TestContentEmpty;
  743. begin
  744. AddResponse('api').content.AddItem('application/json');
  745. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { } } } } } } } }');
  746. end;
  747. procedure TTestOpenApiWriterPathResponses.TestContentSchema;
  748. begin
  749. AssertNotNull('Have schema',AddResponse('api').content.AddItem('application/json').Schema);
  750. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "schema" : true } } } } } } } }');
  751. end;
  752. procedure TTestOpenApiWriterPathResponses.TestContentExample;
  753. begin
  754. AddResponse('api').content.AddItem('application/json').Example:=TJSONObject.Create;
  755. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "example" : {} } } } } } } } }');
  756. end;
  757. procedure TTestOpenApiWriterPathResponses.TestContentExamples;
  758. begin
  759. AddResponse('api').content.AddItem('application/json').Examples.AddItem('abc').Summary:='x';
  760. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "examples" : { "abc" : { "summary": "x"} } } } } } } } } }');
  761. end;
  762. procedure TTestOpenApiWriterPathResponses.TestContentEncodingEmpty;
  763. begin
  764. AddResponse('api').content.AddItem('application/json').Encoding.AddItem('abc'); //.ContentType:='application/json';
  765. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "encoding" : { "abc" : { } } } } } } } } } }');
  766. end;
  767. procedure TTestOpenApiWriterPathResponses.TestContentEncodingContentType;
  768. begin
  769. AddResponse('api').content.AddItem('application/json').Encoding.AddItem('abc').ContentType:='application/json';
  770. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "encoding" : { "abc" : { "contentType" : "application/json" } } } } } } } } } }');
  771. end;
  772. { TTestOpenApiWriterPathResponsesLinks }
  773. function TTestOpenApiWriterPathResponsesLinks.AddLink(aPath: String): TLink;
  774. begin
  775. Result:=AddPath(aPath).Get.Responses.AddItem('default').links.AddItem('abc');
  776. end;
  777. procedure TTestOpenApiWriterPathResponsesLinks.TestEmpty;
  778. begin
  779. AddLink('api');
  780. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { } } } } } } } }');
  781. end;
  782. procedure TTestOpenApiWriterPathResponsesLinks.TestOperatonRef;
  783. begin
  784. AddLink('api').OperationRef:='123';
  785. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "operationRef" : "123" } } } } } } } }');
  786. end;
  787. procedure TTestOpenApiWriterPathResponsesLinks.TestOperatonId;
  788. begin
  789. AddLink('api').OperationId:='123';
  790. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "operationId" : "123" } } } } } } } }');
  791. end;
  792. procedure TTestOpenApiWriterPathResponsesLinks.TestParameters;
  793. begin
  794. AddLink('api').Parameters.Add('x','y');
  795. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "parameters" : { "x" : "y" } } } } } } } } }');
  796. end;
  797. procedure TTestOpenApiWriterPathResponsesLinks.TestRequestBody;
  798. begin
  799. AddLink('api').RequestBody:=TJSONObject.Create();
  800. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "requestBody" : {} } } } } } } } }');
  801. end;
  802. procedure TTestOpenApiWriterPathResponsesLinks.TestDescription;
  803. begin
  804. AddLink('api').Description:='123';
  805. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "description" : "123" } } } } } } } }');
  806. end;
  807. procedure TTestOpenApiWriterPathResponsesLinks.TestServer;
  808. begin
  809. AddLink('api').Server.Url:='123';
  810. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "server" : { "url" : "123" } } } } } } } } }');
  811. end;
  812. procedure TTestOpenApiWriterPathSecurity.TestEmpty;
  813. var
  814. O : TAPIOperation;
  815. begin
  816. O:=AddPath('api').Get;
  817. O.Security.AddSecurity(O);
  818. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "security" : [ {} ] } } } }');
  819. end;
  820. procedure TTestOpenApiWriterPathSecurity.TestAPIKey;
  821. var
  822. O : TAPIOperation;
  823. begin
  824. O:=AddPath('api').Get;
  825. O.Security.AddSecurity(O).AddItem('api_key').Add('akey');
  826. TestWrite('Path param','{ "paths" : { "api" : { "get" : { "security" : [ { "api_key" : ["akey"] } ] } } } }');
  827. end;
  828. { TTestOpenApiWriterWebHooks }
  829. function TTestOpenApiWriterWebHooks.AddWebHook(const aname: string): TPathItem;
  830. begin
  831. Result:=OpenAPI.WebHooks.AddItem(aName);
  832. end;
  833. procedure TTestOpenApiWriterWebHooks.TestEmpty;
  834. begin
  835. AssertNotNull(OpenAPI.WebHooks.AddItem('newitem'));
  836. TestWrite('Path param','{ "webhooks" : { "newitem" : { } } }');
  837. end;
  838. procedure TTestOpenApiWriterWebHooks.TestSummary;
  839. begin
  840. OpenAPI.WebHooks.AddItem('newitem').Summary:='abc';
  841. TestWrite('Path param','{ "webhooks" : { "newitem" : { "summary" : "abc" } } }');
  842. end;
  843. { TTestOpenApiWriterComponents }
  844. function TTestOpenApiWriterComponents.AddComponentSchema(const aname: string): TJSONSchema;
  845. begin
  846. Result:=OpenAPI.Components.Schemas.Add(aName);
  847. end;
  848. procedure TTestOpenApiWriterComponents.TestSchemaEmpty;
  849. begin
  850. AssertNotNull('Schema',AddComponentSchema('abc'));
  851. TestWrite('Path componentschema','{ "components" : { "schemas" : { "abc" : true } } }');
  852. end;
  853. procedure TTestOpenApiWriterComponents.TestSchemaContentEncoding;
  854. begin
  855. AddComponentSchema('abc').Validations.contentEncoding:='utf8';
  856. TestWrite('Path componentschema','{ "components" : { "schemas" : { "abc" : { "contentEncoding" : "utf8" } } } }');
  857. end;
  858. { TTestOpenApiWriterSecurity }
  859. procedure TTestOpenApiWriterSecurity.TestOne;
  860. begin
  861. OpenAPI.Security.AddSecurity(OpenAPI).AddItem('abc');
  862. TestWrite('Security','{ "security" : [ { "abc" : [] } ] }');
  863. end;
  864. procedure StartSources;
  865. begin
  866. _Sources:=TStringList.Create;
  867. _Decl:=TStringList.Create;
  868. _Decl.Add('unit ReadTests;');
  869. _Decl.Add('');
  870. _Decl.Add('interface');
  871. _Decl.Add('');
  872. _Decl.Add('uses fpcunit;');
  873. _Decl.Add('');
  874. _Decl.Add('type');
  875. _Decl.Add('');
  876. end;
  877. procedure WriteSources;
  878. begin
  879. _Decl.Add(' end;');
  880. _Decl.Add('');
  881. _Decl.Add('implementation');
  882. _Decl.Add('');
  883. _Decl.AddStrings(_Sources);
  884. _Decl.Add('');
  885. _Decl.Add('end.');
  886. _Decl.SaveToFile('ReadTests.pp');
  887. _Sources.Free;
  888. _Decl.Free;
  889. end;
  890. initialization
  891. // StartSources;
  892. RegisterTests('Writer',[
  893. TTestopenApiWriterOpenAPI,
  894. TTestopenApiWriterInfo,
  895. TTestopenApiWriterTags,
  896. TTestOpenApiWriterExternalDocs,
  897. TTestOpenApiWriterServers,
  898. TTestOpenApiWriterPaths,
  899. TTestOpenApiWriterPathRequestBody,
  900. TTestOpenApiWriterPathParameters,
  901. TTestOpenApiWriterPathResponses,
  902. TTestOpenApiWriterPathSecurity,
  903. TTestOpenApiWriterPathResponsesLinks,
  904. TTestOpenApiWriterWebHooks,
  905. TTestOpenApiWriterComponents,
  906. TTestOpenApiWriterSecurity
  907. ]);
  908. finalization
  909. if assigned(_Decl) then
  910. WriteSources;
  911. end.