Azure Function OpenApi忽略System.Text.Json.JsonPropertyName属性问题
问题:Azure Functions OpenAPI 生成忽略 System.Text.Json 特性配置
我有一个HTTP触发的Azure Function,接口返回时通过WriteAsJsonAsync(来自Microsoft.Azure.Functions.Worker.Http)使用System.Text.Json序列化,实体类X用[System.Text.Json.Serialization.JsonPropertyName]指定了字段名,返回数据格式正确,但生成的OpenAPI Schema却忽略了这个特性,依然使用属性的原名combinedValue;而改用Newtonsoft.Json.JsonPropertyAttribute时Schema生成正常。我不想混用两种序列化方式,需要让OpenAPI强制使用System.Text.Json的配置。
相关代码与配置
1. Azure Function 代码
[OpenApiOperation(operationId: "GetX", tags: new[] { nameof(GetXHttpTrigger) })] [OpenApiParameter(name: "id", In = ParameterLocation.Path, Required = true, Type = typeof(Guid), Description = "X id.")] [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(X), Description = "The OK response")] [OpenApiResponseWithBody(statusCode: HttpStatusCode.NotFound, contentType: "application/json", bodyType: typeof(StatusMessage), Description = "The NotFound response")] [Function(nameof(GetXHttpTrigger))] public async Task<HttpResponseData> Run( [HttpTrigger( AuthorizationLevel.Function, "get", Route = "Xs/{id}")] HttpRequestData req, Guid id, CancellationToken cancellationToken)
2. 实体类X定义
public class X { [System.Text.Json.Serialization.JsonPropertyName("combined_value")] public string CombinedValue { get; set; } // 其他属性 }
3. 接口返回代码
var response = req.CreateResponse(HttpStatusCode.OK); await response.WriteAsJsonAsync<X>(xData); // WriteAsJson 是 Microsoft.Azure.Functions.Worker.Http 的扩展方法
4. 生成的异常OpenAPI Schema(错误使用属性原名)
x: type: object properties: combinedValue: type: string
5. 项目环境配置
var host = Host.CreateDefaultBuilder() .ConfigureFunctionsWorkerDefaults((context, builder) => { // 无额外配置 }) .ConfigureOpenApi()
6. NuGet包与项目配置
NuGet引用
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.19.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.15.1" /> <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.OpenApi" Version="1.5.1" />
项目文件配置
<TargetFramework>net6.0</TargetFramework> <AzureFunctionsVersion>v4</AzureFunctionsVersion>
解决方案:配置OpenAPI使用System.Text.Json序列化器
默认情况下,Microsoft.Azure.Functions.Worker.Extensions.OpenApi包默认使用Newtonsoft.Json解析实体特性,要切换到System.Text.Json,需要在ConfigureOpenApi时指定序列化器配置:
修改Host配置代码如下:
var host = Host.CreateDefaultBuilder() .ConfigureFunctionsWorkerDefaults((context, builder) => { // 无额外配置 }) .ConfigureOpenApi(options => { // 指定使用System.Text.Json的序列化规则 options.SerializerSettings = new OpenApiSerializerSettings { UseSystemTextJson = true }; });
效果说明
- 设置
UseSystemTextJson = true后,OpenAPI生成器会读取System.Text.Json.Serialization命名空间下的特性(包括JsonPropertyName),生成符合配置的Schema字段名。 - 无需修改实体类的特性,保持原有的
[System.Text.Json.Serialization.JsonPropertyName]即可。
修改后生成的OpenAPI Schema会正确显示:
x: type: object properties: combined_value: type: string
内容的提问来源于stack exchange,提问作者dododo
相关产品推荐
相关产品推荐

