仿制药的宣传命名问题

发布于 2025-02-11 15:48:42 字数 654 浏览 2 评论 0原文

我希望Swagger为通用子模型生成单独生成模型,如下所示:

public class TheModel<T>
{
    public IEnumerable<T> GenericModel { get; set; }
}

public class GenericModel
{
}

public class AnotherGenericModel
{
}

Swagger生成以下模式:

  • genericModel
  • genericModelThemodelthemodelthemodel
  • entergenericModel entergenericModel
  • enternGenericModelTheModelThemas shemodecmodel

themas be:

  • generiCmodel
  • themas the evenericmodel themodel&lt;
  • 另一个genericModel
  • themodel&lt; enternGenericModel&gt;

我想更改创建的模式更改为预期的模式。
这可以用swashbuckle完成吗?

I want swagger to generate models seperately for generic sub models as shown in the example below:

public class TheModel<T>
{
    public IEnumerable<T> GenericModel { get; set; }
}

public class GenericModel
{
}

public class AnotherGenericModel
{
}

Swagger generates the following schemas:

  • GenericModel
  • GenericModelTheModel
  • AnotherGenericModel
  • AnotherGenericModelTheModel

What I want the schemas to be:

  • GenericModel
  • TheModel<GenericModel>
  • AnotherGenericModel
  • TheModel<AnotherGenericModel>

I want to change the schemas created changed to the expected schemas.
Can this be done with Swashbuckle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2025-02-18 15:48:42

我解决了自己的问题,并提出了以下解决方案:

public class GenericFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        var type = context.Type;

        if (type.IsGenericType == false)
            return;

        schema.Title = $"{type.Name[0..^2]}<{type.GenericTypeArguments[0].Name}>";
    }
}

并将其添加到startup.cs swaggergenoptions

options.SchemaFilter<GenericFilter>();

I have solved my own question and came up with the following solution shown below:

public class GenericFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        var type = context.Type;

        if (type.IsGenericType == false)
            return;

        schema.Title = 
quot;{type.Name[0..^2]}<{type.GenericTypeArguments[0].Name}>";
    }
}

And add this to the Startup.cs SwaggerGenOptions

options.SchemaFilter<GenericFilter>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文