Skip to content

Commit 7595507

Browse files
committed
Updated schema reference XML Comment handling.
Update handling of nested schemas and referenced schemas
1 parent cfab5dc commit 7595507

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

src/OpenApi/src/Extensions/OpenApiDocumentExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public static IOpenApiSchema AddOpenApiSchemaByReference(this OpenApiDocument do
2121
document.Workspace ??= new();
2222
var location = document.BaseUri + "/components/schemas/" + schemaId;
2323
document.Workspace.RegisterComponentForDocument(document, schema, location);
24-
return new OpenApiSchemaReference(schemaId, document);
24+
return new OpenApiSchemaReference(schemaId, document)
25+
{
26+
Description = schema.Description,
27+
};
2528
}
2629
}

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public async Task SupportsXmlCommentsOnSchemas()
1919
2020
var builder = WebApplication.CreateBuilder();
2121
22-
builder.Services.AddOpenApi();
22+
builder.Services.AddOpenApi(options => {
23+
var prevCreateSchemaReferenceId = options.CreateSchemaReferenceId;
24+
options.CreateSchemaReferenceId = (x) => x.Type == typeof(AddressNested) ? null : prevCreateSchemaReferenceId(x);
25+
});
2326
2427
var app = builder.Build();
2528
@@ -31,6 +34,7 @@ public async Task SupportsXmlCommentsOnSchemas()
3134
app.MapPost("/todo-with-description", (TodoWithDescription todo) => { });
3235
app.MapPost("/type-with-examples", (TypeWithExamples typeWithExamples) => { });
3336
app.MapPost("/user", (User user) => { });
37+
app.MapPost("/company", (Company company) => { });
3438
3539
app.Run();
3640
@@ -175,6 +179,60 @@ internal class User : IUser
175179
/// <inheritdoc/>
176180
public string Name { get; set; }
177181
}
182+
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+
}
178236
""";
179237
var generator = new XmlCommentGenerator();
180238
await SnapshotTestHelper.Verify(source, generator, out var compilation);
@@ -258,6 +316,21 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
258316
var user = path.RequestBody.Content["application/json"].Schema;
259317
Assert.Equal("The unique identifier for the user.", user.Properties["id"].Description);
260318
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["billingAddressClassWithSummary"].Description);
323+
Assert.Equal("Billing address.", company.Properties["billingAddressClassWithoutSummary"].Description);
324+
Assert.Equal("Billing address.", company.Properties["billingAddressNested"].Description);
325+
Assert.Equal("Visiting address.", company.Properties["visitingAddressClassWithSummary"].Description);
326+
Assert.Equal("Visiting address.", company.Properties["visitingAddressClassWithoutSummary"].Description);
327+
Assert.Equal("Visiting address.", company.Properties["visitingAddressNested"].Description);
328+
329+
var addressWithSummary = document.Components.Schemas["AddressWithSummary"];
330+
Assert.Equal("An address.", addressWithSummary.Description);
331+
332+
var addressWithoutSummary = document.Components.Schemas["AddressWithoutSummary"];
333+
Assert.Null(addressWithSummary.Description);
261334
});
262335
}
263336
}

0 commit comments

Comments
 (0)