自定义生成的模型名称 - Swagger UI [英] Customize generated model names - Swagger UI

查看:36
本文介绍了自定义生成的模型名称 - Swagger UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调整自动生成的 Swagger 定义中使用的模型的displayName".

这只会影响 Swagger 名称,这意味着代码中的命名空间将保持不变,而从 Swagger UI 查看模型时,您会看到自定义名称.

目前,从代码返回的模型名称是一个命名空间,看起来像这样:bcdef,我想向代码添加一个属性并屏蔽"Swagger 文档的名称,以便在生成文档/Swagger 定义时,它将显示为 CustomSwaggerName 而不是.

我有一些 API (C#) 使用的工具包括 Swashbuckle(首选)和 SwaggerGen,但现在,我只想让它在或者,如果可能的话.

我尝试使用看起来正确的属性:

[ResponseType(typeof(Company)),DisplayName("NewCompany")][SwaggerResponse(200,"NewCompany",typeof(object))]

没有运气.我还浏览了

我知道这可能看起来像一个奇怪的用例,但它是为我们的 AWS API Gateway 自动化编写的工具,它将使用 Swagger 定义进行一些比较.

解决方案

(.net core 的帐号:)
我会将类的显示属性与自定义 swagger 模式结合起来:

[DisplayName("NewCompany")]公开课公司{}

然后在启动:

services.AddSwaggerGen(c =>{c.CustomSchemaIds(x => x.GetCustomAttributes().SingleOrDefault().DisplayName);});

另见这个答案

I'm trying to adjust the "displayName" of the model being used in an automatically generated Swagger definition.

This will only affect the Swagger names, meaning the namespace in code would be left untouched, whilst when looking at the model from Swagger UI, you'd see a custom name.

Currently, the model name being returned from code is a namespace and looks something like this: b.c.d.e.f, I would like to add an attribute to the code and "mask" the name for the Swagger docs, so that when the documentation / Swagger definition gets generated it'll be displayed as CustomSwaggerName rather.

I have a few API's (C#) using tools that include Swashbuckle (preferred) and SwaggerGen, but right now, I'd just like to get it working in either, if at all possible.

I've tried using attributes that seem to look correct:

[ResponseType(typeof(Company)),DisplayName("NewCompany")]
[SwaggerResponse(200,"NewCompany",typeof(object))]

With no luck. I also browsed the SwashBuckle git repo, hoping to find something.

An image that should help further explain what i'm trying to achieve.

I know this might seem like a strange use case but it's for a tool being written for our AWS API Gateway automation, which will use the Swagger definition for some comparisons.

解决方案

(Accounts for .net core:)
I would combine the display attribute of the class with a custom swagger schema:

[DisplayName("NewCompany")]
public class Company
{
}

and then in Startup:

services.AddSwaggerGen(c =>
{                     
    c.CustomSchemaIds(x => x.GetCustomAttributes<DisplayNameAttribute>().SingleOrDefault().DisplayName);
});

see also this answer

这篇关于自定义生成的模型名称 - Swagger UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆