我有一个带有 openapi 配置的函数应用程序,我想禁用模型部分
我已经在项目中配置了openApi
public static async Task Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(worker =>
worker.UseNewtonsoftJson())
.ConfigureOpenApi()
}
在函数中我定义了开放的API属性
[OpenApiOperation(operationId: nameof(TestNotificationHttpFunction), tags: new[] { "Test Integration API" }, Summary = "Queue Test Notification", Description = "This API Integrates TestOperations.")]
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(TestNotification), Required = true, Description = "TestNotification to process")]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, In = OpenApiSecurityLocationType.Header, Name = "x-functions-key")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(bool), Description = "testNotification Queued Or Not")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.BadRequest, Description = "The operation was not completed successfully")]
我想禁用第模型部分,可以吗?
在Api中我已经像这样删除了
app.UseSwaggerUI(options =>
{
options.DefaultModelsExpandDepth(-1);
}
我不知道如何在天蓝色功能中禁用
我们正在使用进程外/隔离模型,并且“OpenApi__HideSwaggerUI”确实无法像 Dayan Ruiz 所说的那样工作。添加配置设置对我们来说也不起作用。
我通过在 Swagger 中实现自定义授权来修复此问题,该授权仅检查“OpenApi__HideSwaggerUI”标志,并在其禁用时返回错误消息。它的好处是,如果 Swagger 本身解决了这个问题,它(可能)不会干扰这个解决方案..
为自定义授权添加 DI 注册,传入“OpenApi__HideSwaggerUI”的值: 程序.cs
bool.TryParse(configuration["Values:OpenApi__HideSwaggerUI"], out bool hideSwagger);
services.AddSingleton<IOpenApiHttpTriggerAuthorization>(_ => new DisableOpenApiHttpTriggerAuthorization(hideSwagger));
禁用OpenApiHttpTriggerAuthorization.cs
公共类DisableOpenApiHttpTriggerAuthorization:DefaultOpenApiHttpTriggerAuthorization { 私人只读布尔_hideSwagger;
public DisableOpenApiHttpTriggerAuthorization(bool hideSwagger)
{
_hideSwagger = hideSwagger;
}
public override async Task<OpenApiAuthorizationResult> AuthorizeAsync(IHttpRequestDataObject req)
{
if (_hideSwagger)
{
var result = new OpenApiAuthorizationResult()
{
StatusCode = HttpStatusCode.Forbidden,
ContentType = "text/plain",
Payload = "OpenApi is disabled on this environment",
};
return await Task.FromResult(result).ConfigureAwait(false);
}
return await base.AuthorizeAsync(req);
}
}
如果您想
disable it locally
使用 Azure Functions:
local.settings.json
Values
部分。"OpenApi__HideSwaggerUI": "true"
你应该有这样的东西:
现在,如果您进入 swagger 页面,您将看到
HTTP ERROR 404
如果您愿意
disable it in production
您可以:
"OpenApi__HideSwaggerUI": "true"
现在服务器重启后你就可以进入swagger页面了,你会看到
HTTP ERROR 404
这里是完整的文档: https://github.com/Azure/azure-functions-openapi-extension/blob/main/docs/openapi.md