Swagger,在生成操作时忽略属性。为了解决属性类型的冲突

huangapple go评论138阅读模式
英文:

Swagger, ignore property when generating actions. To resolve conflict for Property Type

问题

我正在使用ASP.Net Core上的Swashbuckle 5.6.3。

我遇到了这个错误:
生成操作时出错

追踪问题,原因是响应属性的类型发生了冲突:

public class Product {
    public string name {get; set;}
    ..... 更多属性 .....
    public Reverb.Product ReverbProduct {get; set;} // 这里是我的问题
}

我们同时有ProductReverb.Product属性类型,这引起了问题。

Reverb.Product类在我的应用程序内部使用,我不需要将其暴露给API,所以我编写了一个使用属性的模式过滤器:

[AttributeUsage(AttributeTargets.Property)]
public class SwaggerExcludeAttribute : Attribute
{
}

public class SwaggerExcludeFilter : ISchemaFilter
{
    private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        var type = context?.Type; 
        if (schema?.Properties == null || type == null)
            return;

        var excludedProperties = type.GetProperties()
                                     .Where(t =>
                                            t.GetCustomAttribute<SwaggerExcludeAttribute>()
                                            != null);

        foreach (var excludedProperty in excludedProperties)
        {
            if (schema.Properties.ContainsKey(excludedProperty.Name))
                schema.Properties.Remove(excludedProperty.Name);
        }
        l.Report(schema.Dump(false), AlwaysLog: true);
    }
}

这段代码是根据我在SO和其他地方找到的各种帮助编写的(存在一些版本冲突,但已解决...!)

然而,这并没有解决我的错误。

所以,我猜想虽然SchemaFilter可以阻止这个属性暴露给我的API,但它不能阻止Swashbuckle在生成操作时尝试解析这个属性类型。

有没有办法完全忽略这个属性,而不仅仅在模式中忽略它?

英文:

I am using Swashbuckle 5.6.3 on ASP.Net Core.

I'm getting this error:
Failed to generate Operation for action

Tracing the problem, it is because I have a conflict on the property type of the response:

public class Product {
    public string name {get; set;}
    ..... more properties....
    public Reverb.Product ReverbProduct {get; set;} // HERE IS MY PROBLEM
}

We have both Product and Reverb.Product property types, which it's not liking.

The class Reverb.Product is used internally within my application and I have no need for it to exposed to the API, so I have written a schema filter using an Attribute:

[AttributeUsage(AttributeTargets.Property)]
public class SwaggerExcludeAttribute : Attribute
{
}

public class SwaggerExcludeFilter : ISchemaFilter
{
    private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        var type = context?.Type; 
        if (schema?.Properties == null || type == null)
            return;

        var excludedProperties = type.GetProperties()
                                     .Where(t =>
                                            t.GetCustomAttribute<SwaggerExcludeAttribute>()
                                            != null);

        foreach (var excludedProperty in excludedProperties)
        {
            if (schema.Properties.ContainsKey(excludedProperty.Name))
                schema.Properties.Remove(excludedProperty.Name);
        }
        l.Report(schema.Dump(false), AlwaysLog: true);
    }
}

This code was written using various help I found on SO and others (had some version conflict, but resolved...!)

However, this doesn't fix my error.

So, I guess that whilst the SchemaFilter might stop this property being exposed to my API, it is not stopping Swashbuckle from trying to resolve this property type when generating operations.

Is there any way to make it ignore this property altogether, not just in the schema?

答案1

得分: 0

我在这个问题上找到了一个解决方案(CustomSchemaIds):

https://stackoverflow.com/questions/46071513/swagger-error-conflicting-schemaids-duplicate-schemaids-detected-for-types-a-a

services.ConfigureSwaggerGen(options =>
{
   // 在这里进行自定义配置
   ...
     // 用于.NET Core的UseFullTypeNameInSchemaIds替代方案
    options.CustomSchemaIds(x => x.FullName);
});

然而,这会完全限定属性,而不是忽略它。

所以我仍然没有一个可行的解决方案来解决那个问题。

英文:

I found a solution (CustomSchemaIds) on this question:

https://stackoverflow.com/questions/46071513/swagger-error-conflicting-schemaids-duplicate-schemaids-detected-for-types-a-a

services.ConfigureSwaggerGen(options =>
{
   // your custom configuration goes here
   ...
     // UseFullTypeNameInSchemaIds replacement for .NET Core
    options.CustomSchemaIds(x => x.FullName);
});

However this fully qualifies the property, rather than ignoring it.

So I still don't have a viable solution to that question.

huangapple
  • 本文由 发表于 2023年7月24日 19:06:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定