ShP
ShP

Reputation: 1143

C# ServiceStack Attribute and Swagger UI - List of complex objects

What would be the proper way of using annotation in C# for Swagger-UI in order to have a definition of complex objects listed.

For example, if we have something like this:

  [ApiMember(Name = "Foo", Description = "Some description", 
DataType = "List<Foo>", IsRequired = true)]    
public List<Foo> Foo {get; set;}

So, if I use it like that, swagger-ui will just show in documentation that this is a List but not link or anything to Foo.

I mean, it's shown properly in model \ schema, but just not listed in the field definition, is that how it's suppose to work, or it can be changed to point to exact data structure which is expected in the list \ array?

EDIT Here's the full DTO sample:

[Route("/warranties", "POST", Summary = "POST New Warrantty", Notes = "Create new warranty into our system")]
public class CreateWarrantyRequest : IReturn<ApiResponse>
{
    [ApiMember(Name = "CoverageId", Description = "Coverage Id", DataType = "int", IsRequired = true)]
    public string CoverageId { get; set; }

    [ApiMember(Name = "WarrantyProducts", Description = "Warranty Products", DataType = "List<WarrantyProduct>", IsRequired = true)]
    public List<WarrantyProduct> WarrantyProducts { get; set; }
}

public class WarrantyProduct
{
    [ApiMember(Name = "Manufacturer", Description = "Manufacturer Name", DataType = "string", IsRequired = true)]
    public string Manufacturer { get; set; }
    [ApiMember(Name = "ProductType ", Description = "Product Type", DataType = "ProductType", IsRequired = true)]
    public ProductType ProductType { get; set; }
    [ApiMember(Name = "SerialNumber", Description = "Serial Number", DataType = "string", IsRequired = true)]
    public string SerialNumber { get; set; }
    [ApiMember(Name = "PurchasePrice", Description = "Purchase Price", DataType = "decimal", IsRequired = true)]
    public decimal PurchasePrice { get; set; }    
}

And this is how it looks with SwaggerFeature: enter image description here

And this is how it looks with OpenApiFeature: enter image description here

Upvotes: 1

Views: 1752

Answers (1)

mythz
mythz

Reputation: 143284

The DataType is for specifying a known type. Defining complex Types like List<T> is done by specifying an array Type with its items referencing the Complex Type schema, e.g:

{
  "description": "A complex object array response",
  "schema": {
    "type": "array",
    "items": {
      "$ref": "#/definitions/VeryComplexType"
    }
  }
}

This is what ServiceStack does automatically without specifying the DataType, Complex Types can't be defined using [ApiMember].

Upvotes: 2

Related Questions