@@ -80,28 +80,49 @@ function TOpenApiAnalyzer.BuildMetaMethod(Method: TMetaMethod; const Path: strin
80
80
ListType: TListType;
81
81
ConsumesJson: Boolean;
82
82
ProducesJson: Boolean;
83
+ ContentPair: TPair<string, TMediaType>;
84
+ BodyParamName: string;
83
85
begin
86
+ if (Operation.Servers <> nil ) and (Operation.Servers.Count > 1 ) then
87
+ raise Exception.Create(' Multiple servers are not yet supported' );
88
+
84
89
Result := False;
85
90
try
86
- // ConsumesJson := Operation.Consumes.Contains(MimeTypeJson);
87
- // ProducesJson := Operation.Produces.Contains(MimeTypeJson);
88
-
89
91
Method.HttpMethod := HttpMethod;
90
92
Method.UrlPath := Path;
91
93
92
- // for Param in Operation.Parameters do
93
- // begin
94
- // if Param.InBody then
95
- // if ConsumesJson then
96
- // Method.Consumes := MimeTypeJson
97
- // else
98
- // raise EOpenApiAnalyzerException.CreateFmt('Body parameter %s is present by method does not consume JSON', [Param.Name]);
99
- //
100
- // MetaParam := TMetaParam.Create;
101
- // Method.Params.Add(MetaParam);
102
- // MetaParam.Description := Param.Description;
103
- // BuildMetaParam(MetaParam, Param, Method.CodeName);
104
- // end;
94
+ if Operation.Parameters <> nil then
95
+ for Param in Operation.Parameters do
96
+ begin
97
+ MetaParam := TMetaParam.Create;
98
+ Method.Params.Add(MetaParam);
99
+ MetaParam.Description := Param.Description;
100
+ BuildMetaParam(MetaParam, Param, Method.CodeName);
101
+ end ;
102
+
103
+ if Operation.RequestBody <> nil then
104
+ begin
105
+ ConsumesJson := False;
106
+ if Operation.RequestBody.Content <> nil then
107
+ for ContentPair in Operation.RequestBody.Content do
108
+ if SameText(MimeTypeJson, ContentPair.Key) then
109
+ begin
110
+ ConsumesJson := True;
111
+
112
+ // Add JSON body param
113
+ MetaParam := TMetaParam.Create;
114
+ Method.Params.Add(MetaParam);
115
+ MetaParam.Description := Operation.RequestBody.Description;
116
+
117
+ BodyParamName := ' Body' ;
118
+ MetaParam.RestName := BodyParamName;
119
+ MetaParam.CodeName := ProcessNaming(MetaParam.RestName, Options.ServiceOptions.ParamNaming);
120
+ MetaParam.ParamType := MetaTypeFromSchema(ContentPair.Value .Schema, Method.CodeName + ' Input' , TListType.ltAuto);
121
+ MetaParam.Location := TParamLocation.plBody;
122
+ end ;
123
+ if not ConsumesJson then
124
+ raise EOpenApiAnalyzerException.CreateFmt(' Request body is present in method "%s" but does not consume JSON' , [Method.UrlPath]);
125
+ end ;
105
126
106
127
ResponseType := nil ;
107
128
// for ResponseItem in Operation.Responses do
@@ -139,37 +160,27 @@ function TOpenApiAnalyzer.BuildMetaMethod(Method: TMetaMethod; const Path: strin
139
160
140
161
procedure TOpenApiAnalyzer.BuildMetaParam (MetaParam: TMetaParam; Param: TParameter; const MethodName: string);
141
162
begin
142
- // MetaParam.RestName := Param.Name;
143
- // MetaParam.CodeName := ProcessNaming(Param.Name, Options.ServiceOptions.ParamNaming);
144
- // case Param.&In of
145
- // Body:
146
- // begin
147
- // MetaParam.ParamType := MetaTypeFromSchema(Param.Schema, MethodName + 'Input', TListType.ltAuto);
148
- // MetaParam.Location := TParamLocation.plBody;
149
- // end;
150
- // Query:
151
- // begin
152
- // MetaParam.ParamType := MetaTypeFromSchema(Param.Schema, MethodName + Param.Name, TListType.ltAuto);
153
- // MetaParam.Location := TParamLocation.plQuery;
154
- // end;
155
- // Path:
156
- // begin
157
- // MetaParam.ParamType := MetaTypeFromSchema(Param.Schema, MethodName + Param.Name, TListType.ltAuto);
158
- // MetaParam.Location := TParamLocation.plUrl;
159
- // end;
160
- // FormData:
161
- // begin
162
- // MetaParam.ParamType := MetaTypeFromSchema(Param.Schema, MethodName + Param.Name, TListType.ltAuto);
163
- // MetaParam.Location := TParamLocation.plForm;
164
- // end;
165
- // Header:
166
- // begin
167
- // MetaParam.ParamType := MetaTypeFromSchema(Param.Schema, MethodName + Param.Name, TListType.ltAuto);
168
- // MetaParam.Location := TParamLocation.plHeader;
169
- // end;
170
- // else
171
- // raise EOpenApiAnalyzerException.CreateFmt('Unsupported parameter type: %s', [GetEnumName(TypeInfo(TLocation), Ord(Param.&In))]);
172
- // end;
163
+ MetaParam.RestName := Param.Name ;
164
+ MetaParam.CodeName := ProcessNaming(Param.Name , Options.ServiceOptions.ParamNaming);
165
+ case Param.&In of
166
+ Query:
167
+ begin
168
+ MetaParam.ParamType := MetaTypeFromSchema(Param.Schema, MethodName + Param.Name , TListType.ltAuto);
169
+ MetaParam.Location := TParamLocation.plQuery;
170
+ end ;
171
+ Path:
172
+ begin
173
+ MetaParam.ParamType := MetaTypeFromSchema(Param.Schema, MethodName + Param.Name , TListType.ltAuto);
174
+ MetaParam.Location := TParamLocation.plUrl;
175
+ end ;
176
+ Header:
177
+ begin
178
+ MetaParam.ParamType := MetaTypeFromSchema(Param.Schema, MethodName + Param.Name , TListType.ltAuto);
179
+ MetaParam.Location := TParamLocation.plHeader;
180
+ end ;
181
+ else
182
+ raise EOpenApiAnalyzerException.CreateFmt(' Unsupported parameter type: %s' , [GetEnumName(TypeInfo(TLocation), Ord(Param.&In ))]);
183
+ end ;
173
184
end ;
174
185
175
186
procedure TOpenApiAnalyzer.DoSolveServiceOperation (var ServiceName, ServiceDescription, OperationName: string; const Path: string;
@@ -216,7 +227,7 @@ function TOpenApiAnalyzer.GetBaseUrl: string;
216
227
if Document.Servers <> nil then
217
228
begin
218
229
if Document.Servers.Count > 1 then
219
- raise EOpenApiAnalyzerException .Create(' Multiple servers are not supported' );
230
+ raise Exception .Create(' Multiple servers are not yet supported' );
220
231
221
232
Server := Document.Servers[0 ];
222
233
Result := Server.Url;
@@ -229,7 +240,7 @@ function TOpenApiAnalyzer.GetBaseUrl: string;
229
240
if Pos(' ://' , Result) = 0 then
230
241
begin
231
242
if Options.DocumentUrl = ' ' then
232
- raise EOpenApiAnalyzerException .Create(' Cannot determine the URL of the API spe, please provide it using DocumentUrl param' );
243
+ raise Exception .Create(' Cannot determine the URL of the API spe, please provide it using DocumentUrl param' );
233
244
Result := Options.DocumentUrl + Result;
234
245
end ;
235
246
end ;
@@ -240,7 +251,7 @@ function TOpenApiAnalyzer.MetaTypeFromReference(RefSchema: TReferenceSchema; con
240
251
ReferencedSchema: TJsonSchema;
241
252
begin
242
253
if not Document.Components.Schemas.TryGetValue(RefSchema.SchemaName, ReferencedSchema) then
243
- raise EOpenApiAnalyzerException .CreateFmt(' Reference not in schema "%s"' , [RefSchema.Ref]);
254
+ raise Exception .CreateFmt(' Could not solve reference "%s"' , [RefSchema.Ref]);
244
255
245
256
// if it's object, then just reference the type. Otherwise, use the referenced type inline
246
257
if ReferencedSchema is TObjectSchema then
@@ -298,7 +309,7 @@ procedure TOpenApiAnalyzer.ProcessOperation(const Path: string; PathItem: TPathI
298
309
procedure TOpenApiAnalyzer.ProcessPathItem (const Path: string; PathItem: TPathItem);
299
310
begin
300
311
if (Document.Servers <> nil ) and (Document.Servers.Count > 1 ) then
301
- raise EOpenApiAnalyzerException .Create(' Multiple servers are not supported. ' );
312
+ raise Exception .Create(' Multiple servers are not yet supported' );
302
313
303
314
ProcessOperation(Path, PathItem, PathItem.Get, ' GET' );
304
315
ProcessOperation(Path, PathItem, PathItem.Put, ' PUT' );
0 commit comments