ITADN

[Bug]: SwaggerSchema annotations are not applied to reference properties

#3857Openmarcotod1410 创建于 2026-03-24
bug
M
marcotod1410commented
### Describe the bug Let's consider the `WeatherForecast` class with two properties that have the SwaggerSchema attribute applied to them: ```cs public class WeatherForecast { public DateOnly Date { get; set; } [SwaggerSchema("Description of TemperatureC")] public int TemperatureC { get; set; } public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); public string? Summary { get; set; } [SwaggerSchema("Description of TestProperty")] public Test? TestProperty { get; set; } = null; } public class Test { public int MyProperty { get; set; } = 123; } ``` The descriptions for the `WeatherForecast.TestProperty` property is not applied when the schema is generated. ### Expected behavior The components section of the swagger.json schema should look like this: ```json "components": { "schemas": { "Test": { "type": "object", "properties": { "myProperty": { "type": "integer", "format": "int32" } }, "additionalProperties": false }, "WeatherForecast": { "type": "object", "properties": { "date": { "type": "string", "format": "date" }, "temperatureC": { "type": "integer", "description": "Description of TemperatureC", "format": "int32" }, "temperatureF": { "type": "integer", "format": "int32", "readOnly": true }, "summary": { "type": "string", "nullable": true }, "testProperty": { "$ref": "#/components/schemas/Test", "description": "Description of TestProperty", } }, "additionalProperties": false } } } ``` ### Actual behavior It looks like this (as you can see, the description is applied only on the `temperatureC` property: ```json "components": { "schemas": { "Test": { "type": "object", "properties": { "myProperty": { "type": "integer", "format": "int32" } }, "additionalProperties": false }, "WeatherForecast": { "type": "object", "properties": { "date": { "type": "string", "format": "date" }, "temperatureC": { "type": "integer", "description": "Description of TemperatureC", "format": "int32" }, "temperatureF": { "type": "integer", "format": "int32", "readOnly": true }, "summary": { "type": "string", "nullable": true }, "testProperty": { "$ref": "#/components/schemas/Test" } }, "additionalProperties": false } } } ``` ### Steps to reproduce Start with the default ASP.NET Web API template, adding the EnableAnnotations hook into the AddSwaggerGen call. By default, the template uses the version 6.6.2 of Swashbuckle.AspNetCore library, but we were able to reproduce this issue also on the newest version. Here's the Program.cs needed to start the project, generated from the default template mentioned earlier, with the addition of the EnableAnnotations call. ```cs namespace TestWebSwagger { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(options => { options.EnableAnnotations(); }); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); } } } ``` ### Exception(s) (if any) _No response_ ### Swashbuckle.AspNetCore version Reproduced on both 6.6.2 and latest version (10.1.5) ### .NET Version .NET 8.0 ### Anything else? _No response_
0 条评论