delphimvcframework/sources/MVCFramework.Middleware.Swagger.pas

716 lines
24 KiB
ObjectPascal
Raw Normal View History

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;
{$I dmvcframework.inc}
2019-07-27 20:23:48 +02:00
interface
uses
MVCFramework,
MVCFramework.Logger,
2019-07-27 20:23:48 +02:00
Swag.Doc,
MVCFramework.Swagger.Commons,
Swag.Doc.SecurityDefinition,
Swag.Common.Types,
System.JSON, MVCFramework.Commons;
2019-07-27 20:23:48 +02:00
type
TMVCSwaggerMiddleware = class(TInterfacedObject, IMVCMiddleware)
private
fEngine: TMVCEngine;
fSwaggerInfo: TMVCSwaggerInfo;
fSwagDocURL: string;
fJWTDescription: string;
fEnableBasicAuthentication: Boolean;
fEnableBearerAuthentication: Boolean;
fHost: string;
fBasePath: string;
fPathFilter: string;
fTransferProtocolSchemes: TMVCTransferProtocolSchemes;
2019-07-27 20:23:48 +02:00
procedure DocumentApiInfo(const ASwagDoc: TSwagDoc);
procedure DocumentApiSettings(AContext: TWebContext; ASwagDoc: TSwagDoc);
procedure DocumentApiAuthentication(const ASwagDoc: TSwagDoc);
2019-07-27 20:23:48 +02:00
procedure DocumentApi(ASwagDoc: TSwagDoc);
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
constructor Create(
const AEngine: TMVCEngine;
const ASwaggerInfo: TMVCSwaggerInfo;
const ASwaggerDocumentationURL: string = '/swagger.json';
const AJWTDescription: string = JWT_DEFAULT_DESCRIPTION;
const AEnableBasicAuthentication: Boolean = False;
const AHost: string = '';
const ABasePath: string = '';
const APathFilter: String = '';
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);
procedure OnAfterControllerAction(AContext: TWebContext;
const AControllerQualifiedClassName: string; const AActionName: string;
const AHandled: Boolean);
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,
Swag.Doc.Path.Operation.Response,
MVCFramework.Middleware.JWT,
MVCFramework.ActiveRecordController,
Swag.Doc.Path.Operation.RequestParameter,
Swag.Doc.SecurityDefinitionApiKey,
Swag.Doc.SecurityDefinitionBasic,
2020-01-03 20:49:53 +01:00
Swag.Doc.Definition,
System.Generics.Collections,
System.Generics.Defaults,
System.TypInfo,
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;
const ASwaggerDocumentationURL, AJWTDescription: string; const AEnableBasicAuthentication: Boolean;
const AHost, ABasePath: string;
const APathFilter: String;
const ATransferProtocolSchemes: TMVCTransferProtocolSchemes; const AEnableBearerAuthentication: Boolean);
2019-07-27 20:23:48 +02:00
begin
inherited Create;
fSwagDocURL := ASwaggerDocumentationURL;
fEngine := AEngine;
fSwaggerInfo := ASwaggerInfo;
fJWTDescription := AJWTDescription;
fEnableBasicAuthentication := AEnableBasicAuthentication;
fEnableBearerAuthentication := AEnableBearerAuthentication;
fHost := AHost;
fBasePath := ABasePath;
fPathFilter := APathFilter;
fTransferProtocolSchemes := ATransferProtocolSchemes;
2019-07-27 20:23:48 +02:00
end;
destructor TMVCSwaggerMiddleware.Destroy;
begin
inherited Destroy;
end;
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,
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
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;
lPathUri: string;
lIndex: Integer;
lAuthTypeName: string;
lIsIgnoredPath: Boolean;
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
lVisitedMethodSignatures := TList<String>.Create;
2019-07-27 20:23:48 +02:00
try
lRttiContext := TRttiContext.Create;
try
for lController in fEngine.Controllers do
2019-07-27 20:23:48 +02:00
begin
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
if lAttr is MVCSwagIgnorePathAttribute then
begin
lControllerPath := '';
Break;
end;
if lAttr is MVCPathAttribute then
2019-07-27 20:23:48 +02:00
begin
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;
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;
if lAttr is MVCSWAGDefaultSummaryTags then
2019-07-27 20:23:48 +02:00
begin
lControllerDefaultSummaryTags := MVCSWAGDefaultSummaryTags(lAttr).GetTags;
2019-07-27 20:23:48 +02:00
end;
end;
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
{only public and puches methods are inspected}
if not (lMethod.Visibility in [mvPublished, mvPublic]) then
2019-08-05 16:59:35 +02:00
begin
continue;
2019-08-05 16:59:35 +02:00
end;
{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
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
if (not lIsIgnoredPath) and lFoundAttr then
2019-07-27 20:23:48 +02:00
begin
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
lSwagPathOp := TSwagPathOperation.Create;
TMVCSwagger.FillOperationSummary(
lSwagPathOp,
lMethod,
ASwagDoc.Definitions,
I,
lControllerDefaultModelClass,
2022-01-04 15:44:47 +01:00
lControllerDefaultModelSingularName,
lControllerDefaultModelPluralName,
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,
lControllerDefaultModelClass,
lControllerDefaultModelSingularName,
lControllerDefaultModelPluralName)
);
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;
finally
lRttiContext.Free;
2019-07-27 20:23:48 +02:00
end;
finally
lVisitedMethodSignatures.Free;
2019-07-27 20:23:48 +02:00
end;
end;
procedure TMVCSwaggerMiddleware.DocumentApiInfo(const ASwagDoc: TSwagDoc);
begin
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;
procedure TMVCSwaggerMiddleware.DocumentApiAuthentication(const ASwagDoc: TSwagDoc);
var
lMiddleware: IMVCMiddleware;
lJWTMiddleware: TMVCJWTAuthenticationMiddleware;
lRttiContext: TRttiContext;
lObjType: TRttiType;
lJwtUrlField: TRttiField;
lJwtUrlSegment: string;
lSecurityDefsBearer: TSwagSecurityDefinitionApiKey;
lSecurityDefsBasic: TSwagSecurityDefinitionBasic;
begin
lJWTMiddleware := nil;
2024-09-10 23:42:24 +02:00
lJwtUrlField := nil;
for lMiddleware in fEngine.Middlewares do
begin
if lMiddleware is TMVCJWTAuthenticationMiddleware then
begin
lJWTMiddleware := lMiddleware as TMVCJWTAuthenticationMiddleware;
Break;
end;
end;
if Assigned(lJWTMiddleware) or fEnableBasicAuthentication then
begin
lSecurityDefsBasic := TSwagSecurityDefinitionBasic.Create;
lSecurityDefsBasic.SchemeName := SECURITY_BASIC_NAME;
lSecurityDefsBasic.Description := 'Send username and password for authentication';
ASwagDoc.SecurityDefinitions.Add(lSecurityDefsBasic);
end;
if Assigned(lJWTMiddleware) then
begin
lRttiContext := TRttiContext.Create;
try
lObjType := lRttiContext.GetType(lJWTMiddleware.ClassInfo);
lJwtUrlField := lObjType.GetField('FLoginURLSegment');
if Assigned(lJwtUrlField) then
begin
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, '/');
// Path operation Middleware JWT
ASwagDoc.Paths.Add(TMVCSwagger.GetJWTAuthenticationPath(lJwtUrlSegment,
lJWTMiddleware.UserNameHeaderName, lJWTMiddleware.PasswordHeaderName));
end;
finally
lRttiContext.Free;
end;
end;
// 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;
end;
2019-07-27 20:23:48 +02:00
procedure TMVCSwaggerMiddleware.DocumentApiSettings(AContext: TWebContext; ASwagDoc: TSwagDoc);
var
lSwagSchemes: TSwagTransferProtocolSchemes;
2019-07-27 20:23:48 +02:00
begin
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
ASwagDoc.BasePath := fBasePath;
2019-07-27 20:23:48 +02:00
if ASwagDoc.BasePath.IsEmpty then
begin
ASwagDoc.BasePath := fEngine.Config[TMVCConfigKey.PathPrefix];
end;
if ASwagDoc.BasePath.IsEmpty then
begin
2019-07-27 20:23:48 +02:00
ASwagDoc.BasePath := '/';
end;
2019-07-27 20:23:48 +02: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;
procedure TMVCSwaggerMiddleware.OnAfterControllerAction(AContext: TWebContext;
const AControllerQualifiedClassName: string; const AActionName: string;
const AHandled: Boolean);
2019-07-27 20:23:48 +02:00
begin
// 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
// do nothing
2019-07-27 20:23:48 +02:00
end;
procedure TMVCSwaggerMiddleware.OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
var
LSwagDoc: TSwagDoc;
begin
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);
DocumentApiAuthentication(LSwagDoc);
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;
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
lPathComparer: IComparer<TSwagPath>;
lOperationComparer: IComparer<TSwagPathOperation>;
lSwagPath: TSwagPath;
{$IF not defined(RIOORBETTER)}
lSwagPathList: TArray<TSwagPath>;
lSwagOperationList: TArray<TSwagPathOperation>;
{$ENDIF}
2020-01-03 20:49:53 +01:00
begin
// Sort paths
lPathComparer := TDelegatedComparer<TSwagPath>.Create(
function(const Left, Right: TSwagPath): Integer
begin
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
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);
{$IF defined(RIOORBETTER)}
ASwagDoc.Paths.Sort(lPathComparer);
{$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}
// 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
{$IF defined(RIOORBETTER)}
lSwagPath.Operations.Sort(lOperationComparer);
{$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}
end;
2020-01-03 20:49:53 +01:00
end;
2019-07-27 20:23:48 +02:00
end.