Swagger customisation

I have an XData console app that generates Swagger. It does not use any Aurelius but just publishes a service and the models that it uses. The object models are in their own unit and the Swagger displays

GFSProxyDtos.TCreateLabelRequest

Is it possible to omit the unit name?

Thanks

Hi @Weetch_Russell, yes, it's possible. It's not trivial, but here is the code. First, you create a class implementing IOpenApiEvents:

uses
  System.StrUtils, XData.OpenApi.Service.Internal, OpenApi.Classes.Path,
  OpenApi.Document, OpenApi.Classes.Operation, Bcl.Json.Converters, XData.Model.Classes,

type
  TMyOpenApiEvents = class(TInterfacedObject, IOpenApiEvents)
  public
    procedure DocumentCreated(Document: TOpenApiDocument);
    procedure GetDefinitionName(var DefinitionName: string; TypeToken: TTypeToken);
    procedure ActionProcessed(Action: TXDataAction; Operation: TOperation; Path: TPathItem);
  end;

{...}

{ TMyOpenApiEvents }

procedure TMyOpenApiEvents.ActionProcessed(Action: TXDataAction; Operation: TOperation; Path: TPathItem);
begin
end;

procedure TMyOpenApiEvents.DocumentCreated(Document: TOpenApiDocument);
begin
end;

procedure TMyOpenApiEvents.GetDefinitionName(var DefinitionName: string; TypeToken: TTypeToken);
const
  Prefix = 'GFSProxyDtos.';
begin
  if StartsText(Prefix, DefinitionName) then
    System.Delete(DefinitionName, 1, Length(Prefix));
end;

Then you set the IOpenApiEvents in the Sparkle context. In your XData server, add a new generic middleware, and in the OnRequest event just use:

procedure TServerModule.XDataServer1GenericRequest(Sender: TObject; Context: THttpServerContext; Next: THttpServerProc);
begin
  Context.SetItem<IOpenApiEvents>(TMyOpenApiEvents.Create);
  Next(Context);
end;

I hope it helps.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.