Base solution for your next web application
Open Closed

Subclasses and Swagger #8922


User avatar
0
area8.tech created

Hello, I have a question about how swagger generates the JSON about the subclasses. I added the

    [JsonConverter(typeof(JsonInheritanceConverter), "discriminator")]
    [KnownType(typeof(SubClass))]

decorators on the superclass and I added the Package NJsonSchema as I see on their documentation, but the generated JSON doesn't have the discriminator field and the subclasses aren't generated, so I cannot use them in the Angular FrontEnd

How can I resolve this? Thank you for your help. Agostino


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    We are using https://github.com/domaindrivendev/Swashbuckle.AspNetCore, did you check its documentation ? If you can't find a solution on its documentation, could you share your entire classes ?

    Thanks,

  • User Avatar
    0
    area8.tech created

    Hello @ismcagdas, thank you for your answer. I tried to add a DocumentFilter and a SchemaFilter as described in https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/451 this page but that code has errors and methods and classes don't match (it seems to be related to an old version of the library). I tried to fix that code but seems too different.

    Then I found this Package NJsonSchema and i added the code

        [JsonConverter(typeof(JsonInheritanceConverter), "discriminator")]
        [KnownType(typeof(Dog))]
        public class Animal { ... }
        
        public class Dog { ... }
    

    and it works, infact the returned JSON has the discriminator field, but it is not added to the Swagger, so NSwag.js does not update the angular "service proxies".

    Any hint? Agostino

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    NJsonSchema is a different library doing similar things like Swashbuckle.AspNetCore. So, you need to document schema filters for Swashbuckle.AspNetCore to solve this issue. Does your project have SwaggerEnumSchemaFilter.cs ? If so, you can cehck its implementation.

    Thanks,