RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.61k stars 1.22k forks source link

Flatten c# interfaces #4893

Open pgpaccuratech opened 1 month ago

pgpaccuratech commented 1 month ago

I have a DB entity that includes a lot of information for various kinds of orders. But when using the REST interface, I would like to filter the irrelevant data away from the API. My idea was to use interfaces to do this. Example:

    public interface IOrderBase
    {
        public int Id { get; set; }
    }

    public interface ISalesOrder : IOrderBase
    {
        public int? CustomerId { get; set; }
    }

    public interface IPurchaseOrder : IOrderBase
    {
        public int? VendorId { get; set; }
    }

    public class OrderEntity : IPurchaseOrder, ISalesOrder
    {
        public int Id { get; set; }
        public int? CustomerId { get; set; }
        public int? VendorId { get; set; }
    }

    [HttpGet("{id}")]
    [Route("purchaseOrder")]
    public IPurchaseOrder GetPurchaseOrder(int id)
    {
        OrderEntity purchaseOrder = // use entity framework to fetch record
        return (IPurchaseOrder)purchaseOrder;
    }

Unfortunately, the API does not include OrderID in the PurchaseOrder. From the schemas

      "PurchaseOrder": {
        "type": "object",
        "x-abstract": true,
        "additionalProperties": false,
        "properties": {
          "vendorId": {
            "type": "integer",
            "format": "int32",
            "nullable": true
          }
        }
      }

I have openApiConfiguration.SchemaSettings.FlattenInheritanceHierarchy = true; in AddOpenApiDocument(), but that does not work.