简体   繁体   中英

How to configure swashbuckle correct for polymorphism

I have a problem to get the right OpenApi definition aftr update from 5.0.0 to 5.4.1

We had custom Polymorphism filter with 5.0.0 version, but they does not work correct with latest one. So I removed them and started to use GeneratePolymorphicSchemas(). It does what I need for our polymorphic models but not just for them. We have also some other abstract and concrete classes, where we don't need type discriminator. I tried different configurations but without any success. Either the generated definition is wrong or I get error on swagger UI or a server 503 error.

Link to the sample project Sample project

Here are my polimorhic models

namespace SwashbuckleTest.Models
{
    public interface ITypeDiscriminator
    {
        string TypeDiscriminator { get; }
    }

    public abstract class SurveyStep : ITypeDiscriminator
    {
        public virtual string Id { get; set; }
        public string TypeDiscriminator => GetType().Name;
    }

    public abstract class SurveyStepResult : ITypeDiscriminator
    {
        public string Id { get; set; }

        public string TypeDiscriminator => GetType().Name;
    }

    public class BoolStep : SurveyStep
    {
        private string _id;

        public BoolStep()
        {
            ResultObject = new BoolStepResult();
        }

        public override string Id
        {
            get => _id;
            set
            {
                _id = value;
                ResultObject.Id = value;
            }
        }

        public string Question { get; set; }

        public BoolStepResult ResultObject { get; }
    }

    public class BoolStepResult : SurveyStepResult
    {
        public bool Value { get; set; }
    }
}

Here other models

namespace SwashbuckleTest.Models
{
    public abstract class SomeBaseModel
    {
        public string BaseValue { get; set; }
    }

    public class SomeConcreteModel : SomeBaseModel
    {
        public int ConcreteValue { get; set; }
    }
}

and configurations I have tried

options.UseAllOfToExtendReferenceSchemas();
options.GeneratePolymorphicSchemas(t =>
{
    var types = t.Is<SurveyStep>() ? new List<Type>() {typeof(BoolStep)}
        : t.Is<SurveyStepResult>() ? new List<Type>() {typeof(BoolStepResult)}
        : null;
    return types;
} , t => t.Is<ITypeDiscriminator>() ? nameof(ITypeDiscriminator.TypeDiscriminator).ToCamelCase() : null);

// or
options.GeneratePolymorphicSchemas(discriminatorSelector: t => t.Is<ITypeDiscriminator>() ? nameof(ITypeDiscriminator.TypeDiscriminator).ToCamelCase() : null);

I found the problem by my self.

The Is<> extension method does not filter abstract classes so we got here endless recursion.

It helped us to generate swagger.json, but we got other problems, that are little bit deeper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM