Skip to content

Commit 3b4d7ea

Browse files
committed
OpenApi v3 initial support
1 parent f17e994 commit 3b4d7ea

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

Source/OpenApiGen.V3.Analyzer.pas

+6-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ function TOpenApiAnalyzer.BuildMetaMethod(Method: TMetaMethod; const Path: strin
120120
MetaParam.ParamType := MetaTypeFromSchema(ContentPair.Value.Schema, Method.CodeName + 'Input', TListType.ltAuto);
121121
MetaParam.Location := TParamLocation.plBody;
122122
end;
123-
if not ConsumesJson then
123+
if ConsumesJson then
124+
Method.Consumes := MimeTypeJson
125+
else
124126
raise EOpenApiAnalyzerException.CreateFmt('Request body is present in method "%s" but does not consume JSON', [Method.UrlPath]);
125127
end;
126128

@@ -149,7 +151,9 @@ function TOpenApiAnalyzer.BuildMetaMethod(Method: TMetaMethod; const Path: strin
149151
end;
150152
Method.ReturnType := ResponseType;
151153
if (ResponseType <> nil) and not ResponseType.IsBinary then
152-
if not ProducesJson then
154+
if ProducesJson then
155+
Method.Produces := MimeTypeJson
156+
else
153157
raise EOpenApiAnalyzerException.CreateFmt('Response body is present in method "%s" but does not produce JSON', [Method.UrlPath]);
154158
Result := True;
155159
except

Tests/PetStore3/PetStore3Client.pas

+18
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ function TPetService.UpdatePet(Body: TPet): TPet;
343343
begin
344344
Request := CreateRequest('/pet', 'PUT');
345345
Request.AddBody(Converter.TPetToJson(Body));
346+
Request.AddHeader('Content-Type', 'application/json');
347+
Request.AddHeader('Accept', 'application/json');
346348
Response := Request.Execute;
347349
CheckError(Response);
348350
Result := Converter.TPetFromJson(Response.ContentAsString);
@@ -355,6 +357,8 @@ function TPetService.AddPet(Body: TPet): TPet;
355357
begin
356358
Request := CreateRequest('/pet', 'POST');
357359
Request.AddBody(Converter.TPetToJson(Body));
360+
Request.AddHeader('Content-Type', 'application/json');
361+
Request.AddHeader('Accept', 'application/json');
358362
Response := Request.Execute;
359363
CheckError(Response);
360364
Result := Converter.TPetFromJson(Response.ContentAsString);
@@ -367,6 +371,7 @@ function TPetService.FindPetsByStatus(Status: string): TPetList;
367371
begin
368372
Request := CreateRequest('/pet/findByStatus', 'GET');
369373
Request.AddQueryParam('status', Status);
374+
Request.AddHeader('Accept', 'application/json');
370375
Response := Request.Execute;
371376
CheckError(Response);
372377
Result := Converter.TPetListFromJson(Response.ContentAsString);
@@ -381,6 +386,7 @@ function TPetService.FindPetsByTags(Tags: stringArray): TPetList;
381386
Request := CreateRequest('/pet/findByTags', 'GET');
382387
for I := 0 to Length(Tags) - 1 do
383388
Request.AddQueryParam('tags', Tags[I]);
389+
Request.AddHeader('Accept', 'application/json');
384390
Response := Request.Execute;
385391
CheckError(Response);
386392
Result := Converter.TPetListFromJson(Response.ContentAsString);
@@ -393,6 +399,7 @@ function TPetService.GetPetById(PetId: Int64): TPet;
393399
begin
394400
Request := CreateRequest('/pet/{petId}', 'GET');
395401
Request.AddUrlParam('petId', IntToStr(PetId));
402+
Request.AddHeader('Accept', 'application/json');
396403
Response := Request.Execute;
397404
CheckError(Response);
398405
Result := Converter.TPetFromJson(Response.ContentAsString);
@@ -431,6 +438,7 @@ function TStoreService.GetInventory: TGetInventoryOutput;
431438
Response: IRestResponse;
432439
begin
433440
Request := CreateRequest('/store/inventory', 'GET');
441+
Request.AddHeader('Accept', 'application/json');
434442
Response := Request.Execute;
435443
CheckError(Response);
436444
Result := Converter.TGetInventoryOutputFromJson(Response.ContentAsString);
@@ -443,6 +451,8 @@ function TStoreService.PlaceOrder(Body: TOrder): TOrder;
443451
begin
444452
Request := CreateRequest('/store/order', 'POST');
445453
Request.AddBody(Converter.TOrderToJson(Body));
454+
Request.AddHeader('Content-Type', 'application/json');
455+
Request.AddHeader('Accept', 'application/json');
446456
Response := Request.Execute;
447457
CheckError(Response);
448458
Result := Converter.TOrderFromJson(Response.ContentAsString);
@@ -455,6 +465,7 @@ function TStoreService.GetOrderById(OrderId: Int64): TOrder;
455465
begin
456466
Request := CreateRequest('/store/order/{orderId}', 'GET');
457467
Request.AddUrlParam('orderId', IntToStr(OrderId));
468+
Request.AddHeader('Accept', 'application/json');
458469
Response := Request.Execute;
459470
CheckError(Response);
460471
Result := Converter.TOrderFromJson(Response.ContentAsString);
@@ -480,6 +491,8 @@ function TUserService.CreateUser(Body: TUser): TUser;
480491
begin
481492
Request := CreateRequest('/user', 'POST');
482493
Request.AddBody(Converter.TUserToJson(Body));
494+
Request.AddHeader('Content-Type', 'application/json');
495+
Request.AddHeader('Accept', 'application/json');
483496
Response := Request.Execute;
484497
CheckError(Response);
485498
Result := Converter.TUserFromJson(Response.ContentAsString);
@@ -492,6 +505,8 @@ function TUserService.CreateUsersWithListInput(Body: TUserList): TUser;
492505
begin
493506
Request := CreateRequest('/user/createWithList', 'POST');
494507
Request.AddBody(Converter.TUserListToJson(Body));
508+
Request.AddHeader('Content-Type', 'application/json');
509+
Request.AddHeader('Accept', 'application/json');
495510
Response := Request.Execute;
496511
CheckError(Response);
497512
Result := Converter.TUserFromJson(Response.ContentAsString);
@@ -505,6 +520,7 @@ function TUserService.LoginUser(Username: string; Password: string): string;
505520
Request := CreateRequest('/user/login', 'GET');
506521
Request.AddQueryParam('username', Username);
507522
Request.AddQueryParam('password', Password);
523+
Request.AddHeader('Accept', 'application/json');
508524
Response := Request.Execute;
509525
CheckError(Response);
510526
Result := Converter.stringFromJson(Response.ContentAsString);
@@ -527,6 +543,7 @@ function TUserService.GetUserByName(Username: string): TUser;
527543
begin
528544
Request := CreateRequest('/user/{username}', 'GET');
529545
Request.AddUrlParam('username', Username);
546+
Request.AddHeader('Accept', 'application/json');
530547
Response := Request.Execute;
531548
CheckError(Response);
532549
Result := Converter.TUserFromJson(Response.ContentAsString);
@@ -540,6 +557,7 @@ procedure TUserService.UpdateUser(Username: string; Body: TUser);
540557
Request := CreateRequest('/user/{username}', 'PUT');
541558
Request.AddUrlParam('username', Username);
542559
Request.AddBody(Converter.TUserToJson(Body));
560+
Request.AddHeader('Content-Type', 'application/json');
543561
Response := Request.Execute;
544562
CheckError(Response);
545563
end;

Tests/TestPetStore3Client.pas

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
unit TestPetStore3Client;
2+
3+
interface
4+
5+
uses
6+
SysUtils, TestFramework, PetStore3Client, PetStore3Dtos, OpenApiRest;
7+
8+
type
9+
TPetStore3ClientTests = class(TTestCase)
10+
published
11+
procedure CreateAndGetPet;
12+
end;
13+
14+
implementation
15+
16+
{ TPetStore3ClientTests }
17+
18+
procedure TPetStore3ClientTests.CreateAndGetPet;
19+
const
20+
PetId = 61341;
21+
CategoryId = 61341;
22+
TagId = 61341;
23+
var
24+
Pet: TPet;
25+
Tag: TTag;
26+
Client: IPetStore3Client;
27+
begin
28+
Client := TPetStore3Client.Create;
29+
30+
// Create the pet
31+
Pet := TPet.Create;
32+
try
33+
Pet.Id := PetId;
34+
Pet.Name := 'Josephine';
35+
Pet.Status := 'available';
36+
37+
Pet.Category := TCategory.Create;
38+
Pet.Category.Id := CategoryId;
39+
Pet.Category.Name := 'Terrier Dogs';
40+
41+
Pet.Tags := TTagList.Create;
42+
Tag := TTag.Create;
43+
Tag.Id := TagId;
44+
Tag.Name := 'Terrier';
45+
Pet.Tags.Add(Tag);
46+
47+
Pet.PhotoUrls.Add('http://dummy.com/dog.png');
48+
Client.Pet.AddPet(Pet);
49+
finally
50+
Pet.Free;
51+
end;
52+
53+
// Now pet should exist
54+
Pet := Client.Pet.GetPetById(PetId);
55+
try
56+
Check(Pet <> nil, Format('Pet %d not found', [PetId]));
57+
CheckEquals(PetId, Pet.Id);
58+
CheckEquals('Josephine', Pet.Name);
59+
CheckEquals('available', Pet.Status);
60+
61+
Check(Pet.Category <> nil, 'Category is nil');
62+
CheckEquals(CategoryId, Pet.Category.Id);
63+
CheckEquals('Terrier Dogs', Pet.Category.Name);
64+
65+
Check(Pet.Tags <> nil, 'Tags is nil');
66+
CheckEquals(1, Pet.Tags.Count);
67+
CheckEquals(TagId, Pet.Tags[0].Id);
68+
Checkequals('Terrier', Pet.Tags[0].Name);
69+
70+
CheckEquals(1, Pet.PhotoUrls.Count);
71+
CheckEquals('http://dummy.com/dog.png', Pet.PhotoUrls[0]);
72+
finally
73+
Pet.Free;
74+
end;
75+
76+
// Delete the newly created pet
77+
Client.Pet.DeletePet('special-key', PetId);
78+
79+
// Make sure pet does not exist anymore
80+
try
81+
Pet := Client.Pet.GetPetById(PetId);
82+
Pet.Free;
83+
Check(False, 'Exception not raised');
84+
except
85+
on E: EOpenApiClientException do
86+
CheckEquals(404, E.Response.StatusCode);
87+
end;
88+
end;
89+
90+
initialization
91+
RegisterTest(TPetStore3ClientTests.Suite);
92+
93+
end.

0 commit comments

Comments
 (0)