Tarray vs TList

Hi,

I tried to use TArray instead of TList from:
https://support.tmssoftware.com/t/json-value-when-function-response-is-tlist-myclasstype/14706

, but the answer is still with value,(below answer)

{
  "value": [
    {
      "$id": 1,
      "id": "067D821D-684E-472B-AD68-E4A6FFCFC098",
      "testvar": "",
      "OrganisationName": "ORG1"
    },
    {
      "$id": 2,
      "id": "EEA94E66-6648-4EAD-8DD9-0BA1D7A69735",
      "testvar": "",
      "OrganisationName": "ORG2"
    }
  ]
}

I would need:

[
    {
      "$id": 1,
      "id": "067D821D-684E-472B-AD68-E4A6FFCFC098",
      "testvar": "",
      "OrganisationName": "ORG1"
    },
    {
      "$id": 2,
      "id": "EEA94E66-6648-4EAD-8DD9-0BA1D7A69735",
      "testvar": "",
      "OrganisationName": "ORG2"
    }
]
    [Route('organisations-get')]
    [HttpGet]     function GetOrganisations:TArray<T_Organisation>;

function TAVSessionService.GetOrganisations:TArray<T_Organisation>;
begin
  LogServerRequest(TXDataOperationContext.Current.Request);
  Setlength(Result,2);
  Result[0] := T_Organisation.Create;
  Result[0].OrganisationName := 'ORG1';
  Result[0].Id :=  StringToGUID('{067D821D-684E-472B-AD68-E4A6FFCFC098}');
  Result[1] := T_Organisation.Create;
  Result[1].OrganisationName := 'ORG2';
  Result[1].Id :=  StringToGUID('{EEA94E66-6648-4EAD-8DD9-0BA1D7A69735}');
  //TrowNotImplemented;
end;

Second question is how to ignore from JSON response testvar, I tried [XDataExcludeProperty, JsonIgnore]

  [Entity, Automapping]
  T_Organisation = class
  strict private
    Fid: TGUID;
    [XDataExcludeProperty, JsonIgnore]
    Ftestvar : string;
  public
    OrganisationName                         : string;
    property Id: TGUID read FId write FId;
    [XDataExcludeProperty, JsonIgnore]
    property testvar:string read Ftestvar write Ftestvar;
  end;

Thanks a lot,
Mihai

Hi there,

Since T_Organisation is an Entity you just need XDataExcludeProperty (and for the Ftestvar field only). Did you include XData.Model.Attributes in uses clause ?

Regards,

1 Like

I added XData.Model.Attributes in uses and now It works.

Thanks,
Mihai

1 Like

I'm sorry, I fixed the answer there. It was incorrect, indeed.

1 Like

Thanks a lot,
It works now,

I just added content type:
TXDataOperationContext.Current.Response.Headers.AddValue('content-type','application/json');

uses {...}, Bcl.Json;
...
function TMyService.Data: TStream;
var
  Data: TArray<string>;
begin
  Result := TMemoryStream.Create;
  TXDataOperationContext.Current.Handler.ManagedObjects.Add(Result);
  TXDataOperationContext.Current.Response.Headers.AddValue('content-type','application/json');

  SetLength(Data, 2);
  Data[0] := 'First';
  Data[1] := 'Second';
  TJson.SerializeAs<TArray<string>>(Data, Result);
end;

Is there any way to put a model in Swagger, because it works ok, but I cannot document the response of TStream in swagger.

It stays as File.

Also how can I serve these files internally

https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js
https://unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js
https://unpkg.com/swagger-ui-dist@3/swagger-ui.css

Assuming that I do not have Internet access when running swagger.

The only way to do that is if you create your own Swagger endpoint. You can open unit XData.OpenApi.Service.Internal.pas and check the source code. It's simply yet another XData service operation contract and implementation. You can copy and paste such code and create your own service operation, and then after you retrieve the documentation object in this line:

    Doc := Builder.GetDocument;

You can modify the Doc object as you wish, modifying the type of the response, for example. That of course will require a deeper knowledge of Swagger and the underlying XData objects representing it.

That is similar for the Swagger UI files. It's totally separated from the swagger.json which contains the API specification. You can download the files directly from Swagger UI and serve the files yourself, somehow: GitHub - swagger-api/swagger-ui: Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API..