REST API hierarchical classes

Hi,

I’m implementing a REST API using the HTTPAPI stuff. All is going fairly well but I’ve hit a problem with hierarchical “nested” classes.

For example, I need an API which allows invoices to be sent. These need to consist of a parent invoice containing properties and then a list of items.

A very simplified version of my class declarations is as follows:

TSalesInvoiceItem = class(TROComplexType)
private
FQuantity : double;
FUnitPrice : double;
published
property Quantity : double read FQuantity write FQuantity;
property UnitPrice : double read FUnitPrice write FUnitPrice;
end;
TSalesInvoiceItems = TROArray;

TSalesInvoice = class(TROComplexType)
private
FInvoiceNumber : integer;
FOrderNumber : string;

FItems : TSalesInvoiceItems;
published
property InvoiceNumber : integer read FInvoiceNumber write FInvoiceNumber;
property OrderNumber : string read FOrderNumber write FOrderNumber;

property Items : TSalesInvoiceItems read FItems;
end;

(apologies for the formatting, I can never work out how to include properly formatted code clips)

I then have a method, which accepts a parameter of type TSalesInvoice.

This all compiles fine but, when I import the generated swagger interface into Postman, the body of this API contains only the JSON representation of the TSalesInvoice class with no sign of the “Items” property.

Calling the API works but the FItems member variable is NIL.

I tried manually adding an “Items” element to the JSON, as an array containing one JSON object with the correct properties for the TSalesInvoiceItem class but it is simply ignored and the FItems variable is still NIL when the function is called.

How can I implement this solution? Thanks.

Hi,

it works as expected with

 TSalesInvoiceItem = class(TROComplexType)
  private
    fQuantity: Double;
    fUnitPrice: Double;
  published
    property Quantity: Double read fQuantity write fQuantity;
    property UnitPrice: Double read fUnitPrice write fUnitPrice;
  end;

  TSalesInvoiceItemCollection = class(TROCollection<TSalesInvoiceItem>);

  TSalesInvoice = class(TROComplexType)
  private
    fInvoiceNumber: Integer;
    fOrderNumber: UnicodeString;
    fItems: TSalesInvoiceItems;
    function GetItems: TSalesInvoiceItems;
  protected
    property int_Items: TSalesInvoiceItems read fItems;
  published
    property InvoiceNumber: Integer read fInvoiceNumber write fInvoiceNumber;
    property OrderNumber: UnicodeString read fOrderNumber write fOrderNumber;
    property Items: TSalesInvoiceItems read GetItems write fItems;
  end;

  TSalesInvoiceCollection = class(TROCollection<TSalesInvoice>);

  TSalesInvoiceItems = class(TROArray<TSalesInvoiceItem>);

generated json:

{
  "swagger": "2.0",
  "info": {
    "title": "NewProjectLibrary",
    "version": "1.0.0"
  },
  "host": "localhost:8099",
  "basePath": "/api",
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/NewService/NewMethod": {
      "post": {
        "parameters": [
          {
            "name": "NewServiceNewMethodRequest",
            "in": "body",
            "schema": {
              "$ref": "#/definitions/NewServiceNewMethodRequest"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The method call completed successfully"
          },
          "400": {
            "description": "Bad Request"
          },
          "500": {
            "description": "Internal Server Error"
          }
        }
      }
    }
  },
  "definitions": {
    "NewServiceNewMethodRequest": {
      "type": "object",
      "properties": {
        "NewParam": {
          "$ref": "#/definitions/TSalesInvoice"
        }
      }
    },
    "TSalesInvoice": {
      "type": "object",
      "properties": {
        "InvoiceNumber": {
          "type": "integer",
          "format": "int32"
        },
        "Items": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/TSalesInvoiceItem"
          }
        },
        "OrderNumber": {
          "type": "string"
        }
      }
    },
    "TSalesInvoiceItem": {
      "type": "object",
      "properties": {
        "Quantity": {
          "type": "number",
          "format": "double"
        },
        "UnitPrice": {
          "type": "number",
          "format": "double"
        }
      }
    }
  }
}

I’ve attached .rodl for reference
NewProjectLibrary.rodl (1.4 KB)


you can put your code into ``` like

```
code
```

Thanks for the quick reply, I’ll give that a try