简体   繁体   English

如何为多态性正确配置 swashbuckle

[英]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从 5.0.0 更新到 5.4.1 后,我无法获得正确的 OpenApi 定义

We had custom Polymorphism filter with 5.0.0 version, but they does not work correct with latest one.我们有 5.0.0 版本的自定义多态过滤器,但它们不适用于最新版本。 So I removed them and started to use GeneratePolymorphicSchemas().所以我删除了它们并开始使用 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.生成的定义是错误的,或者我在 swagger UI 上收到错误或服务器 503 错误。

Link to the sample project Sample project链接到示例项目示例项目

Here are my polimorhic models这是我的polimorhic模型

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. Is<>扩展方法不过滤抽象类,所以我们在这里无限递归。

It helped us to generate swagger.json, but we got other problems, that are little bit deeper.它帮助我们生成了 swagger.json,但我们遇到了其他问题,这些问题有点深。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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