Skip to content

Commit f17e994

Browse files
committed
Response bodies now taken into account.
1 parent 7ae0845 commit f17e994

File tree

4 files changed

+205
-55
lines changed

4 files changed

+205
-55
lines changed

Source/OpenApiGen.V3.Analyzer.pas

+25-19
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,33 @@ function TOpenApiAnalyzer.BuildMetaMethod(Method: TMetaMethod; const Path: strin
124124
raise EOpenApiAnalyzerException.CreateFmt('Request body is present in method "%s" but does not consume JSON', [Method.UrlPath]);
125125
end;
126126

127+
ProducesJson := False;
127128
ResponseType := nil;
128-
// for ResponseItem in Operation.Responses do
129-
// if ((ResponseItem.Key = 'default') or (StrToInt(ResponseItem.Key) < 300)) and Assigned(ResponseItem.Value.Schema) then
130-
// begin
131-
// if Options.XDataService then
132-
// ListType := TListType.ltArray
133-
// else
134-
// ListType := TListType.ltAuto;
135-
// TargetResponseType := MetaTypeFromSchema(ResponseItem.Value.Schema, Method.CodeName + 'Output', ListType);
136-
// if ResponseType = nil then
137-
// ResponseType := TargetResponseType
138-
// else
139-
// if ResponseType.TypeName <> TargetResponseType.TypeName then
140-
// raise EOpenApiAnalyzerException.CreateFmt('Ambiguous response types: %s and %s', [ResponseType.TypeName, TargetResponseType.TypeName]);
141-
// end;
142-
// Method.ReturnType := ResponseType;
129+
for ResponseItem in Operation.Responses do
130+
if ((ResponseItem.Key = 'default') or (StrToInt(ResponseItem.Key) < 300)) and Assigned(ResponseItem.Value.Content) then
131+
begin
132+
for ContentPair in ResponseItem.Value.Content do
133+
if SameText(MimeTypeJson, ContentPair.Key) then
134+
begin
135+
ProducesJson := True;
136+
137+
if Options.XDataService then
138+
ListType := TListType.ltArray
139+
else
140+
ListType := TListType.ltAuto;
141+
142+
TargetResponseType := MetaTypeFromSchema(ContentPair.Value.Schema, Method.CodeName + 'Output', ListType);
143+
if ResponseType = nil then
144+
ResponseType := TargetResponseType
145+
else
146+
if ResponseType.TypeName <> TargetResponseType.TypeName then
147+
raise EOpenApiAnalyzerException.CreateFmt('Ambiguous response types: %s and %s', [ResponseType.TypeName, TargetResponseType.TypeName]);
148+
end;
149+
end;
150+
Method.ReturnType := ResponseType;
143151
if (ResponseType <> nil) and not ResponseType.IsBinary then
144-
if ProducesJson then
145-
Method.Produces := MimeTypeJson
146-
else
147-
raise EOpenApiAnalyzerException.Create('Method returns data be method does not produce JSON');
152+
if not ProducesJson then
153+
raise EOpenApiAnalyzerException.CreateFmt('Response body is present in method "%s" but does not produce JSON', [Method.UrlPath]);
148154
Result := True;
149155
except
150156
on E: EOpenApiAnalyzerException do

Tests/PetStore3/PetStore3Client.pas

+48-36
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ TRestService = class(TCustomRestService)
3636
/// <remarks>
3737
/// Update an existing pet by Id
3838
/// </remarks>
39-
procedure UpdatePet(Body: TPet);
39+
function UpdatePet(Body: TPet): TPet;
4040
/// <summary>
4141
/// Add a new pet to the store
4242
/// </summary>
@@ -46,7 +46,7 @@ TRestService = class(TCustomRestService)
4646
/// <remarks>
4747
/// Add a new pet to the store
4848
/// </remarks>
49-
procedure AddPet(Body: TPet);
49+
function AddPet(Body: TPet): TPet;
5050
/// <summary>
5151
/// Finds Pets by status
5252
/// </summary>
@@ -56,7 +56,7 @@ TRestService = class(TCustomRestService)
5656
/// <remarks>
5757
/// Multiple status values can be provided with comma separated strings
5858
/// </remarks>
59-
procedure FindPetsByStatus(Status: string);
59+
function FindPetsByStatus(Status: string): TPetList;
6060
/// <summary>
6161
/// Finds Pets by tags
6262
/// </summary>
@@ -66,7 +66,7 @@ TRestService = class(TCustomRestService)
6666
/// <remarks>
6767
/// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
6868
/// </remarks>
69-
procedure FindPetsByTags(Tags: stringArray);
69+
function FindPetsByTags(Tags: stringArray): TPetList;
7070
/// <summary>
7171
/// Find pet by ID
7272
/// </summary>
@@ -76,7 +76,7 @@ TRestService = class(TCustomRestService)
7676
/// <remarks>
7777
/// Returns a single pet
7878
/// </remarks>
79-
procedure GetPetById(PetId: Int64);
79+
function GetPetById(PetId: Int64): TPet;
8080
/// <summary>
8181
/// Updates a pet in the store with form data
8282
/// </summary>
@@ -104,23 +104,23 @@ TPetService = class(TRestService, IPetService)
104104
/// <param name="Body">
105105
/// Update an existent pet in the store
106106
/// </param>
107-
procedure UpdatePet(Body: TPet);
107+
function UpdatePet(Body: TPet): TPet;
108108
/// <param name="Body">
109109
/// Create a new pet in the store
110110
/// </param>
111-
procedure AddPet(Body: TPet);
111+
function AddPet(Body: TPet): TPet;
112112
/// <param name="Status">
113113
/// Status values that need to be considered for filter
114114
/// </param>
115-
procedure FindPetsByStatus(Status: string);
115+
function FindPetsByStatus(Status: string): TPetList;
116116
/// <param name="Tags">
117117
/// Tags to filter by
118118
/// </param>
119-
procedure FindPetsByTags(Tags: stringArray);
119+
function FindPetsByTags(Tags: stringArray): TPetList;
120120
/// <param name="PetId">
121121
/// ID of pet to return
122122
/// </param>
123-
procedure GetPetById(PetId: Int64);
123+
function GetPetById(PetId: Int64): TPet;
124124
/// <param name="PetId">
125125
/// ID of pet that needs to be updated
126126
/// </param>
@@ -148,14 +148,14 @@ TPetService = class(TRestService, IPetService)
148148
/// <remarks>
149149
/// Returns a map of status codes to quantities
150150
/// </remarks>
151-
procedure GetInventory;
151+
function GetInventory: TGetInventoryOutput;
152152
/// <summary>
153153
/// Place an order for a pet
154154
/// </summary>
155155
/// <remarks>
156156
/// Place a new order in the store
157157
/// </remarks>
158-
procedure PlaceOrder(Body: TOrder);
158+
function PlaceOrder(Body: TOrder): TOrder;
159159
/// <summary>
160160
/// Find purchase order by ID
161161
/// </summary>
@@ -165,7 +165,7 @@ TPetService = class(TRestService, IPetService)
165165
/// <remarks>
166166
/// For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions.
167167
/// </remarks>
168-
procedure GetOrderById(OrderId: Int64);
168+
function GetOrderById(OrderId: Int64): TOrder;
169169
/// <summary>
170170
/// Delete purchase order by ID
171171
/// </summary>
@@ -180,12 +180,12 @@ TPetService = class(TRestService, IPetService)
180180

181181
TStoreService = class(TRestService, IStoreService)
182182
public
183-
procedure GetInventory;
184-
procedure PlaceOrder(Body: TOrder);
183+
function GetInventory: TGetInventoryOutput;
184+
function PlaceOrder(Body: TOrder): TOrder;
185185
/// <param name="OrderId">
186186
/// ID of order that needs to be fetched
187187
/// </param>
188-
procedure GetOrderById(OrderId: Int64);
188+
function GetOrderById(OrderId: Int64): TOrder;
189189
/// <param name="OrderId">
190190
/// ID of the order that needs to be deleted
191191
/// </param>
@@ -206,14 +206,14 @@ TStoreService = class(TRestService, IStoreService)
206206
/// <remarks>
207207
/// This can only be done by the logged in user.
208208
/// </remarks>
209-
procedure CreateUser(Body: TUser);
209+
function CreateUser(Body: TUser): TUser;
210210
/// <summary>
211211
/// Creates list of users with given input array
212212
/// </summary>
213213
/// <remarks>
214214
/// Creates list of users with given input array
215215
/// </remarks>
216-
procedure CreateUsersWithListInput(Body: TUserList);
216+
function CreateUsersWithListInput(Body: TUserList): TUser;
217217
/// <summary>
218218
/// Logs user into the system
219219
/// </summary>
@@ -223,7 +223,7 @@ TStoreService = class(TRestService, IStoreService)
223223
/// <param name="Password">
224224
/// The password for login in clear text
225225
/// </param>
226-
procedure LoginUser(Username: string; Password: string);
226+
function LoginUser(Username: string; Password: string): string;
227227
/// <summary>
228228
/// Logs out current logged in user session
229229
/// </summary>
@@ -234,7 +234,7 @@ TStoreService = class(TRestService, IStoreService)
234234
/// <param name="Username">
235235
/// The name that needs to be fetched. Use user1 for testing.
236236
/// </param>
237-
procedure GetUserByName(Username: string);
237+
function GetUserByName(Username: string): TUser;
238238
/// <summary>
239239
/// Update user
240240
/// </summary>
@@ -265,20 +265,20 @@ TUserService = class(TRestService, IUserService)
265265
/// <param name="Body">
266266
/// Created user object
267267
/// </param>
268-
procedure CreateUser(Body: TUser);
269-
procedure CreateUsersWithListInput(Body: TUserList);
268+
function CreateUser(Body: TUser): TUser;
269+
function CreateUsersWithListInput(Body: TUserList): TUser;
270270
/// <param name="Username">
271271
/// The user name for login
272272
/// </param>
273273
/// <param name="Password">
274274
/// The password for login in clear text
275275
/// </param>
276-
procedure LoginUser(Username: string; Password: string);
276+
function LoginUser(Username: string; Password: string): string;
277277
procedure LogoutUser;
278278
/// <param name="Username">
279279
/// The name that needs to be fetched. Use user1 for testing.
280280
/// </param>
281-
procedure GetUserByName(Username: string);
281+
function GetUserByName(Username: string): TUser;
282282
/// <param name="Username">
283283
/// name that need to be deleted
284284
/// </param>
@@ -336,7 +336,7 @@ function TRestService.Converter: TJsonConverter;
336336

337337
{ TPetService }
338338

339-
procedure TPetService.UpdatePet(Body: TPet);
339+
function TPetService.UpdatePet(Body: TPet): TPet;
340340
var
341341
Request: IRestRequest;
342342
Response: IRestResponse;
@@ -345,9 +345,10 @@ procedure TPetService.UpdatePet(Body: TPet);
345345
Request.AddBody(Converter.TPetToJson(Body));
346346
Response := Request.Execute;
347347
CheckError(Response);
348+
Result := Converter.TPetFromJson(Response.ContentAsString);
348349
end;
349350

350-
procedure TPetService.AddPet(Body: TPet);
351+
function TPetService.AddPet(Body: TPet): TPet;
351352
var
352353
Request: IRestRequest;
353354
Response: IRestResponse;
@@ -356,9 +357,10 @@ procedure TPetService.AddPet(Body: TPet);
356357
Request.AddBody(Converter.TPetToJson(Body));
357358
Response := Request.Execute;
358359
CheckError(Response);
360+
Result := Converter.TPetFromJson(Response.ContentAsString);
359361
end;
360362

361-
procedure TPetService.FindPetsByStatus(Status: string);
363+
function TPetService.FindPetsByStatus(Status: string): TPetList;
362364
var
363365
Request: IRestRequest;
364366
Response: IRestResponse;
@@ -367,9 +369,10 @@ procedure TPetService.FindPetsByStatus(Status: string);
367369
Request.AddQueryParam('status', Status);
368370
Response := Request.Execute;
369371
CheckError(Response);
372+
Result := Converter.TPetListFromJson(Response.ContentAsString);
370373
end;
371374

372-
procedure TPetService.FindPetsByTags(Tags: stringArray);
375+
function TPetService.FindPetsByTags(Tags: stringArray): TPetList;
373376
var
374377
Request: IRestRequest;
375378
I: Integer;
@@ -380,9 +383,10 @@ procedure TPetService.FindPetsByTags(Tags: stringArray);
380383
Request.AddQueryParam('tags', Tags[I]);
381384
Response := Request.Execute;
382385
CheckError(Response);
386+
Result := Converter.TPetListFromJson(Response.ContentAsString);
383387
end;
384388

385-
procedure TPetService.GetPetById(PetId: Int64);
389+
function TPetService.GetPetById(PetId: Int64): TPet;
386390
var
387391
Request: IRestRequest;
388392
Response: IRestResponse;
@@ -391,6 +395,7 @@ procedure TPetService.GetPetById(PetId: Int64);
391395
Request.AddUrlParam('petId', IntToStr(PetId));
392396
Response := Request.Execute;
393397
CheckError(Response);
398+
Result := Converter.TPetFromJson(Response.ContentAsString);
394399
end;
395400

396401
procedure TPetService.UpdatePetWithForm(PetId: Int64; Name: string; Status: string);
@@ -420,17 +425,18 @@ procedure TPetService.DeletePet(ApiKey: string; PetId: Int64);
420425

421426
{ TStoreService }
422427

423-
procedure TStoreService.GetInventory;
428+
function TStoreService.GetInventory: TGetInventoryOutput;
424429
var
425430
Request: IRestRequest;
426431
Response: IRestResponse;
427432
begin
428433
Request := CreateRequest('/store/inventory', 'GET');
429434
Response := Request.Execute;
430435
CheckError(Response);
436+
Result := Converter.TGetInventoryOutputFromJson(Response.ContentAsString);
431437
end;
432438

433-
procedure TStoreService.PlaceOrder(Body: TOrder);
439+
function TStoreService.PlaceOrder(Body: TOrder): TOrder;
434440
var
435441
Request: IRestRequest;
436442
Response: IRestResponse;
@@ -439,9 +445,10 @@ procedure TStoreService.PlaceOrder(Body: TOrder);
439445
Request.AddBody(Converter.TOrderToJson(Body));
440446
Response := Request.Execute;
441447
CheckError(Response);
448+
Result := Converter.TOrderFromJson(Response.ContentAsString);
442449
end;
443450

444-
procedure TStoreService.GetOrderById(OrderId: Int64);
451+
function TStoreService.GetOrderById(OrderId: Int64): TOrder;
445452
var
446453
Request: IRestRequest;
447454
Response: IRestResponse;
@@ -450,6 +457,7 @@ procedure TStoreService.GetOrderById(OrderId: Int64);
450457
Request.AddUrlParam('orderId', IntToStr(OrderId));
451458
Response := Request.Execute;
452459
CheckError(Response);
460+
Result := Converter.TOrderFromJson(Response.ContentAsString);
453461
end;
454462

455463
procedure TStoreService.DeleteOrder(OrderId: Int64);
@@ -465,7 +473,7 @@ procedure TStoreService.DeleteOrder(OrderId: Int64);
465473

466474
{ TUserService }
467475

468-
procedure TUserService.CreateUser(Body: TUser);
476+
function TUserService.CreateUser(Body: TUser): TUser;
469477
var
470478
Request: IRestRequest;
471479
Response: IRestResponse;
@@ -474,9 +482,10 @@ procedure TUserService.CreateUser(Body: TUser);
474482
Request.AddBody(Converter.TUserToJson(Body));
475483
Response := Request.Execute;
476484
CheckError(Response);
485+
Result := Converter.TUserFromJson(Response.ContentAsString);
477486
end;
478487

479-
procedure TUserService.CreateUsersWithListInput(Body: TUserList);
488+
function TUserService.CreateUsersWithListInput(Body: TUserList): TUser;
480489
var
481490
Request: IRestRequest;
482491
Response: IRestResponse;
@@ -485,9 +494,10 @@ procedure TUserService.CreateUsersWithListInput(Body: TUserList);
485494
Request.AddBody(Converter.TUserListToJson(Body));
486495
Response := Request.Execute;
487496
CheckError(Response);
497+
Result := Converter.TUserFromJson(Response.ContentAsString);
488498
end;
489499

490-
procedure TUserService.LoginUser(Username: string; Password: string);
500+
function TUserService.LoginUser(Username: string; Password: string): string;
491501
var
492502
Request: IRestRequest;
493503
Response: IRestResponse;
@@ -497,6 +507,7 @@ procedure TUserService.LoginUser(Username: string; Password: string);
497507
Request.AddQueryParam('password', Password);
498508
Response := Request.Execute;
499509
CheckError(Response);
510+
Result := Converter.stringFromJson(Response.ContentAsString);
500511
end;
501512

502513
procedure TUserService.LogoutUser;
@@ -509,7 +520,7 @@ procedure TUserService.LogoutUser;
509520
CheckError(Response);
510521
end;
511522

512-
procedure TUserService.GetUserByName(Username: string);
523+
function TUserService.GetUserByName(Username: string): TUser;
513524
var
514525
Request: IRestRequest;
515526
Response: IRestResponse;
@@ -518,6 +529,7 @@ procedure TUserService.GetUserByName(Username: string);
518529
Request.AddUrlParam('username', Username);
519530
Response := Request.Execute;
520531
CheckError(Response);
532+
Result := Converter.TUserFromJson(Response.ContentAsString);
521533
end;
522534

523535
procedure TUserService.UpdateUser(Username: string; Body: TUser);

0 commit comments

Comments
 (0)