2024-09-10 23:42:24 +02:00
|
|
|
|
// ***************************************************************************
|
2019-10-09 23:14:56 +02:00
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2024-01-02 17:04:27 +01:00
|
|
|
|
// Copyright (c) 2010-2024 Daniele Teti and the DMVCFramework Team
|
2019-10-09 23:14:56 +02:00
|
|
|
|
//
|
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
|
//
|
|
|
|
|
// Collaborators on this file:
|
2024-01-02 17:04:27 +01:00
|
|
|
|
// Jo<4A>o Ant<6E>nio Duarte (https://github.com/joaoduarte19)
|
2019-10-09 23:14:56 +02:00
|
|
|
|
//
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
unit MVCFramework.Middleware.Swagger;
|
|
|
|
|
|
2020-12-10 19:25:12 +01:00
|
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
MVCFramework,
|
2021-12-31 18:00:54 +01:00
|
|
|
|
MVCFramework.Logger,
|
2019-07-27 20:23:48 +02:00
|
|
|
|
Swag.Doc,
|
2019-08-01 22:31:59 +02:00
|
|
|
|
MVCFramework.Swagger.Commons,
|
|
|
|
|
Swag.Doc.SecurityDefinition,
|
|
|
|
|
Swag.Common.Types,
|
2022-12-25 19:46:07 +01:00
|
|
|
|
System.JSON, MVCFramework.Commons;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
TMVCSwaggerMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
|
|
|
|
private
|
2019-11-27 20:27:07 +01:00
|
|
|
|
fEngine: TMVCEngine;
|
|
|
|
|
fSwaggerInfo: TMVCSwaggerInfo;
|
|
|
|
|
fSwagDocURL: string;
|
|
|
|
|
fJWTDescription: string;
|
|
|
|
|
fEnableBasicAuthentication: Boolean;
|
2024-09-03 13:49:28 +02:00
|
|
|
|
fEnableBearerAuthentication: Boolean;
|
2021-10-22 01:18:49 +02:00
|
|
|
|
fHost: string;
|
|
|
|
|
fBasePath: string;
|
2022-04-01 16:49:29 +02:00
|
|
|
|
fPathFilter: string;
|
2022-12-25 19:46:07 +01:00
|
|
|
|
fTransferProtocolSchemes: TMVCTransferProtocolSchemes;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
procedure DocumentApiInfo(const ASwagDoc: TSwagDoc);
|
|
|
|
|
procedure DocumentApiSettings(AContext: TWebContext; ASwagDoc: TSwagDoc);
|
2019-10-30 16:02:30 +01:00
|
|
|
|
procedure DocumentApiAuthentication(const ASwagDoc: TSwagDoc);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
procedure DocumentApi(ASwagDoc: TSwagDoc);
|
2024-10-01 16:16:27 +02:00
|
|
|
|
procedure DocumentActiveRecordControllerApi(ASwagDoc: TSwagDoc);
|
2020-01-03 20:49:53 +01:00
|
|
|
|
procedure SortApiPaths(ASwagDoc: TSwagDoc);
|
2019-10-09 23:14:56 +02:00
|
|
|
|
procedure InternalRender(AContent: string; AContext: TWebContext);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
public
|
2022-12-25 19:46:07 +01:00
|
|
|
|
constructor Create(
|
|
|
|
|
const AEngine: TMVCEngine;
|
|
|
|
|
const ASwaggerInfo: TMVCSwaggerInfo;
|
|
|
|
|
const ASwaggerDocumentationURL: string = '/swagger.json';
|
|
|
|
|
const AJWTDescription: string = JWT_DEFAULT_DESCRIPTION;
|
2022-04-01 16:49:29 +02:00
|
|
|
|
const AEnableBasicAuthentication: Boolean = False;
|
2022-12-25 19:46:07 +01:00
|
|
|
|
const AHost: string = '';
|
|
|
|
|
const ABasePath: string = '';
|
|
|
|
|
const APathFilter: String = '';
|
2024-09-03 13:49:28 +02:00
|
|
|
|
const ATransferProtocolSchemes: TMVCTransferProtocolSchemes = [psHTTP, psHTTPS];
|
|
|
|
|
const AEnableBearerAuthentication: Boolean = False);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
procedure OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
|
|
|
|
|
procedure OnBeforeControllerAction(AContext: TWebContext; const AControllerQualifiedClassName: string;
|
|
|
|
|
const AActionName: string; var AHandled: Boolean);
|
2022-08-13 00:21:00 +02:00
|
|
|
|
procedure OnAfterControllerAction(AContext: TWebContext;
|
|
|
|
|
const AControllerQualifiedClassName: string; const AActionName: string;
|
|
|
|
|
const AHandled: Boolean);
|
2020-04-29 01:57:29 +02:00
|
|
|
|
procedure OnAfterRouting(AContext: TWebContext; const AHandled: Boolean);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.Classes,
|
|
|
|
|
JsonDataObjects,
|
|
|
|
|
System.Rtti,
|
|
|
|
|
Swag.Doc.Path,
|
|
|
|
|
Swag.Doc.Path.Operation,
|
2019-07-30 23:15:37 +02:00
|
|
|
|
Swag.Doc.Path.Operation.Response,
|
|
|
|
|
MVCFramework.Middleware.JWT,
|
2024-10-01 16:16:27 +02:00
|
|
|
|
MVCFramework.ActiveRecordController,
|
2019-08-01 22:31:59 +02:00
|
|
|
|
Swag.Doc.Path.Operation.RequestParameter,
|
2019-10-30 16:02:30 +01:00
|
|
|
|
Swag.Doc.SecurityDefinitionApiKey,
|
2019-11-27 20:27:07 +01:00
|
|
|
|
Swag.Doc.SecurityDefinitionBasic,
|
2020-01-03 20:49:53 +01:00
|
|
|
|
Swag.Doc.Definition,
|
2020-05-12 23:25:09 +02:00
|
|
|
|
System.Generics.Collections,
|
2024-10-01 16:16:27 +02:00
|
|
|
|
System.Generics.Defaults,
|
2022-04-01 00:29:39 +02:00
|
|
|
|
System.TypInfo,
|
2024-10-01 16:16:27 +02:00
|
|
|
|
MVCFramework.Serializer.Commons,
|
|
|
|
|
Json.Common.Helpers,
|
|
|
|
|
MVCFramework.ActiveRecord;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
|
|
|
|
{ TMVCSwaggerMiddleware }
|
|
|
|
|
|
|
|
|
|
constructor TMVCSwaggerMiddleware.Create(const AEngine: TMVCEngine; const ASwaggerInfo: TMVCSwaggerInfo;
|
2021-10-22 01:18:49 +02:00
|
|
|
|
const ASwaggerDocumentationURL, AJWTDescription: string; const AEnableBasicAuthentication: Boolean;
|
2022-04-01 16:49:29 +02:00
|
|
|
|
const AHost, ABasePath: string;
|
2022-12-25 19:46:07 +01:00
|
|
|
|
const APathFilter: String;
|
2024-09-03 13:49:28 +02:00
|
|
|
|
const ATransferProtocolSchemes: TMVCTransferProtocolSchemes; const AEnableBearerAuthentication: Boolean);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
|
|
|
|
inherited Create;
|
2019-11-27 20:27:07 +01:00
|
|
|
|
fSwagDocURL := ASwaggerDocumentationURL;
|
|
|
|
|
fEngine := AEngine;
|
|
|
|
|
fSwaggerInfo := ASwaggerInfo;
|
|
|
|
|
fJWTDescription := AJWTDescription;
|
|
|
|
|
fEnableBasicAuthentication := AEnableBasicAuthentication;
|
2024-09-03 13:49:28 +02:00
|
|
|
|
fEnableBearerAuthentication := AEnableBearerAuthentication;
|
2021-10-22 01:18:49 +02:00
|
|
|
|
fHost := AHost;
|
|
|
|
|
fBasePath := ABasePath;
|
2022-04-01 16:49:29 +02:00
|
|
|
|
fPathFilter := APathFilter;
|
2022-12-25 19:46:07 +01:00
|
|
|
|
fTransferProtocolSchemes := ATransferProtocolSchemes;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
destructor TMVCSwaggerMiddleware.Destroy;
|
|
|
|
|
begin
|
|
|
|
|
inherited Destroy;
|
|
|
|
|
end;
|
|
|
|
|
|
2024-10-01 16:16:27 +02:00
|
|
|
|
procedure TMVCSwaggerMiddleware.DocumentActiveRecordControllerApi(ASwagDoc: TSwagDoc);
|
|
|
|
|
var
|
|
|
|
|
lRttiContext: TRttiContext;
|
|
|
|
|
lObjType: TRttiType;
|
|
|
|
|
lController: TMVCControllerDelegate;
|
|
|
|
|
lSwagPath: TSwagPath;
|
|
|
|
|
lAttr: TCustomAttribute;
|
|
|
|
|
lControllerPath: string;
|
|
|
|
|
lMethodPath: string;
|
|
|
|
|
lMethod: TRttiMethod;
|
|
|
|
|
lFoundAttr: Boolean;
|
|
|
|
|
lMVCHttpMethods: TMVCHTTPMethods;
|
|
|
|
|
lSwagPathOp: TSwagPathOperation;
|
|
|
|
|
I: TMVCHTTPMethodType;
|
|
|
|
|
lPathUri: string;
|
|
|
|
|
lIndex: Integer;
|
|
|
|
|
lAuthTypeName: string;
|
|
|
|
|
lIsIgnoredPath: Boolean;
|
|
|
|
|
lControllerDefaultModelClass: TClass;
|
|
|
|
|
lControllerDefaultSummaryTags: TArray<string>;
|
|
|
|
|
lPathAttributeFound: Boolean;
|
|
|
|
|
lVisitedMethodSignatures: TList<String>;
|
|
|
|
|
lMethodSignature: string;
|
|
|
|
|
lControllerDefaultModelSingularName: string;
|
|
|
|
|
lControllerDefaultModelPluralName: string;
|
|
|
|
|
lEntitiesMapping: TArray<TMVCEntityMapping>;
|
|
|
|
|
lPrefixURLSegment: string;
|
|
|
|
|
lEntityMapping: TMVCEntityMapping;
|
|
|
|
|
begin
|
|
|
|
|
lVisitedMethodSignatures := TList<String>.Create;
|
|
|
|
|
try
|
|
|
|
|
lRttiContext := TRttiContext.Create;
|
|
|
|
|
try
|
|
|
|
|
for lController in fEngine.Controllers do
|
|
|
|
|
begin
|
|
|
|
|
lControllerPath := '';
|
|
|
|
|
SetLength(lControllerDefaultSummaryTags, 0);
|
|
|
|
|
lPathAttributeFound := False;
|
|
|
|
|
lObjType := lRttiContext.GetType(lController.Clazz);
|
|
|
|
|
|
|
|
|
|
//Automatic API generated by TMVCActiveRecordController
|
|
|
|
|
if not lController.Clazz.InheritsFrom(TMVCActiveRecordController) then
|
|
|
|
|
begin
|
|
|
|
|
Continue;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lEntitiesMapping := ActiveRecordMappingRegistry.GetURLSegmentWithEntities;
|
|
|
|
|
lPrefixURLSegment := lController.URLSegment;
|
|
|
|
|
|
|
|
|
|
for lEntityMapping in lEntitiesMapping do
|
|
|
|
|
begin
|
|
|
|
|
lControllerPath := lPrefixURLSegment;
|
|
|
|
|
lControllerDefaultModelClass := lEntityMapping.Value;
|
|
|
|
|
lControllerDefaultModelSingularName := lEntityMapping.Value.ClassName;
|
|
|
|
|
lControllerDefaultModelPluralName := lEntityMapping.Key;
|
|
|
|
|
SetLength(lControllerDefaultSummaryTags, 1);
|
|
|
|
|
lControllerDefaultSummaryTags[0] := TMVCSerializerHelper.ApplyNameCase(ncPascalCase, lEntityMapping.Key);
|
|
|
|
|
|
|
|
|
|
for lMethod in lObjType.GetMethods do
|
|
|
|
|
begin
|
|
|
|
|
{only public and published methods are inspected}
|
|
|
|
|
if not (lMethod.Visibility in [mvPublished, mvPublic]) then
|
|
|
|
|
begin
|
|
|
|
|
Continue;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lIsIgnoredPath := False;
|
|
|
|
|
lFoundAttr := False;
|
|
|
|
|
lMVCHttpMethods := [];
|
|
|
|
|
lMethodPath := '';
|
|
|
|
|
|
|
|
|
|
for lAttr in lMethod.GetAttributes do
|
|
|
|
|
begin
|
|
|
|
|
if lAttr is MVCSwagIgnorePathAttribute then
|
|
|
|
|
begin
|
|
|
|
|
lIsIgnoredPath := True;
|
|
|
|
|
end;
|
|
|
|
|
if lAttr is MVCPathAttribute then
|
|
|
|
|
begin
|
|
|
|
|
lMethodPath := MVCPathAttribute(lAttr).Path;
|
|
|
|
|
lMethodPath := lMethodPath.Replace('($entityname)', lEntityMapping.Key, [rfReplaceAll]);
|
|
|
|
|
lFoundAttr := True;
|
|
|
|
|
end;
|
|
|
|
|
if lAttr is MVCHTTPMethodsAttribute then
|
|
|
|
|
begin
|
|
|
|
|
lMVCHttpMethods := MVCHTTPMethodsAttribute(lAttr).MVCHTTPMethods;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
if (not lIsIgnoredPath) and lFoundAttr then
|
|
|
|
|
begin
|
|
|
|
|
lMethodSignature := lObjType.Name + '.' + lEntityMapping.Key + '_' + lMethod.Name;
|
|
|
|
|
if lVisitedMethodSignatures.Contains(lMethodSignature) then
|
|
|
|
|
begin
|
|
|
|
|
Continue;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
lVisitedMethodSignatures.Add(lMethodSignature);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
//LogI(lObjType.Name + '.' + lMethod.Name + ' ' + lMethod.Parent.ToString);
|
|
|
|
|
lSwagPath := nil;
|
|
|
|
|
lPathUri := TMVCSwagger.MVCPathToSwagPath(lControllerPath + lMethodPath);
|
|
|
|
|
for lIndex := 0 to Pred(ASwagDoc.Paths.Count) do
|
|
|
|
|
begin
|
|
|
|
|
if SameText(ASwagDoc.Paths[lIndex].Uri, lPathUri) then
|
|
|
|
|
begin
|
|
|
|
|
lSwagPath := ASwagDoc.Paths[lIndex];
|
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
if not Assigned(lSwagPath) then
|
|
|
|
|
begin
|
|
|
|
|
lSwagPath := TSwagPath.Create;
|
|
|
|
|
lSwagPath.Uri := lPathUri;
|
|
|
|
|
ASwagDoc.Paths.Add(lSwagPath);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
for I in lMVCHttpMethods do
|
|
|
|
|
begin
|
|
|
|
|
lSwagPathOp := TSwagPathOperation.Create;
|
|
|
|
|
TMVCSwagger.FillOperationSummary(
|
|
|
|
|
lSwagPathOp,
|
|
|
|
|
lMethod,
|
|
|
|
|
ASwagDoc.Definitions,
|
|
|
|
|
I,
|
|
|
|
|
lControllerDefaultModelClass,
|
|
|
|
|
lControllerDefaultModelSingularName,
|
|
|
|
|
lControllerDefaultModelPluralName,
|
|
|
|
|
lControllerDefaultSummaryTags);
|
|
|
|
|
if TMVCSwagger.MethodRequiresAuthentication(lMethod, lObjType, lAuthTypeName) then
|
|
|
|
|
begin
|
|
|
|
|
lSwagPathOp.Security.Add(lAuthTypeName);
|
|
|
|
|
end;
|
|
|
|
|
lSwagPathOp.Parameters.AddRange(
|
|
|
|
|
TMVCSwagger.GetParamsFromMethod(
|
|
|
|
|
lSwagPath.Uri,
|
|
|
|
|
lMethod,
|
|
|
|
|
ASwagDoc.Definitions,
|
2024-10-18 07:32:58 +02:00
|
|
|
|
I,
|
2024-10-01 16:16:27 +02:00
|
|
|
|
lControllerDefaultModelClass,
|
|
|
|
|
lControllerDefaultModelSingularName,
|
|
|
|
|
lControllerDefaultModelPluralName)
|
|
|
|
|
);
|
|
|
|
|
lSwagPathOp.Operation := TMVCSwagger.MVCHttpMethodToSwagPathOperation(I);
|
|
|
|
|
lSwagPath.Operations.Add(lSwagPathOp);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lRttiContext.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lVisitedMethodSignatures.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
procedure TMVCSwaggerMiddleware.DocumentApi(ASwagDoc: TSwagDoc);
|
|
|
|
|
var
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lRttiContext: TRttiContext;
|
|
|
|
|
lObjType: TRttiType;
|
|
|
|
|
lController: TMVCControllerDelegate;
|
|
|
|
|
lSwagPath: TSwagPath;
|
|
|
|
|
lAttr: TCustomAttribute;
|
|
|
|
|
lControllerPath: string;
|
|
|
|
|
lMethodPath: string;
|
|
|
|
|
lMethod: TRttiMethod;
|
|
|
|
|
lFoundAttr: Boolean;
|
|
|
|
|
lMVCHttpMethods: TMVCHTTPMethods;
|
|
|
|
|
lSwagPathOp: TSwagPathOperation;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
I: TMVCHTTPMethodType;
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lPathUri: string;
|
|
|
|
|
lIndex: Integer;
|
|
|
|
|
lAuthTypeName: string;
|
|
|
|
|
lIsIgnoredPath: Boolean;
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lControllerDefaultModelClass: TClass;
|
|
|
|
|
lControllerDefaultSummaryTags: TArray<string>;
|
|
|
|
|
lPathAttributeFound: Boolean;
|
|
|
|
|
lVisitedMethodSignatures: TList<String>;
|
|
|
|
|
lMethodSignature: string;
|
2022-01-04 15:44:47 +01:00
|
|
|
|
lControllerDefaultModelSingularName: string;
|
|
|
|
|
lControllerDefaultModelPluralName: string;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lVisitedMethodSignatures := TList<String>.Create;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
try
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lRttiContext := TRttiContext.Create;
|
|
|
|
|
try
|
|
|
|
|
for lController in fEngine.Controllers do
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lControllerDefaultModelClass := nil;
|
|
|
|
|
lControllerPath := '';
|
|
|
|
|
SetLength(lControllerDefaultSummaryTags, 0);
|
|
|
|
|
lPathAttributeFound := False;
|
|
|
|
|
lObjType := lRttiContext.GetType(lController.Clazz);
|
|
|
|
|
for lAttr in lObjType.GetAttributes do
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
if lAttr is MVCSwagIgnorePathAttribute then
|
|
|
|
|
begin
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lControllerPath := '';
|
|
|
|
|
Break;
|
2019-11-27 20:27:07 +01:00
|
|
|
|
end;
|
|
|
|
|
if lAttr is MVCPathAttribute then
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2022-08-05 12:20:20 +02:00
|
|
|
|
if not lPathAttributeFound then
|
|
|
|
|
begin
|
|
|
|
|
{in case of more than one MVCPath attribute, only the firstone
|
|
|
|
|
is considered by swagger}
|
|
|
|
|
lControllerPath := MVCPathAttribute(lAttr).Path;
|
|
|
|
|
lPathAttributeFound := fPathFilter.IsEmpty or lControllerPath.StartsWith(fPathFilter);
|
|
|
|
|
end;
|
2021-12-31 18:00:54 +01:00
|
|
|
|
end;
|
|
|
|
|
if lAttr is MVCSWAGDefaultModel then
|
|
|
|
|
begin
|
2022-01-04 15:44:47 +01:00
|
|
|
|
lControllerDefaultModelClass := MVCSWAGDefaultModel(lAttr).JsonSchemaClass;
|
|
|
|
|
lControllerDefaultModelSingularName := MVCSWAGDefaultModel(lAttr).SingularModelName;
|
|
|
|
|
lControllerDefaultModelPluralName := MVCSWAGDefaultModel(lAttr).PluralModelName;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
2021-12-31 18:00:54 +01:00
|
|
|
|
if lAttr is MVCSWAGDefaultSummaryTags then
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lControllerDefaultSummaryTags := MVCSWAGDefaultSummaryTags(lAttr).GetTags;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2021-12-31 18:00:54 +01:00
|
|
|
|
if not lPathAttributeFound then
|
|
|
|
|
Continue;
|
|
|
|
|
|
|
|
|
|
//for lMethod in lObjType.GetDeclaredMethods do
|
|
|
|
|
for lMethod in lObjType.GetMethods do
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2021-12-31 18:00:54 +01:00
|
|
|
|
{only public and puches methods are inspected}
|
|
|
|
|
if not (lMethod.Visibility in [mvPublished, mvPublic]) then
|
2019-08-05 16:59:35 +02:00
|
|
|
|
begin
|
2021-12-31 18:00:54 +01:00
|
|
|
|
continue;
|
2019-08-05 16:59:35 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2021-12-31 18:00:54 +01:00
|
|
|
|
{here could arrive also overwritten methods, so we need to exclude
|
|
|
|
|
method which have been overwritten. We can do this checking if the method class
|
|
|
|
|
is the controller which we are inspecting }
|
|
|
|
|
// if lObjType <> lMethod.Parent then
|
|
|
|
|
// begin
|
|
|
|
|
// Continue;
|
|
|
|
|
// end;
|
|
|
|
|
|
|
|
|
|
lIsIgnoredPath := False;
|
|
|
|
|
lFoundAttr := False;
|
|
|
|
|
lMVCHttpMethods := [];
|
|
|
|
|
lMethodPath := '';
|
|
|
|
|
|
|
|
|
|
for lAttr in lMethod.GetAttributes do
|
2019-08-05 16:59:35 +02:00
|
|
|
|
begin
|
2021-12-31 18:00:54 +01:00
|
|
|
|
if lAttr is MVCSwagIgnorePathAttribute then
|
|
|
|
|
begin
|
|
|
|
|
lIsIgnoredPath := True;
|
|
|
|
|
end;
|
|
|
|
|
if lAttr is MVCPathAttribute then
|
|
|
|
|
begin
|
|
|
|
|
lMethodPath := MVCPathAttribute(lAttr).Path;
|
|
|
|
|
lFoundAttr := True;
|
|
|
|
|
end;
|
|
|
|
|
if lAttr is MVCHTTPMethodsAttribute then
|
|
|
|
|
begin
|
|
|
|
|
lMVCHttpMethods := MVCHTTPMethodsAttribute(lAttr).MVCHTTPMethods;
|
|
|
|
|
end;
|
2019-08-05 16:59:35 +02:00
|
|
|
|
end;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
2021-12-31 18:00:54 +01:00
|
|
|
|
if (not lIsIgnoredPath) and lFoundAttr then
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lMethodSignature := lObjType.Name + '.' + lMethod.Name;
|
|
|
|
|
if lVisitedMethodSignatures.Contains(lMethodSignature) then
|
|
|
|
|
begin
|
|
|
|
|
Continue;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
lVisitedMethodSignatures.Add(lMethodSignature);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
//LogI(lObjType.Name + '.' + lMethod.Name + ' ' + lMethod.Parent.ToString);
|
|
|
|
|
lSwagPath := nil;
|
|
|
|
|
lPathUri := TMVCSwagger.MVCPathToSwagPath(lControllerPath + lMethodPath);
|
|
|
|
|
for lIndex := 0 to Pred(ASwagDoc.Paths.Count) do
|
|
|
|
|
begin
|
|
|
|
|
if SameText(ASwagDoc.Paths[lIndex].Uri, lPathUri) then
|
|
|
|
|
begin
|
|
|
|
|
lSwagPath := ASwagDoc.Paths[lIndex];
|
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
if not Assigned(lSwagPath) then
|
|
|
|
|
begin
|
|
|
|
|
lSwagPath := TSwagPath.Create;
|
|
|
|
|
lSwagPath.Uri := lPathUri;
|
|
|
|
|
ASwagDoc.Paths.Add(lSwagPath);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
for I in lMVCHttpMethods do
|
2019-11-03 16:16:35 +01:00
|
|
|
|
begin
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lSwagPathOp := TSwagPathOperation.Create;
|
|
|
|
|
TMVCSwagger.FillOperationSummary(
|
|
|
|
|
lSwagPathOp,
|
|
|
|
|
lMethod,
|
|
|
|
|
ASwagDoc.Definitions,
|
|
|
|
|
I,
|
|
|
|
|
lControllerDefaultModelClass,
|
2022-01-04 15:44:47 +01:00
|
|
|
|
lControllerDefaultModelSingularName,
|
|
|
|
|
lControllerDefaultModelPluralName,
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lControllerDefaultSummaryTags);
|
|
|
|
|
if TMVCSwagger.MethodRequiresAuthentication(lMethod, lObjType, lAuthTypeName) then
|
|
|
|
|
begin
|
|
|
|
|
lSwagPathOp.Security.Add(lAuthTypeName);
|
|
|
|
|
end;
|
2022-01-04 15:44:47 +01:00
|
|
|
|
lSwagPathOp.Parameters.AddRange(
|
|
|
|
|
TMVCSwagger.GetParamsFromMethod(
|
|
|
|
|
lSwagPath.Uri,
|
|
|
|
|
lMethod,
|
|
|
|
|
ASwagDoc.Definitions,
|
2024-10-18 07:32:58 +02:00
|
|
|
|
I,
|
2022-01-11 15:30:40 +01:00
|
|
|
|
lControllerDefaultModelClass,
|
|
|
|
|
lControllerDefaultModelSingularName,
|
|
|
|
|
lControllerDefaultModelPluralName)
|
|
|
|
|
);
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lSwagPathOp.Operation := TMVCSwagger.MVCHttpMethodToSwagPathOperation(I);
|
|
|
|
|
lSwagPath.Operations.Add(lSwagPathOp);
|
2019-11-03 16:16:35 +01:00
|
|
|
|
end;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2021-12-31 18:00:54 +01:00
|
|
|
|
finally
|
|
|
|
|
lRttiContext.Free;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
finally
|
2021-12-31 18:00:54 +01:00
|
|
|
|
lVisitedMethodSignatures.Free;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCSwaggerMiddleware.DocumentApiInfo(const ASwagDoc: TSwagDoc);
|
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
ASwagDoc.Info.Title := fSwaggerInfo.Title;
|
|
|
|
|
ASwagDoc.Info.Version := fSwaggerInfo.Version;
|
|
|
|
|
ASwagDoc.Info.TermsOfService := fSwaggerInfo.TermsOfService;
|
|
|
|
|
ASwagDoc.Info.Description := fSwaggerInfo.Description;
|
|
|
|
|
ASwagDoc.Info.Contact.Name := fSwaggerInfo.ContactName;
|
|
|
|
|
ASwagDoc.Info.Contact.Email := fSwaggerInfo.ContactEmail;
|
|
|
|
|
ASwagDoc.Info.Contact.Url := fSwaggerInfo.ContactUrl;
|
|
|
|
|
ASwagDoc.Info.License.Name := fSwaggerInfo.LicenseName;
|
|
|
|
|
ASwagDoc.Info.License.Url := fSwaggerInfo.LicenseUrl;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-10-30 16:02:30 +01:00
|
|
|
|
procedure TMVCSwaggerMiddleware.DocumentApiAuthentication(const ASwagDoc: TSwagDoc);
|
2019-07-30 23:15:37 +02:00
|
|
|
|
var
|
2020-03-24 00:42:58 +01:00
|
|
|
|
lMiddleware: IMVCMiddleware;
|
|
|
|
|
lJWTMiddleware: TMVCJWTAuthenticationMiddleware;
|
|
|
|
|
lRttiContext: TRttiContext;
|
|
|
|
|
lObjType: TRttiType;
|
|
|
|
|
lJwtUrlField: TRttiField;
|
|
|
|
|
lJwtUrlSegment: string;
|
|
|
|
|
lSecurityDefsBearer: TSwagSecurityDefinitionApiKey;
|
|
|
|
|
lSecurityDefsBasic: TSwagSecurityDefinitionBasic;
|
2019-07-30 23:15:37 +02:00
|
|
|
|
begin
|
2020-03-24 00:42:58 +01:00
|
|
|
|
lJWTMiddleware := nil;
|
2024-09-10 23:42:24 +02:00
|
|
|
|
lJwtUrlField := nil;
|
2020-03-24 00:42:58 +01:00
|
|
|
|
for lMiddleware in fEngine.Middlewares do
|
2019-07-30 23:15:37 +02:00
|
|
|
|
begin
|
2020-03-24 00:42:58 +01:00
|
|
|
|
if lMiddleware is TMVCJWTAuthenticationMiddleware then
|
2019-07-30 23:15:37 +02:00
|
|
|
|
begin
|
2020-03-24 00:42:58 +01:00
|
|
|
|
lJWTMiddleware := lMiddleware as TMVCJWTAuthenticationMiddleware;
|
2019-07-30 23:15:37 +02:00
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-03-24 00:42:58 +01:00
|
|
|
|
if Assigned(lJWTMiddleware) or fEnableBasicAuthentication then
|
2019-10-30 16:02:30 +01:00
|
|
|
|
begin
|
2020-03-24 00:42:58 +01:00
|
|
|
|
lSecurityDefsBasic := TSwagSecurityDefinitionBasic.Create;
|
|
|
|
|
lSecurityDefsBasic.SchemeName := SECURITY_BASIC_NAME;
|
|
|
|
|
lSecurityDefsBasic.Description := 'Send username and password for authentication';
|
|
|
|
|
ASwagDoc.SecurityDefinitions.Add(lSecurityDefsBasic);
|
2019-10-30 16:02:30 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-03-24 00:42:58 +01:00
|
|
|
|
if Assigned(lJWTMiddleware) then
|
2019-07-30 23:15:37 +02:00
|
|
|
|
begin
|
2020-03-24 00:42:58 +01:00
|
|
|
|
lRttiContext := TRttiContext.Create;
|
2019-07-30 23:15:37 +02:00
|
|
|
|
try
|
2020-03-24 00:42:58 +01:00
|
|
|
|
lObjType := lRttiContext.GetType(lJWTMiddleware.ClassInfo);
|
|
|
|
|
lJwtUrlField := lObjType.GetField('FLoginURLSegment');
|
|
|
|
|
if Assigned(lJwtUrlField) then
|
2019-07-30 23:15:37 +02:00
|
|
|
|
begin
|
2020-03-24 00:42:58 +01:00
|
|
|
|
lJwtUrlSegment := lJwtUrlField.GetValue(lJWTMiddleware).AsString;
|
|
|
|
|
if lJwtUrlSegment.StartsWith(ASwagDoc.BasePath) then
|
|
|
|
|
lJwtUrlSegment := lJwtUrlSegment.Remove(0, ASwagDoc.BasePath.Length);
|
|
|
|
|
if not lJwtUrlSegment.StartsWith('/') then
|
|
|
|
|
lJwtUrlSegment.Insert(0, '/');
|
2019-08-01 22:31:59 +02:00
|
|
|
|
|
|
|
|
|
// Path operation Middleware JWT
|
2020-03-24 00:42:58 +01:00
|
|
|
|
ASwagDoc.Paths.Add(TMVCSwagger.GetJWTAuthenticationPath(lJwtUrlSegment,
|
|
|
|
|
lJWTMiddleware.UserNameHeaderName, lJWTMiddleware.PasswordHeaderName));
|
2019-07-30 23:15:37 +02:00
|
|
|
|
end;
|
|
|
|
|
finally
|
2020-03-24 00:42:58 +01:00
|
|
|
|
lRttiContext.Free;
|
2019-07-30 23:15:37 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
2024-09-03 13:49:28 +02:00
|
|
|
|
|
|
|
|
|
// Methods that have the MVCRequiresAuthentication attribute use bearer authentication.
|
|
|
|
|
if fEnableBearerAuthentication or
|
|
|
|
|
(Assigned(lJWTMiddleware) and Assigned(lJwtUrlField)) then
|
|
|
|
|
begin
|
|
|
|
|
lSecurityDefsBearer := TSwagSecurityDefinitionApiKey.Create;
|
|
|
|
|
lSecurityDefsBearer.SchemeName := SECURITY_BEARER_NAME;
|
|
|
|
|
lSecurityDefsBearer.InLocation := kilHeader;
|
|
|
|
|
lSecurityDefsBearer.Name := 'Authorization';
|
|
|
|
|
lSecurityDefsBearer.Description := fJWTDescription;
|
|
|
|
|
ASwagDoc.SecurityDefinitions.Add(lSecurityDefsBearer);
|
|
|
|
|
end;
|
2019-07-30 23:15:37 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
procedure TMVCSwaggerMiddleware.DocumentApiSettings(AContext: TWebContext; ASwagDoc: TSwagDoc);
|
2022-12-25 19:46:07 +01:00
|
|
|
|
var
|
|
|
|
|
lSwagSchemes: TSwagTransferProtocolSchemes;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2021-10-22 01:18:49 +02:00
|
|
|
|
ASwagDoc.Host := fHost;
|
|
|
|
|
if ASwagDoc.Host.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
ASwagDoc.Host := Format('%s:%d', [AContext.Request.RawWebRequest.Host, AContext.Request.RawWebRequest.ServerPort]);
|
|
|
|
|
end;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
2021-10-22 01:18:49 +02:00
|
|
|
|
ASwagDoc.BasePath := fBasePath;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
if ASwagDoc.BasePath.IsEmpty then
|
2021-10-22 01:18:49 +02:00
|
|
|
|
begin
|
|
|
|
|
ASwagDoc.BasePath := fEngine.Config[TMVCConfigKey.PathPrefix];
|
|
|
|
|
end;
|
|
|
|
|
if ASwagDoc.BasePath.IsEmpty then
|
|
|
|
|
begin
|
2019-07-27 20:23:48 +02:00
|
|
|
|
ASwagDoc.BasePath := '/';
|
2021-10-22 01:18:49 +02:00
|
|
|
|
end;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
2022-12-25 19:46:07 +01:00
|
|
|
|
lSwagSchemes := [];
|
|
|
|
|
if psHTTP in fTransferProtocolSchemes then
|
|
|
|
|
begin
|
|
|
|
|
Include(lSwagSchemes, tpsHttp);
|
|
|
|
|
end;
|
|
|
|
|
if psHTTPS in fTransferProtocolSchemes then
|
|
|
|
|
begin
|
|
|
|
|
Include(lSwagSchemes, tpsHttps);
|
|
|
|
|
end;
|
|
|
|
|
ASwagDoc.Schemes := lSwagSchemes;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCSwaggerMiddleware.InternalRender(AContent: string; AContext: TWebContext);
|
|
|
|
|
var
|
|
|
|
|
LContentType: string;
|
|
|
|
|
LEncoding: TEncoding;
|
|
|
|
|
begin
|
|
|
|
|
LContentType := BuildContentType(TMVCMediaType.APPLICATION_JSON, TMVCConstants.DEFAULT_CONTENT_CHARSET);
|
|
|
|
|
AContext.Response.RawWebResponse.ContentType := LContentType;
|
|
|
|
|
|
|
|
|
|
LEncoding := TEncoding.GetEncoding(TMVCConstants.DEFAULT_CONTENT_CHARSET);
|
|
|
|
|
try
|
|
|
|
|
AContext.Response.SetContentStream(TBytesStream.Create(TEncoding.Convert(TEncoding.Default, LEncoding,
|
2019-10-09 23:14:56 +02:00
|
|
|
|
TEncoding.Default.GetBytes(AContent))), LContentType);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
finally
|
|
|
|
|
LEncoding.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2022-08-13 00:21:00 +02:00
|
|
|
|
procedure TMVCSwaggerMiddleware.OnAfterControllerAction(AContext: TWebContext;
|
|
|
|
|
const AControllerQualifiedClassName: string; const AActionName: string;
|
|
|
|
|
const AHandled: Boolean);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2020-04-29 01:57:29 +02:00
|
|
|
|
// do nothing
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCSwaggerMiddleware.OnAfterRouting(AContext: TWebContext; const AHandled: Boolean);
|
|
|
|
|
begin
|
|
|
|
|
// do nothing
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-10-09 23:14:56 +02:00
|
|
|
|
procedure TMVCSwaggerMiddleware.OnBeforeControllerAction(AContext: TWebContext;
|
|
|
|
|
const AControllerQualifiedClassName, AActionName: string; var AHandled: Boolean);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2020-04-29 01:57:29 +02:00
|
|
|
|
// do nothing
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCSwaggerMiddleware.OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
|
|
|
|
|
var
|
|
|
|
|
LSwagDoc: TSwagDoc;
|
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
if SameText(AContext.Request.PathInfo, fSwagDocURL) and (AContext.Request.HTTPMethod in [httpGET, httpPOST]) then
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
|
|
|
|
LSwagDoc := TSwagDoc.Create;
|
2019-10-09 23:14:56 +02:00
|
|
|
|
try
|
2019-08-13 16:57:42 +02:00
|
|
|
|
DocumentApiInfo(LSwagDoc);
|
|
|
|
|
DocumentApiSettings(AContext, LSwagDoc);
|
2019-10-30 16:02:30 +01:00
|
|
|
|
DocumentApiAuthentication(LSwagDoc);
|
2024-10-01 16:16:27 +02:00
|
|
|
|
DocumentActiveRecordControllerApi(LSwagDoc);
|
2019-08-13 16:57:42 +02:00
|
|
|
|
DocumentApi(LSwagDoc);
|
2020-01-03 20:49:53 +01:00
|
|
|
|
SortApiPaths(LSwagDoc);
|
2019-08-13 16:57:42 +02:00
|
|
|
|
|
|
|
|
|
LSwagDoc.GenerateSwaggerJson;
|
2022-10-21 23:08:20 +02:00
|
|
|
|
InternalRender(LSwagDoc.SwaggerJson.Format, AContext);
|
2019-08-13 16:57:42 +02:00
|
|
|
|
AHandled := True;
|
2019-10-09 23:14:56 +02:00
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
finally
|
|
|
|
|
LSwagDoc.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-01-03 20:49:53 +01:00
|
|
|
|
procedure TMVCSwaggerMiddleware.SortApiPaths(ASwagDoc: TSwagDoc);
|
|
|
|
|
var
|
2020-01-03 21:47:01 +01:00
|
|
|
|
lPathComparer: IComparer<TSwagPath>;
|
|
|
|
|
lOperationComparer: IComparer<TSwagPathOperation>;
|
|
|
|
|
lSwagPath: TSwagPath;
|
2021-05-18 17:05:29 +02:00
|
|
|
|
{$IF not defined(RIOORBETTER)}
|
2020-05-12 23:25:09 +02:00
|
|
|
|
lSwagPathList: TArray<TSwagPath>;
|
|
|
|
|
lSwagOperationList: TArray<TSwagPathOperation>;
|
|
|
|
|
{$ENDIF}
|
2020-01-03 20:49:53 +01:00
|
|
|
|
begin
|
2020-01-03 21:47:01 +01:00
|
|
|
|
// Sort paths
|
|
|
|
|
lPathComparer := TDelegatedComparer<TSwagPath>.Create(
|
2020-05-12 23:25:09 +02:00
|
|
|
|
function(const Left, Right: TSwagPath): Integer
|
2020-05-01 01:51:41 +02:00
|
|
|
|
begin
|
2020-12-10 19:25:12 +01:00
|
|
|
|
if (Left.Operations.Count = 0) or (Left.Operations[0].Tags.Count = 0) or
|
|
|
|
|
(Right.Operations.Count = 0) or (Right.Operations[0].Tags.Count = 0) then
|
|
|
|
|
begin
|
|
|
|
|
Result := 1;
|
|
|
|
|
end
|
|
|
|
|
else if SameText(Left.Operations[0].Tags[0], JWT_AUTHENTICATION_TAG) or
|
2020-05-12 23:25:09 +02:00
|
|
|
|
SameText(Right.Operations[0].Tags[0], JWT_AUTHENTICATION_TAG) then
|
|
|
|
|
begin
|
|
|
|
|
Result := -1;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
Result := CompareText(Left.Operations[0].Tags[0], Right.Operations[0].Tags[0]);
|
|
|
|
|
end;
|
|
|
|
|
end);
|
2020-01-03 21:47:01 +01:00
|
|
|
|
|
2021-05-18 17:05:29 +02:00
|
|
|
|
{$IF defined(RIOORBETTER)}
|
2020-01-03 21:47:01 +01:00
|
|
|
|
ASwagDoc.Paths.Sort(lPathComparer);
|
2020-05-12 23:25:09 +02:00
|
|
|
|
{$ELSE}
|
|
|
|
|
ASwagDoc.Paths.TrimExcess;
|
|
|
|
|
lSwagPathList := ASwagDoc.Paths.ToArray;
|
|
|
|
|
ASwagDoc.Paths.OwnsObjects := False;
|
|
|
|
|
ASwagDoc.Paths.Clear;
|
|
|
|
|
TArrayHelper.QuickSort<TSwagPath>(lSwagPathList, lPathComparer, Low(lSwagPathList), High(lSwagPathList));
|
|
|
|
|
ASwagDoc.Paths.AddRange(lSwagPathList);
|
|
|
|
|
ASwagDoc.Paths.OwnsObjects := True;
|
|
|
|
|
{$ENDIF}
|
2020-01-03 21:47:01 +01:00
|
|
|
|
|
|
|
|
|
// Sort paths operations
|
|
|
|
|
lOperationComparer := TDelegatedComparer<TSwagPathOperation>.Create(
|
|
|
|
|
function(const Left, Right: TSwagPathOperation): Integer
|
|
|
|
|
begin
|
|
|
|
|
if Ord(Left.Operation) > Ord(Right.Operation) then
|
|
|
|
|
Result := -1
|
|
|
|
|
else if Ord(Left.Operation) < Ord(Right.Operation) then
|
|
|
|
|
Result := 1
|
|
|
|
|
else
|
|
|
|
|
Result := 0;
|
|
|
|
|
end);
|
|
|
|
|
|
|
|
|
|
for lSwagPath in ASwagDoc.Paths do
|
|
|
|
|
begin
|
2021-05-18 17:05:29 +02:00
|
|
|
|
{$IF defined(RIOORBETTER)}
|
2020-01-03 21:47:01 +01:00
|
|
|
|
lSwagPath.Operations.Sort(lOperationComparer);
|
2020-05-12 23:25:09 +02:00
|
|
|
|
{$ELSE}
|
|
|
|
|
lSwagPath.Operations.TrimExcess;
|
|
|
|
|
lSwagOperationList := lSwagPath.Operations.ToArray;
|
|
|
|
|
lSwagPath.Operations.OwnsObjects := False;
|
|
|
|
|
lSwagPath.Operations.Clear;
|
|
|
|
|
TArrayHelper.QuickSort<TSwagPathOperation>(lSwagOperationList, lOperationComparer,
|
|
|
|
|
Low(lSwagOperationList), High(lSwagOperationList));
|
|
|
|
|
lSwagPath.Operations.AddRange(lSwagOperationList);
|
|
|
|
|
lSwagPath.Operations.OwnsObjects := True;
|
|
|
|
|
{$ENDIF}
|
2020-01-03 21:47:01 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-01-03 20:49:53 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end.
|