|
1 |
| -// Licensed to the .NET Foundation under one or more agreements. |
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System.Net.Http;
|
@@ -175,6 +175,7 @@ internal class User : IUser
|
175 | 175 | /// <inheritdoc/>
|
176 | 176 | public string Name { get; set; }
|
177 | 177 | }
|
| 178 | +
|
178 | 179 | """;
|
179 | 180 | var generator = new XmlCommentGenerator();
|
180 | 181 | await SnapshotTestHelper.Verify(source, generator, out var compilation);
|
@@ -260,4 +261,161 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
|
260 | 261 | Assert.Equal("The user's display name.", user.Properties["name"].Description);
|
261 | 262 | });
|
262 | 263 | }
|
| 264 | + |
| 265 | + [Fact] |
| 266 | + public async Task XmlCommentsOnPropertiesShouldApplyToSchemaReferences() |
| 267 | + { |
| 268 | + var source = """ |
| 269 | +using System; |
| 270 | +using Microsoft.AspNetCore.Builder; |
| 271 | +using Microsoft.Extensions.DependencyInjection; |
| 272 | +
|
| 273 | +var builder = WebApplication.CreateBuilder(); |
| 274 | +
|
| 275 | +builder.Services.AddOpenApi(options => { |
| 276 | + var prevCreateSchemaReferenceId = options.CreateSchemaReferenceId; |
| 277 | + options.CreateSchemaReferenceId = (x) => x.Type == typeof(ModelInline) ? null : prevCreateSchemaReferenceId(x); |
| 278 | +}); |
| 279 | +
|
| 280 | +var app = builder.Build(); |
| 281 | +
|
| 282 | +app.MapPost("/example", (RootModel model) => { }); |
| 283 | +
|
| 284 | +app.Run(); |
| 285 | +
|
| 286 | +/// <summary> |
| 287 | +/// Comment on class ModelWithSummary. |
| 288 | +/// </summary> |
| 289 | +/// <example> |
| 290 | +/// { "street": "ModelWithSummaryClass" } |
| 291 | +/// </example> |
| 292 | +public class ModelWithSummary |
| 293 | +{ |
| 294 | + public string Street { get; set; } |
| 295 | +} |
| 296 | +
|
| 297 | +public class ModelWithoutSummary |
| 298 | +{ |
| 299 | + public string Street { get; set; } |
| 300 | +} |
| 301 | +
|
| 302 | +/// <summary> |
| 303 | +/// Comment on class ModelInline. |
| 304 | +/// </summary> |
| 305 | +/// <example> |
| 306 | +/// { "street": "ModelInlineClass" } |
| 307 | +/// </example> |
| 308 | +public class ModelInline |
| 309 | +{ |
| 310 | + public string Street { get; set; } |
| 311 | +} |
| 312 | +
|
| 313 | +/// <summary> |
| 314 | +/// Comment on class RootModel. |
| 315 | +/// </summary> |
| 316 | +/// <example> |
| 317 | +/// { } |
| 318 | +/// </example> |
| 319 | +public class RootModel |
| 320 | +{ |
| 321 | + public ModelWithSummary NoPropertyComment { get; set; } |
| 322 | +
|
| 323 | + /// <summary> |
| 324 | + /// Comment on property ModelWithSummary1. |
| 325 | + /// </summary> |
| 326 | + /// <example> |
| 327 | + /// { "street": "ModelWithSummary1Prop" } |
| 328 | + /// </example> |
| 329 | + public ModelWithSummary ModelWithSummary1 { get; set; } |
| 330 | +
|
| 331 | + /// <summary> |
| 332 | + /// Comment on property ModelWithSummary2. |
| 333 | + /// </summary> |
| 334 | + /// <example> |
| 335 | + /// { "street": "ModelWithSummary2Prop" } |
| 336 | + /// </example> |
| 337 | + public ModelWithSummary ModelWithSummary2 { get; set; } |
| 338 | +
|
| 339 | + /// <summary> |
| 340 | + /// Comment on property ModelWithoutSummary1. |
| 341 | + /// </summary> |
| 342 | + /// <example> |
| 343 | + /// { "street": "ModelWithoutSummary1Prop" } |
| 344 | + /// </example> |
| 345 | + public ModelWithoutSummary ModelWithoutSummary1 { get; set; } |
| 346 | +
|
| 347 | + /// <summary> |
| 348 | + /// Comment on property ModelWithoutSummary2. |
| 349 | + /// </summary> |
| 350 | + /// <example> |
| 351 | + /// { "street": "ModelWithoutSummary2Prop" } |
| 352 | + /// </example> |
| 353 | + public ModelWithoutSummary ModelWithoutSummary2 { get; set; } |
| 354 | +
|
| 355 | + /// <summary> |
| 356 | + /// Comment on property ModelInline1. |
| 357 | + /// </summary> |
| 358 | + /// <example> |
| 359 | + /// { "street": "ModelInline1Prop" } |
| 360 | + /// </example> |
| 361 | + public ModelInline ModelInline1 { get; set; } |
| 362 | +
|
| 363 | + /// <summary> |
| 364 | + /// Comment on property ModelInline2. |
| 365 | + /// </summary> |
| 366 | + /// <example> |
| 367 | + /// { "street": "ModelInline2Prop" } |
| 368 | + /// </example> |
| 369 | + public ModelInline ModelInline2 { get; set; } |
| 370 | +} |
| 371 | +"""; |
| 372 | + var generator = new XmlCommentGenerator(); |
| 373 | + await SnapshotTestHelper.Verify(source, generator, out var compilation); |
| 374 | + await SnapshotTestHelper.VerifyOpenApi(compilation, document => |
| 375 | + { |
| 376 | + var path = document.Paths["/example"].Operations[HttpMethod.Post]; |
| 377 | + var exampleOperationBodySchema = path.RequestBody.Content["application/json"].Schema; |
| 378 | + Assert.Equal("Comment on class RootModel.", exampleOperationBodySchema.Description); |
| 379 | + |
| 380 | + var rootModelSchema = document.Components.Schemas["RootModel"]; |
| 381 | + Assert.Equal("Comment on class RootModel.", rootModelSchema.Description); |
| 382 | + |
| 383 | + var modelWithSummary = document.Components.Schemas["ModelWithSummary"]; |
| 384 | + Assert.Equal("Comment on class ModelWithSummary.", modelWithSummary.Description); |
| 385 | + Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "street": "ModelWithSummaryClass" }"""), modelWithSummary.Example)); |
| 386 | + |
| 387 | + var modelWithoutSummary = document.Components.Schemas["ModelWithoutSummary"]; |
| 388 | + Assert.Null(modelWithoutSummary.Description); |
| 389 | + |
| 390 | + Assert.DoesNotContain("ModelInline", document.Components.Schemas.Keys); |
| 391 | + |
| 392 | + // Check RootModel properties |
| 393 | + var noPropertyCommentProp = Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["noPropertyComment"]); |
| 394 | + Assert.Null(noPropertyCommentProp.Reference.Description); |
| 395 | + |
| 396 | + var modelWithSummary1Prop = Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["modelWithSummary1"]); |
| 397 | + Assert.Equal("Comment on property ModelWithSummary1.", modelWithSummary1Prop.Description); |
| 398 | + Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "street": "ModelWithSummary1Prop" }"""), modelWithSummary1Prop.Examples[0])); |
| 399 | + |
| 400 | + var modelWithSummary2Prop = Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["modelWithSummary2"]); |
| 401 | + Assert.Equal("Comment on property ModelWithSummary2.", modelWithSummary2Prop.Description); |
| 402 | + Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "street": "ModelWithSummary2Prop" }"""), modelWithSummary2Prop.Examples[0])); |
| 403 | + |
| 404 | + var modelWithoutSummary1Prop = Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["modelWithoutSummary1"]); |
| 405 | + Assert.Equal("Comment on property ModelWithoutSummary1.", modelWithoutSummary1Prop.Description); |
| 406 | + Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "street": "ModelWithoutSummary1Prop" }"""), modelWithoutSummary1Prop.Examples[0])); |
| 407 | + |
| 408 | + var modelWithoutSummary2Prop = Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["modelWithoutSummary2"]); |
| 409 | + Assert.Equal("Comment on property ModelWithoutSummary2.", modelWithoutSummary2Prop.Description); |
| 410 | + Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "street": "ModelWithoutSummary2Prop" }"""), modelWithoutSummary2Prop.Examples[0])); |
| 411 | + |
| 412 | + var modelInline1Prop = Assert.IsType<OpenApiSchema>(rootModelSchema.Properties["modelInline1"]); |
| 413 | + Assert.Equal("Comment on property ModelInline1.", modelInline1Prop.Description); |
| 414 | + Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "street": "ModelInline1Prop" }"""), modelInline1Prop.Example)); |
| 415 | + |
| 416 | + var modelInline2Prop = Assert.IsType<OpenApiSchema>(rootModelSchema.Properties["modelInline2"]); |
| 417 | + Assert.Equal("Comment on property ModelInline2.", modelInline2Prop.Description); |
| 418 | + Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "street": "ModelInline2Prop" }"""), modelInline2Prop.Example)); |
| 419 | + }); |
| 420 | + } |
263 | 421 | }
|
0 commit comments