Skip to content

Commit c9805b9

Browse files
committed
Improve XmlComments SchemaReference tests and revert modified unrelated tests
1 parent 12a0766 commit c9805b9

5 files changed

+29
-181
lines changed

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/SchemaTests.cs

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ public async Task SupportsXmlCommentsOnSchemas()
1919
2020
var builder = WebApplication.CreateBuilder();
2121
22-
builder.Services.AddOpenApi(options => {
23-
var prevCreateSchemaReferenceId = options.CreateSchemaReferenceId;
24-
options.CreateSchemaReferenceId = (x) => x.Type == typeof(AddressNested) ? null : prevCreateSchemaReferenceId(x);
25-
});
22+
builder.Services.AddOpenApi();
2623
2724
var app = builder.Build();
2825
@@ -34,7 +31,6 @@ public async Task SupportsXmlCommentsOnSchemas()
3431
app.MapPost("/todo-with-description", (TodoWithDescription todo) => { });
3532
app.MapPost("/type-with-examples", (TypeWithExamples typeWithExamples) => { });
3633
app.MapPost("/user", (User user) => { });
37-
app.MapPost("/company", (Company company) => { });
3834
3935
app.Run();
4036
@@ -180,59 +176,6 @@ internal class User : IUser
180176
public string Name { get; set; }
181177
}
182178
183-
/// <summary>
184-
/// An address.
185-
/// </summary>
186-
public class AddressWithSummary
187-
{
188-
public string Street { get; set; }
189-
}
190-
191-
public class AddressWithoutSummary
192-
{
193-
public string Street { get; set; }
194-
}
195-
196-
/// <summary>
197-
/// An address.
198-
/// </summary>
199-
public class AddressNested
200-
{
201-
public string Street { get; set; }
202-
}
203-
204-
public class Company
205-
{
206-
/// <summary>
207-
/// Billing address.
208-
/// </summary>
209-
public AddressWithSummary BillingAddressClassWithSummary { get; set; }
210-
211-
/// <summary>
212-
/// Billing address.
213-
/// </summary>
214-
public AddressWithoutSummary BillingAddressClassWithoutSummary { get; set; }
215-
216-
/// <summary>
217-
/// Billing address.
218-
/// </summary>
219-
public AddressNested BillingAddressNested { get; set; }
220-
221-
/// <summary>
222-
/// Visiting address.
223-
/// </summary>
224-
public AddressWithSummary VisitingAddressClassWithSummary { get; set; }
225-
226-
/// <summary>
227-
/// Visiting address.
228-
/// </summary>
229-
public AddressWithoutSummary VisitingAddressClassWithoutSummary { get; set; }
230-
231-
/// <summary>
232-
/// Visiting address.
233-
/// </summary>
234-
public AddressNested VisitingAddressNested { get; set; }
235-
}
236179
""";
237180
var generator = new XmlCommentGenerator();
238181
await SnapshotTestHelper.Verify(source, generator, out var compilation);
@@ -316,22 +259,11 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
316259
var user = path.RequestBody.Content["application/json"].Schema;
317260
Assert.Equal("The unique identifier for the user.", user.Properties["id"].Description);
318261
Assert.Equal("The user's display name.", user.Properties["name"].Description);
319-
320-
path = document.Paths["/company"].Operations[HttpMethod.Post];
321-
var company = path.RequestBody.Content["application/json"].Schema;
322-
Assert.Equal("Billing address.", company.Properties["billingAddressNested"].Description);
323-
Assert.Equal("Visiting address.", company.Properties["visitingAddressNested"].Description);
324-
325-
var addressWithSummary = document.Components.Schemas["AddressWithSummary"];
326-
Assert.Equal("An address.", addressWithSummary.Description);
327-
328-
var addressWithoutSummary = document.Components.Schemas["AddressWithoutSummary"];
329-
Assert.Null(addressWithoutSummary.Description);
330262
});
331263
}
332264

333265
[Fact]
334-
public async Task XmlCommentsOnPropertiesShouldNotApplyToReferencedSchemas()
266+
public async Task XmlCommentsOnPropertiesShouldApplyToSchemaReferences()
335267
{
336268
var source = """
337269
using System;
@@ -377,6 +309,8 @@ public class ModelInline
377309
/// </summary>
378310
public class RootModel
379311
{
312+
public ModelWithSummary NoPropertyComment { get; set; }
313+
380314
/// <summary>
381315
/// Comment on property FirstModelWithSummary.
382316
/// </summary>
@@ -426,7 +360,28 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
426360
Assert.Null(modelWithoutSummary.Description);
427361

428362
Assert.DoesNotContain("ModelInline", document.Components.Schemas.Keys);
363+
364+
// Check RootModel properties
365+
var noPropertyCommentReference = Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["noPropertyComment"]);
366+
Assert.Null(noPropertyCommentReference.Reference.Description);
367+
368+
Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["firstModelWithSummary"]);
369+
Assert.Equal("Comment on property FirstModelWithSummary.", rootModelSchema.Properties["firstModelWithSummary"].Description);
370+
371+
Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["firstModelWithoutSummary"]);
372+
Assert.Equal("Comment on property FirstModelWithoutSummary.", rootModelSchema.Properties["firstModelWithoutSummary"].Description);
373+
374+
Assert.IsType<OpenApiSchema>(rootModelSchema.Properties["firstModelInline"]);
429375
Assert.Equal("Comment on property FirstModelInline.", rootModelSchema.Properties["firstModelInline"].Description);
376+
377+
// Verify that comments on the same type override each other
378+
Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["secondModelWithSummary"]);
379+
Assert.Equal("Comment on property FirstModelWithSummary.", rootModelSchema.Properties["firstModelWithSummary"].Description);
380+
381+
Assert.IsType<OpenApiSchemaReference>(rootModelSchema.Properties["secondModelWithoutSummary"]);
382+
Assert.Equal("Comment on property FirstModelWithoutSummary.", rootModelSchema.Properties["firstModelWithoutSummary"].Description);
383+
384+
Assert.IsType<OpenApiSchema>(rootModelSchema.Properties["secondModelInline"]);
430385
Assert.Equal("Comment on property SecondModelInline.", rootModelSchema.Properties["secondModelInline"].Description);
431386
});
432387
}

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/SchemaTests.SupportsXmlCommentsOnSchemas#OpenApiXmlCommentSupport.generated.verified.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ private static Dictionary<string, XmlComment> GenerateCacheEntries()
7676
cache.Add(@"T:ProjectBoard.ProtectedInternalElement", new XmlComment(@"Can find this XML comment.", null, null, null, null, false, null, null, null));
7777
cache.Add(@"T:ProjectRecord", new XmlComment(@"The project that contains Todo items.", null, null, null, null, false, null, [new XmlParameterComment(@"Name", @"The name of the project.", null, false), new XmlParameterComment(@"Description", @"The description of the project.", null, false)], null));
7878
cache.Add(@"T:User", new XmlComment(null, null, null, null, null, false, null, null, null));
79-
cache.Add(@"T:AddressWithSummary", new XmlComment(@"An address.", null, null, null, null, false, null, null, null));
80-
cache.Add(@"T:AddressNested", new XmlComment(@"An address.", null, null, null, null, false, null, null, null));
8179
cache.Add(@"P:ProjectBoard.ProtectedInternalElement.Name", new XmlComment(@"The unique identifier for the element.", null, null, null, null, false, null, null, null));
8280
cache.Add(@"P:ProjectRecord.Name", new XmlComment(@"The name of the project.", null, null, null, null, false, null, null, null));
8381
cache.Add(@"P:ProjectRecord.Description", new XmlComment(@"The description of the project.", null, null, null, null, false, null, null, null));
@@ -102,12 +100,6 @@ private static Dictionary<string, XmlComment> GenerateCacheEntries()
102100
cache.Add(@"P:IUser.Name", new XmlComment(@"The user's display name.", null, null, null, null, false, null, null, null));
103101
cache.Add(@"P:User.Id", new XmlComment(@"The unique identifier for the user.", null, null, null, null, false, null, null, null));
104102
cache.Add(@"P:User.Name", new XmlComment(@"The user's display name.", null, null, null, null, false, null, null, null));
105-
cache.Add(@"P:Company.BillingAddressClassWithSummary", new XmlComment(@"Billing address.", null, null, null, null, false, null, null, null));
106-
cache.Add(@"P:Company.BillingAddressClassWithoutSummary", new XmlComment(@"Billing address.", null, null, null, null, false, null, null, null));
107-
cache.Add(@"P:Company.BillingAddressNested", new XmlComment(@"Billing address.", null, null, null, null, false, null, null, null));
108-
cache.Add(@"P:Company.VisitingAddressClassWithSummary", new XmlComment(@"Visiting address.", null, null, null, null, false, null, null, null));
109-
cache.Add(@"P:Company.VisitingAddressClassWithoutSummary", new XmlComment(@"Visiting address.", null, null, null, null, false, null, null, null));
110-
cache.Add(@"P:Company.VisitingAddressNested", new XmlComment(@"Visiting address.", null, null, null, null, false, null, null, null));
111103

112104
return cache;
113105
}
@@ -542,13 +534,12 @@ file static class JsonNodeExtensions
542534
file static class GeneratedServiceCollectionExtensions
543535
{
544536
[InterceptsLocation]
545-
public static IServiceCollection AddOpenApi(this IServiceCollection services, Action<OpenApiOptions> configureOptions)
537+
public static IServiceCollection AddOpenApi(this IServiceCollection services)
546538
{
547539
return services.AddOpenApi("v1", options =>
548540
{
549541
options.AddSchemaTransformer(new XmlCommentSchemaTransformer());
550542
options.AddOperationTransformer(new XmlCommentOperationTransformer());
551-
configureOptions(options);
552543
});
553544
}
554545

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests/snapshots/SchemaTests.SupportsXmlCommentsOnSchemas_openapi.json.verified.txt

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -172,60 +172,10 @@
172172
}
173173
}
174174
}
175-
},
176-
"/company": {
177-
"post": {
178-
"tags": [
179-
"testhost"
180-
],
181-
"requestBody": {
182-
"content": {
183-
"application/json": {
184-
"schema": {
185-
"$ref": "#/components/schemas/Company"
186-
}
187-
}
188-
}
189-
},
190-
"responses": {
191-
"200": {
192-
"description": "OK"
193-
}
194-
}
195-
}
196175
}
197176
},
198177
"components": {
199178
"schemas": {
200-
"AddressWithoutSummary": {
201-
"type": [
202-
"null",
203-
"object"
204-
],
205-
"properties": {
206-
"street": {
207-
"type": [
208-
"null",
209-
"string"
210-
]
211-
}
212-
}
213-
},
214-
"AddressWithSummary": {
215-
"type": [
216-
"null",
217-
"object"
218-
],
219-
"properties": {
220-
"street": {
221-
"type": [
222-
"null",
223-
"string"
224-
]
225-
}
226-
},
227-
"description": "An address."
228-
},
229179
"BoardItem": {
230180
"type": "object",
231181
"properties": {
@@ -238,57 +188,6 @@
238188
},
239189
"description": "An item on the board."
240190
},
241-
"Company": {
242-
"type": "object",
243-
"properties": {
244-
"billingAddressClassWithSummary": {
245-
"description": "Billing address.",
246-
"$ref": "#/components/schemas/AddressWithSummary"
247-
},
248-
"billingAddressClassWithoutSummary": {
249-
"description": "Billing address.",
250-
"$ref": "#/components/schemas/AddressWithoutSummary"
251-
},
252-
"billingAddressNested": {
253-
"type": [
254-
"null",
255-
"object"
256-
],
257-
"properties": {
258-
"street": {
259-
"type": [
260-
"null",
261-
"string"
262-
]
263-
}
264-
},
265-
"description": "Billing address."
266-
},
267-
"visitingAddressClassWithSummary": {
268-
"description": "Visiting address.",
269-
"$ref": "#/components/schemas/AddressWithSummary"
270-
},
271-
"visitingAddressClassWithoutSummary": {
272-
"description": "Visiting address.",
273-
"$ref": "#/components/schemas/AddressWithoutSummary"
274-
},
275-
"visitingAddressNested": {
276-
"type": [
277-
"null",
278-
"object"
279-
],
280-
"properties": {
281-
"street": {
282-
"type": [
283-
"null",
284-
"string"
285-
]
286-
}
287-
},
288-
"description": "Visiting address."
289-
}
290-
}
291-
},
292191
"Project": {
293192
"required": [
294193
"name",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
"RootModel": {
6262
"type": "object",
6363
"properties": {
64+
"noPropertyComment": {
65+
"$ref": "#/components/schemas/ModelWithSummary"
66+
},
6467
"firstModelWithSummary": {
6568
"description": "Comment on property FirstModelWithSummary.",
6669
"$ref": "#/components/schemas/ModelWithSummary"

0 commit comments

Comments
 (0)