2019-10-09 23:14:56 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
2019-10-09 23:14:56 +02:00
|
|
|
|
//
|
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
|
//
|
|
|
|
|
// Collaborators on this file:
|
|
|
|
|
// Jo<4A>o Ant<6E>nio Duarte (https://github.com/joaoduarte19)
|
|
|
|
|
//
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// 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,
|
|
|
|
|
Swag.Doc,
|
2019-08-01 22:31:59 +02:00
|
|
|
|
MVCFramework.Swagger.Commons,
|
|
|
|
|
Swag.Doc.SecurityDefinition,
|
|
|
|
|
Swag.Common.Types,
|
|
|
|
|
System.JSON;
|
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;
|
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);
|
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
|
2019-10-09 23:14:56 +02:00
|
|
|
|
constructor Create(const AEngine: TMVCEngine; const ASwaggerInfo: TMVCSwaggerInfo;
|
2019-11-03 16:16:35 +01:00
|
|
|
|
const ASwaggerDocumentationURL: string = '/swagger.json'; const AJWTDescription: string = JWT_DEFAULT_DESCRIPTION;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
const AEnableBasicAuthentication: 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 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,
|
|
|
|
|
MVCFramework.Commons,
|
|
|
|
|
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,
|
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,
|
2020-01-03 20:49:53 +01:00
|
|
|
|
System.Generics.Defaults;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
|
|
|
|
{ TMVCSwaggerMiddleware }
|
|
|
|
|
|
|
|
|
|
constructor TMVCSwaggerMiddleware.Create(const AEngine: TMVCEngine; const ASwaggerInfo: TMVCSwaggerInfo;
|
2019-10-30 16:02:30 +01:00
|
|
|
|
const ASwaggerDocumentationURL, AJWTDescription: string; const AEnableBasicAuthentication: 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;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
destructor TMVCSwaggerMiddleware.Destroy;
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
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;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lRttiContext := TRttiContext.Create;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
try
|
2019-11-27 20:27:07 +01:00
|
|
|
|
for lController in fEngine.Controllers do
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lControllerPath := '';
|
|
|
|
|
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
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lControllerPath := '';
|
2019-07-27 20:23:48 +02:00
|
|
|
|
Break;
|
|
|
|
|
end;
|
2019-11-27 20:27:07 +01:00
|
|
|
|
if lAttr is MVCPathAttribute then
|
|
|
|
|
begin
|
|
|
|
|
lControllerPath := MVCPathAttribute(lAttr).Path;
|
|
|
|
|
end;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-11-27 20:27:07 +01:00
|
|
|
|
if lControllerPath.IsEmpty then
|
2019-07-27 20:23:48 +02:00
|
|
|
|
Continue;
|
|
|
|
|
|
2019-11-27 20:27:07 +01:00
|
|
|
|
for lMethod in lObjType.GetDeclaredMethods do
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lIsIgnoredPath := False;
|
|
|
|
|
lFoundAttr := False;
|
|
|
|
|
lMVCHttpMethods := [];
|
|
|
|
|
lMethodPath := '';
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
2019-11-27 20:27:07 +01:00
|
|
|
|
for lAttr in lMethod.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
|
|
|
|
|
lIsIgnoredPath := True;
|
|
|
|
|
end;
|
|
|
|
|
if lAttr is MVCPathAttribute then
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lMethodPath := MVCPathAttribute(lAttr).Path;
|
|
|
|
|
lFoundAttr := True;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
2019-11-27 20:27:07 +01:00
|
|
|
|
if lAttr is MVCHTTPMethodsAttribute then
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lMVCHttpMethods := MVCHTTPMethodsAttribute(lAttr).MVCHTTPMethods;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-11-27 20:27:07 +01:00
|
|
|
|
if (not lIsIgnoredPath) and lFoundAttr then
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lSwagPath := nil;
|
|
|
|
|
lPathUri := TMVCSwagger.MVCPathToSwagPath(lControllerPath + lMethodPath);
|
|
|
|
|
for lIndex := 0 to Pred(ASwagDoc.Paths.Count) do
|
2019-08-05 16:59:35 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
if SameText(ASwagDoc.Paths[lIndex].Uri, lPathUri) then
|
2019-08-05 16:59:35 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lSwagPath := ASwagDoc.Paths[lIndex];
|
2019-08-05 16:59:35 +02:00
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-11-27 20:27:07 +01:00
|
|
|
|
if not Assigned(lSwagPath) then
|
2019-08-05 16:59:35 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lSwagPath := TSwagPath.Create;
|
|
|
|
|
lSwagPath.Uri := lPathUri;
|
|
|
|
|
ASwagDoc.Paths.Add(lSwagPath);
|
2019-08-05 16:59:35 +02:00
|
|
|
|
end;
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
2019-11-27 20:27:07 +01:00
|
|
|
|
for I in lMVCHttpMethods do
|
2019-07-27 20:23:48 +02:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lSwagPathOp := TSwagPathOperation.Create;
|
2020-10-19 21:44:38 +02:00
|
|
|
|
TMVCSwagger.FillOperationSummary(lSwagPathOp, lMethod, ASwagDoc.Definitions, I);
|
2019-11-27 20:27:07 +01:00
|
|
|
|
if TMVCSwagger.MethodRequiresAuthentication(lMethod, lObjType, lAuthTypeName) then
|
2019-11-03 16:16:35 +01:00
|
|
|
|
begin
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lSwagPathOp.Security.Add(lAuthTypeName);
|
2019-11-03 16:16:35 +01:00
|
|
|
|
end;
|
2020-01-03 19:48:22 +01:00
|
|
|
|
lSwagPathOp.Parameters.AddRange(TMVCSwagger.GetParamsFromMethod(lSwagPath.Uri, lMethod,
|
|
|
|
|
ASwagDoc.Definitions));
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lSwagPathOp.Operation := TMVCSwagger.MVCHttpMethodToSwagPathOperation(I);
|
|
|
|
|
lSwagPath.Operations.Add(lSwagPathOp);
|
2019-07-27 20:23:48 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
2019-11-27 20:27:07 +01:00
|
|
|
|
lRttiContext.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;
|
|
|
|
|
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-08-01 22:31:59 +02:00
|
|
|
|
|
|
|
|
|
// Methods that have the MVCRequiresAuthentication attribute use bearer authentication.
|
2020-03-24 00:42:58 +01:00
|
|
|
|
lSecurityDefsBearer := TSwagSecurityDefinitionApiKey.Create;
|
|
|
|
|
lSecurityDefsBearer.SchemeName := SECURITY_BEARER_NAME;
|
|
|
|
|
lSecurityDefsBearer.InLocation := kilHeader;
|
|
|
|
|
lSecurityDefsBearer.Name := 'Authorization';
|
|
|
|
|
lSecurityDefsBearer.Description := fJWTDescription;
|
|
|
|
|
ASwagDoc.SecurityDefinitions.Add(lSecurityDefsBearer);
|
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;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-07-27 20:23:48 +02:00
|
|
|
|
procedure TMVCSwaggerMiddleware.DocumentApiSettings(AContext: TWebContext; ASwagDoc: TSwagDoc);
|
|
|
|
|
begin
|
|
|
|
|
ASwagDoc.Host := Format('%s:%d', [AContext.Request.RawWebRequest.Host, AContext.Request.RawWebRequest.ServerPort]);
|
2019-11-27 20:27:07 +01:00
|
|
|
|
ASwagDoc.BasePath := fEngine.Config[TMVCConfigKey.PathPrefix];
|
2019-07-27 20:23:48 +02:00
|
|
|
|
|
|
|
|
|
if ASwagDoc.BasePath.IsEmpty then
|
|
|
|
|
ASwagDoc.BasePath := '/';
|
|
|
|
|
|
|
|
|
|
ASwagDoc.Schemes := [tpsHttp, tpsHttps];
|
|
|
|
|
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 AActionName: string;
|
|
|
|
|
const AHandled: Boolean);
|
|
|
|
|
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);
|
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;
|
2020-05-07 23:32:59 +02:00
|
|
|
|
InternalRender(LSwagDoc.SwaggerJson.ToJSON, 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.
|