Swashbuckle.aspnetcore: GeneratePolymorphicSchemas uses oneOf but not allOf

Created on 22 Mar 2019  路  10Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Hi, i'm testing the new "GeneratePolymorphicSchemas" feature in swashbuckle 5.0.0 rc.

The OpenApi v3 generator creates a oneOf list of all derived classes for the base class. But i would like to have a allOf list in the derived class which includes the reference to the base class. I hope this makes sense.

Is this a feature that is not supported? Or do i have to create my own filter to support this scenario?

Most helpful comment

Support for oneOf, allOf and discriminator added by #1468

All 10 comments

Yes it does make sense but no it鈥檚 not currently supported. It would be possible to implement but does introduce a little more complexity. Can you explain your use case and why this is important just to help me prioritize this enhancement?

Thank you for your prompt reply, very much appreciated! Here is our use case in simplified form.

This are our types:

// the base class for all derived classes
class EntityBase {
    public string Urn { get; set; }
}

// a container for Entity updates
class EntityUpdate {
    public string EntityType { get; set;}
    public EntityBase Entity { get; set;}
}

// more than 20 types that are derived from EntityBase
class Event : EntityBase {}
class Competitor : EntityBase {}
class Competition : EntityBase {}

Our Web API has this method - we get a List of EntityUpdate objects, where each EntityUpdate contains instances of types that are derived from EntityBase (e.g. Event, Competitor, Competition, etc.)

public List<EntityUpdate> GetEntityUpdates(long version) {
   // ....
}

We use Swashbuckle to create OpenApi 3.0 contracts and use the swagger-codegen tool to generate TypeScript files for Angular (https://github.com/swagger-api/swagger-codegen). This is probably a very common use case.

From an API standpoint you could argue that it would be easy to flatten all types. It would work fine without the polymorphic schemas too. But there is a lot of TypeScript code that would benefit from the information that those types are derived from a common base class. The derived classes are not just used for the API, but also in the program logic. Sure you could use DTOs for the APIs and create other (derived) types for the program logic, but we are talking about 20+ types with lots of properites.

class Event extends EntityBase {}
class Competitor extends EntityBase {}
class Comppetitor extends EntityBase {}

I found many people asking for the same feature in the previous versions of Swashbuckle. Some fixed it with a custom filter, but since Swashbuckle 5.0.0 supports polymorphic schemas, it would be awesome to get this out of the box. Thank you very much for your great work!

I wish all issue's were submitted in this tone - makes it all worth while so thankyou!
I can see how this feature would be useful for client generation tools. I'll take a look and see if I can sneak it into the upcoming 5.0 release.

After looking into this a little deeper, it's a little more complex than I thought and so I won't be in a position to include it with the 5.0.0 milestone

Thank you for taking a look at it, and for adding this feature to the backlog.

I'm also voting for this feature as at the moment we have 10 derived classes that should be handled.

Unfortunately i had to switch to NSwag because Swashbuckle does not support this feature.

also a vote for this issue form my side, same problem here :-)

I'm keen to get this added but it's complicated and is holding up the 5.0.0 release, which is super important for ASP.NET Core 3.0 support. So, I'm going to tackle it as a fast follow in v5.1.0.

Support for oneOf, allOf and discriminator added by #1468

Was this page helpful?
0 / 5 - 0 ratings