Skip to content

Commit 188647e

Browse files
committed
Now importing endpoints with empty OperationId and with global Consumes tag
- Some Swagger documents did not have the OperationId for endpoints. Now for those cases the OpenAPI generator automatically creates a method name based on path and HTTP method. - Some Swagger documents did not have the Consumes tag specified for each endpoint, but just a single one global. The generator was not taking the global Consumes tag into account, ignoring those endpoints without the tag.
1 parent 77a4a59 commit 188647e

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Source/OpenApiGen.CustomAnalyzer.pas

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
interface
44

55
uses
6-
Generics.Collections, SysUtils, Character,
6+
Generics.Collections, SysUtils, Character, StrUtils,
77
Bcl.Logging,
88
XData.JSchema.Classes,
99
OpenApiGen.Options,
@@ -20,7 +20,6 @@ TOpenApiCustomAnalyzer = class
2020
FMetaClient: TMetaClient;
2121
FOptions: TBuilderOptions;
2222
FOwnsOptions: Boolean;
23-
function ToId(const S: string): string; virtual;
2423
strict private
2524
FOnGetMethodName: TGetIdentifierProc;
2625
FOnGetTypeName: TGetIdentifierProc;
@@ -30,6 +29,9 @@ TOpenApiCustomAnalyzer = class
3029
FOnGetInterfaceName: TGetIdentifierProc;
3130
FOnGetServiceClassName: TGetIdentifierProc;
3231
strict protected
32+
function ToId(const S: string): string; virtual;
33+
function BuildOperationName(const Path, HttpMethod: string): string;
34+
3335
function ProcessNaming(const S: string; Options: TNamingOptions): string;
3436
procedure DoGetPropName(var PropName: string; const Original: string);
3537
procedure DoGetFieldName(var FieldName: string; const Original: string);
@@ -66,6 +68,8 @@ TOpenApiCustomAnalyzer = class
6668
EOpenApiAnalyzerException = class(Exception)
6769
end;
6870

71+
function ToPascalCase(const S: string): string;
72+
6973
implementation
7074

7175
uses
@@ -103,6 +107,26 @@ function ToPascalCase(const S: string): string;
103107

104108
{ TOpenApiCustomAnalyzer }
105109

110+
function TOpenApiCustomAnalyzer.BuildOperationName(const Path, HttpMethod: string): string;
111+
var
112+
TempName: string;
113+
Parts: TArray<string>;
114+
Part: string;
115+
begin
116+
TempName := Path;
117+
TempName := StringReplace(TempName, '{', '', [rfReplaceAll]);
118+
TempName := StringReplace(TempName, '}', '', [rfReplaceAll]);
119+
Parts := SplitString(TempName + '/' + LowerCase(HttpMethod), '/');
120+
if TempName = '/' then
121+
Result := 'Root'
122+
else
123+
Result := '';
124+
for Part in Parts do
125+
if Part <> '' then
126+
Result := Result + ToPascalCase(Part);
127+
Result := ToId(Result);
128+
end;
129+
106130
constructor TOpenApiCustomAnalyzer.Create(Options: TBuilderOptions; AOwnsOptions: Boolean);
107131
begin
108132
inherited Create;

Source/OpenApiGen.V2.Analyzer.pas

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ function TOpenApiAnalyzer.BuildMetaMethod(Method: TMetaMethod; const Path: strin
7878
Result := False;
7979
try
8080
ConsumesJson := Operation.Consumes.Contains(MimeTypeJson);
81+
if not ConsumesJson and (Operation.Consumes.Count = 0) then
82+
ConsumesJson := Document.Consumes.Contains(MimeTypeJson);
8183
ProducesJson := Operation.Produces.Contains(MimeTypeJson);
84+
if not ProducesJson and (Operation.Produces.Count = 0) then
85+
ProducesJson := Document.Produces.Contains(MimeTypeJson);
8286

8387
Method.HttpMethod := HttpMethod;
8488
Method.UrlPath := Path;
@@ -261,6 +265,10 @@ procedure TOpenApiAnalyzer.ProcessOperation(const Path: string; PathItem: TPathI
261265
// Service Mode
262266
DoSolveServiceOperation(ServiceName, ServiceDescription, OperationName, Path, PathItem, Operation);
263267

268+
// Auto-generate operation name
269+
if OperationName = '' then
270+
OperationName := BuildOperationName(Path, HttpMethod);
271+
264272
// Find or create the service
265273
DoGetServiceName(ServiceName, ServiceName);
266274
Service := MetaClient.FindService(ServiceName);

Source/OpenApiGen.V3.Analyzer.pas

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ procedure TOpenApiAnalyzer.ProcessOperation(const Path: string; PathItem: TPathI
329329
// Service Mode
330330
DoSolveServiceOperation(ServiceName, ServiceDescription, OperationName, Path, PathItem, Operation);
331331

332+
// Auto-generate operation name
333+
if OperationName = '' then
334+
OperationName := BuildOperationName(Path, HttpMethod);
335+
332336
// Find or create the service
333337
DoGetServiceName(ServiceName, ServiceName);
334338
Service := MetaClient.FindService(ServiceName);

0 commit comments

Comments
 (0)