Skip to content

Commit f2a18b7

Browse files
committed
Implementing specific v3 handling of parameters and request body
1 parent f805115 commit f2a18b7

File tree

6 files changed

+370
-116
lines changed

6 files changed

+370
-116
lines changed

Source/OpenApiGen.CustomAnalyzer.pas

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ function TOpenApiCustomAnalyzer.MetaTypeFromObject(const Name: string; Schema: T
240240
function TOpenApiCustomAnalyzer.MetaTypeFromReference(RefSchema: TReferenceSchema; const DefaultTypeName: string;
241241
ListType: TListType): IMetaType;
242242
begin
243-
244243
end;
245244

246245
function TOpenApiCustomAnalyzer.MetaTypeFromSchema(Schema: TJsonSchema; const DefaultTypeName: string;

Source/OpenApiGen.V2.Analyzer.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function TOpenApiAnalyzer.BuildMetaMethod(Method: TMetaMethod; const Path: strin
9191
if ConsumesJson then
9292
Method.Consumes := MimeTypeJson
9393
else
94-
raise EOpenApiAnalyzerException.CreateFmt('Body parameter %s is present by method does not consume JSON', [Param.Name]);
94+
raise EOpenApiAnalyzerException.CreateFmt('Body parameter %s is present but method does not consume JSON', [Param.Name]);
9595

9696
MetaParam := TMetaParam.Create;
9797
Method.Params.Add(MetaParam);

Source/OpenApiGen.V3.Analyzer.pas

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,49 @@ function TOpenApiAnalyzer.BuildMetaMethod(Method: TMetaMethod; const Path: strin
8080
ListType: TListType;
8181
ConsumesJson: Boolean;
8282
ProducesJson: Boolean;
83+
ContentPair: TPair<string, TMediaType>;
84+
BodyParamName: string;
8385
begin
86+
if (Operation.Servers <> nil) and (Operation.Servers.Count > 1) then
87+
raise Exception.Create('Multiple servers are not yet supported');
88+
8489
Result := False;
8590
try
86-
// ConsumesJson := Operation.Consumes.Contains(MimeTypeJson);
87-
// ProducesJson := Operation.Produces.Contains(MimeTypeJson);
88-
8991
Method.HttpMethod := HttpMethod;
9092
Method.UrlPath := Path;
9193

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;
105126

106127
ResponseType := nil;
107128
// for ResponseItem in Operation.Responses do
@@ -139,37 +160,27 @@ function TOpenApiAnalyzer.BuildMetaMethod(Method: TMetaMethod; const Path: strin
139160

140161
procedure TOpenApiAnalyzer.BuildMetaParam(MetaParam: TMetaParam; Param: TParameter; const MethodName: string);
141162
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;
173184
end;
174185

175186
procedure TOpenApiAnalyzer.DoSolveServiceOperation(var ServiceName, ServiceDescription, OperationName: string; const Path: string;
@@ -216,7 +227,7 @@ function TOpenApiAnalyzer.GetBaseUrl: string;
216227
if Document.Servers <> nil then
217228
begin
218229
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');
220231

221232
Server := Document.Servers[0];
222233
Result := Server.Url;
@@ -229,7 +240,7 @@ function TOpenApiAnalyzer.GetBaseUrl: string;
229240
if Pos('://', Result) = 0 then
230241
begin
231242
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');
233244
Result := Options.DocumentUrl + Result;
234245
end;
235246
end;
@@ -240,7 +251,7 @@ function TOpenApiAnalyzer.MetaTypeFromReference(RefSchema: TReferenceSchema; con
240251
ReferencedSchema: TJsonSchema;
241252
begin
242253
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]);
244255

245256
// if it's object, then just reference the type. Otherwise, use the referenced type inline
246257
if ReferencedSchema is TObjectSchema then
@@ -298,7 +309,7 @@ procedure TOpenApiAnalyzer.ProcessOperation(const Path: string; PathItem: TPathI
298309
procedure TOpenApiAnalyzer.ProcessPathItem(const Path: string; PathItem: TPathItem);
299310
begin
300311
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');
302313

303314
ProcessOperation(Path, PathItem, PathItem.Get, 'GET');
304315
ProcessOperation(Path, PathItem, PathItem.Put, 'PUT');

0 commit comments

Comments
 (0)