用Swagger隐藏服务(Hiding services with Swagger)
有什么方法可以在进入生产时从swagger UI中隐藏某些服务但是在localhost上运行时会显示?
EX:
/// <summary> /// GET: .../api/SomeController/{id: int} /// </summary> /// <param name="id">int</param> /// <returns></returns> public IHttpActionResult SomeService(int id) { return Ok();}
当我在我的环境中运行并且去swagger UI localhost:12345 / swagger / ui / index我会看到服务文档等,但是当我在http:// someDomain / swagger / ui / index时我不会看见。
我一直在查看文档,但没有找到任何与此相关的内容。
谢谢。
is there any way that some services can be hidden from swagger UI when going into production but when run on localhost be displayed?
EX:
/// <summary> /// GET: .../api/SomeController/{id: int} /// </summary> /// <param name="id">int</param> /// <returns></returns> public IHttpActionResult SomeService(int id) { return Ok();}
When i'm running on my environment and go to swagger UI localhost:12345/swagger/ui/index i will see that service documentation etc, but when i'm on http://someDomain/swagger/ui/index i will not see it.
I've been looking in the documentation but didn't find anything related to this.
Thank you.
原文:https://stackoverflow.com/questions/41037162
最满意答案
您可以创建自己的属性并使用它从swagger中排除该方法或整个控制器:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public class HideInDocsAttribute:Attribute { }
接着:
public class HideInDocsFilter:IDocumentFilter { public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer, IHostingEnvironment env) { if(env.IsEnvironment("Production")) { foreach (var apiDescription in apiExplorer.ApiDescriptions) { if (!apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any() && !apiDescription.ActionDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any()) continue; var route = "/" + apiDescription.Route.RouteTemplate.TrimEnd('/'); swaggerDoc.paths.Remove(route); } } } }
You can create your own attribute and use it to exclude that method or whole controller from swagger:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public class HideInDocsAttribute:Attribute { }
And then:
public class HideInDocsFilter:IDocumentFilter { public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer, IHostingEnvironment env) { if(env.IsEnvironment("Production")) { foreach (var apiDescription in apiExplorer.ApiDescriptions) { if (!apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any() && !apiDescription.ActionDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any()) continue; var route = "/" + apiDescription.Route.RouteTemplate.TrimEnd('/'); swaggerDoc.paths.Remove(route); } } } }
相关问答
更多-
注意,对于Swagger 2.0(OpenAPI Specification 2.0),Parameter对象允许将type设置为file : consumes: - multipart/form-data # and/or application/x-www-form-urlencoded parameters: - name: file in: formData description: The uploaded file data requ ...
-
我刚为Apache Wink做了一个项目。 你可以在下面找到它 https://github.com/zouzias/swagger-jaxrs-wink 我也会尽快将其推向swagger-core样品系列。 I have just made a project for Apache Wink. You can find it below https://github.com/zouzias/swagger-jaxrs-wink I will push it to the swagger-core samp ...
-
我正在使用另一个应用程序来模仿它的服务,它可能运行的是旧版本的JHipster,Swagger ......并且这使问题再次出现,但这次是在Edge上。 删除浏览器的缓存文件可修复Edge和Chrome的错误。 I was using another app to mimic its services and it was probably running older versions of JHipster, Swagger... and that made the problem appear agai ...
-
例 有关Swagger-servlet modeule的更多信息,请参见此处 Example More information on Swagger-servlet modeule can be found here
-
泽西岛2 Swagger配置(Jersey 2 Swagger configuration)[2021-08-12]
实际上,这将是一个与泽西4.1杰克逊库版本有关的问题。 我最终做了什么来完成它的工作: 增加Swagger版本(感谢Ron没有注意到这个新版本) 在{glassfish_install}/glassfish/modules取代最后一个版本(此时为2.6)的Jackson jars在此处找到 删除了我的域( {glassfish_install}/glassfish/domains/domain1/osgi-cache/felix )的{glassfish_install}/glassfish/domains ... -
用Swagger隐藏服务(Hiding services with Swagger)[2021-06-17]
您可以创建自己的属性并使用它从swagger中排除该方法或整个控制器: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public class HideInDocsAttribute:Attribute { } 接着: public class HideInDocsFilter:IDocumentFilter { public void Apply(SwaggerDocument swaggerDoc, Sch ... -
swagger-tools解决了我的问题。 在解析每个swagger文件后,我已经递归地创建了管道。 swagger-tools solved my question. I've created the pipeline recursively after parsing each swagger file.
-
问题的两个部分: 1)现在无法在EasyAPI上启用Swagger。 也就是说,我们不合并也不生成Swagger for EasyAPI,因为所有参数都在代码中定义。 2)如果要启用Swagger,则需要在API上方的app.js文件中将“swagger:true”添加到Azure移动应用程序的初始化程序中。 进入Easy API或Easy Tables并编辑任何API /表。 这将打开编辑器。 转到上面的目录(包含api和tables目录的目录)并在那里编辑app.js文件。 Two parts to y ...
-
导入Swagger端点时出错(Error importing Swagger Endpoint)[2021-09-05]
目前它是GReg-5.1.0的限制。 https://wso2.org/jira/browse/REGISTRY-3195 有一个解决方法,这将在产品的更高版本中正确修复。 It is currently a limitation with GReg-5.1.0. https://wso2.org/jira/browse/REGISTRY-3195 Has a workaround, and this will be properly fixed in a later version of the prod ... -
Swagger问题与Autofac(Swagger issue with Autofac)[2021-03-28]
我解决了官方github资源库Swashbuckle的问题 。 问题是由于OWIN引起的。 我想安装包Swashbuckle.Core 。 I was able to solve the issue fallowing the official github respository Swashbuckle. The problem was due OWIN. I miss installed the package Swashbuckle.Core.