domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.25k stars 1.31k forks source link

Interface inheritance not working in 5.0.0 #1522

Closed ghost closed 4 years ago

ghost commented 4 years ago

Since 5.0.0-rc5 (rc4 is the latest version with a different behavior) inherited properties are not generated in the model any more:

    public interface IX
    {
        string ASD { get; set; }
    }

    public interface IWeatherForecast : IX
    {
        DateTime Date { get; set; }

        int TemperatureC { get; set; }

        int TemperatureF { get; }

        string Summary { get; set; }
    }

    public class WeatherForecast : IWeatherForecast
    {
        public DateTime Date { get; set; }

        public int TemperatureC { get; set; }

        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

        public string Summary { get; set; }
        public string ASD { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    }

I've taken the default DotNetCore 3.1 WebAPI example and inherited with an extra property "ASD" from an interface. With 5.0.0-rc4 the model has the property, with later versions it is missing. Is this an intended behavior? And if: how to enable inheritance?

Here are the generated models:

5.0.0-rc4:

"IWeatherForecast": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32",
            "readOnly": true
          },
          "summary": {
            "type": "string",
            "nullable": true
          },
          "asd": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false
      }

= 5.0.0-rc5

      "IWeatherForecast": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32",
            "readOnly": true
          },
          "summary": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false
      }
domaindrivendev commented 4 years ago

5.0.0-rc4 generates schemas according to the behavior of the Newtonsoft serializer whereas the official 5.0.0 bases schemas on the behavior of the System.Text.Json (STJ) serializer.

Which serializer are you using? If it’s STJ, have you confirmed it behaves the way you’d expect. That is, independently of Swashbuckle does it spit out the interface inherited properties when you call the op (e.g. via curl, postman etc)?

If the answer to the above is YES, then this is indeed a bug in swagger as it should reflect the serializer behavior exactly

domaindrivendev commented 4 years ago

If you’re using Newtonsoft, then you need to explicitly tell Swashbuckle to honor its behavior as described here https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/README.md#systemtextjson-stj-vs-newtonsoft

ghost commented 4 years ago

Yes, I'm still using Newtonsoft indeed - and this fixed my issue. Thank you very much!