Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/OpenApi/gen/XmlCommentGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (typeComment.Examples is { Count: > 0 } typeExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = typeExamples.ConvertAll(example => example.Parse()!);
}
}

Expand All @@ -610,9 +610,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
{
// Inlined schema
schema.Description = description;
if (propertyComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (propertyComment.Examples is { Count: > 0 } propertyExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = propertyExamples.ConvertAll(example => example.Parse()!);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public class TodoWithDescription
public string Description { get; set; }
}

/// <example>{ "stringType": "First example" }</example>
/// <example>{ "stringType": "Second example" }</example>
public class TypeWithExamples
{
/// <example>true</example>
Expand All @@ -138,6 +140,7 @@ public class TypeWithExamples
/// <example>2022-01-01</example>
public DateOnly DateOnlyType { get; set; }
/// <example>Hello, World!</example>
/// <example>Goodbye, World!</example>
public string StringType { get; set; }
/// <example>2d8f1eac-b5c6-4e29-8c62-4d9d75ef3d3d</example>
public Guid GuidType { get; set; }
Expand Down Expand Up @@ -220,6 +223,10 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, additionalAssemblies, docume
path = document.Paths["/type-with-examples"].Operations[HttpMethod.Post];
var typeWithExamples = path.RequestBody.Content["application/json"].Schema;

Assert.Equal(2, typeWithExamples.Examples!.Count);
Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "stringType": "First example" }"""), typeWithExamples.Examples![0]));
Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "stringType": "Second example" }"""), typeWithExamples.Examples![1]));

var booleanTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["booleanType"].Examples![0]);
Assert.True(booleanTypeExample.GetValue<bool>());

Expand All @@ -243,6 +250,9 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, additionalAssemblies, docume

var stringTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["stringType"].Examples![0]);
Assert.Equal("Hello, World!", stringTypeExample.GetValue<string>());
Assert.Equal(2, typeWithExamples.Properties["stringType"].Examples!.Count);
var secondStringTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["stringType"].Examples![1]);
Assert.Equal("Goodbye, World!", secondStringTypeExample.GetValue<string>());

var guidTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["guidType"].Examples![0]);
Assert.Equal("2d8f1eac-b5c6-4e29-8c62-4d9d75ef3d3d", guidTypeExample.GetValue<string>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public class TypeWithExamples
/// <example>2022-01-01</example>
public DateOnly DateOnlyType { get; set; }
/// <example>Hello, World!</example>
/// <example>Goodbye, World!</example>
public string StringType { get; set; }
/// <example>2d8f1eac-b5c6-4e29-8c62-4d9d75ef3d3d</example>
public Guid GuidType { get; set; }
Expand Down Expand Up @@ -240,6 +241,9 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>

var stringTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["stringType"].Examples![0]);
Assert.Equal("Hello, World!", stringTypeExample.GetValue<string>());
Assert.Equal(2, typeWithExamples.Properties["stringType"].Examples!.Count);
var secondStringTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["stringType"].Examples![1]);
Assert.Equal("Goodbye, World!", secondStringTypeExample.GetValue<string>());

var guidTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["guidType"].Examples![0]);
Assert.Equal("2d8f1eac-b5c6-4e29-8c62-4d9d75ef3d3d", guidTypeExample.GetValue<string>());
Expand Down Expand Up @@ -293,6 +297,9 @@ public async Task XmlCommentsOnPropertiesShouldApplyToSchemaReferences()
/// <example>
/// { "street": "ModelWithSummaryClass" }
/// </example>
/// <example>
/// { "street": "ModelWithSummaryClass2" }
/// </example>
public class ModelWithSummary
{
public string Street { get; set; }
Expand Down Expand Up @@ -387,6 +394,8 @@ await SnapshotTestHelper.VerifyOpenApi(compilation, document =>
var modelWithSummary = document.Components.Schemas["ModelWithSummary"];
Assert.Equal("Comment on class ModelWithSummary.", modelWithSummary.Description);
Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "street": "ModelWithSummaryClass" }"""), modelWithSummary.Examples![0]));
Assert.Equal(2, modelWithSummary.Examples!.Count);
Assert.True(JsonNode.DeepEquals(JsonNode.Parse("""{ "street": "ModelWithSummaryClass2" }"""), modelWithSummary.Examples![1]));

var modelWithoutSummary = document.Components.Schemas["ModelWithoutSummary"];
Assert.Null(modelWithoutSummary.Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (typeComment.Examples is { Count: > 0 } typeExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = typeExamples.ConvertAll(example => example.Parse()!);
}
}

Expand All @@ -592,9 +592,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
{
// Inlined schema
schema.Description = description;
if (propertyComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (propertyComment.Examples is { Count: > 0 } propertyExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = propertyExamples.ConvertAll(example => example.Parse()!);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ file static class XmlCommentCache
cache.Add(@"P:ClassLibrary.TodoWithDescription.Id", new XmlComment(@"The identifier of the todo.", null, null, null, null, false, null, null, null));
cache.Add(@"P:ClassLibrary.TodoWithDescription.Name", new XmlComment(null, null, null, null, @"The name of the todo.", false, null, null, null));
cache.Add(@"P:ClassLibrary.TodoWithDescription.Description", new XmlComment(@"A description of the todo.", null, null, null, @"Another description of the todo.", false, null, null, null));
cache.Add(@"T:ClassLibrary.TypeWithExamples", new XmlComment(null, null, null, null, null, false, [@"{ ""stringType"": ""First example"" }", @"{ ""stringType"": ""Second example"" }"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.BooleanType", new XmlComment(null, null, null, null, null, false, [@"true"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.IntegerType", new XmlComment(null, null, null, null, null, false, [@"42"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.LongType", new XmlComment(null, null, null, null, null, false, [@"1234567890123456789"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.DoubleType", new XmlComment(null, null, null, null, null, false, [@"3.14"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.FloatType", new XmlComment(null, null, null, null, null, false, [@"3.14"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.DateTimeType", new XmlComment(null, null, null, null, null, false, [@"2022-01-01T00:00:00Z"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.DateOnlyType", new XmlComment(null, null, null, null, null, false, [@"2022-01-01"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.StringType", new XmlComment(null, null, null, null, null, false, [@"Hello, World!"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.StringType", new XmlComment(null, null, null, null, null, false, [@"Hello, World!", @"Goodbye, World!"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.GuidType", new XmlComment(null, null, null, null, null, false, [@"2d8f1eac-b5c6-4e29-8c62-4d9d75ef3d3d"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.TimeOnlyType", new XmlComment(null, null, null, null, null, false, [@"12:30:45"], null, null));
cache.Add(@"P:ClassLibrary.TypeWithExamples.TimeSpanType", new XmlComment(null, null, null, null, null, false, [@"P3DT4H5M"], null, null));
Expand Down Expand Up @@ -595,9 +596,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (typeComment.Examples is { Count: > 0 } typeExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = typeExamples.ConvertAll(example => example.Parse()!);
}
}

Expand All @@ -621,9 +622,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
{
// Inlined schema
schema.Description = description;
if (propertyComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (propertyComment.Examples is { Count: > 0 } propertyExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = propertyExamples.ConvertAll(example => example.Parse()!);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (typeComment.Examples is { Count: > 0 } typeExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = typeExamples.ConvertAll(example => example.Parse()!);
}
}

Expand All @@ -719,9 +719,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
{
// Inlined schema
schema.Description = description;
if (propertyComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (propertyComment.Examples is { Count: > 0 } propertyExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = propertyExamples.ConvertAll(example => example.Parse()!);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (typeComment.Examples is { Count: > 0 } typeExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = typeExamples.ConvertAll(example => example.Parse()!);
}
}

Expand All @@ -593,9 +593,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
{
// Inlined schema
schema.Description = description;
if (propertyComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (propertyComment.Examples is { Count: > 0 } propertyExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = propertyExamples.ConvertAll(example => example.Parse()!);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (typeComment.Examples is { Count: > 0 } typeExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = typeExamples.ConvertAll(example => example.Parse()!);
}
}

Expand All @@ -593,9 +593,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
{
// Inlined schema
schema.Description = description;
if (propertyComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (propertyComment.Examples is { Count: > 0 } propertyExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = propertyExamples.ConvertAll(example => example.Parse()!);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (typeComment.Examples is { Count: > 0 } typeExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = typeExamples.ConvertAll(example => example.Parse()!);
}
}

Expand All @@ -593,9 +593,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
{
// Inlined schema
schema.Description = description;
if (propertyComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (propertyComment.Examples is { Count: > 0 } propertyExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = propertyExamples.ConvertAll(example => example.Parse()!);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
if (XmlCommentCache.Cache.TryGetValue(DocumentationCommentIdHelper.NormalizeDocId(context.JsonTypeInfo.Type.CreateDocumentationId()), out var typeComment))
{
schema.Description = typeComment.Summary;
if (typeComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (typeComment.Examples is { Count: > 0 } typeExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = typeExamples.ConvertAll(example => example.Parse()!);
}
}

Expand All @@ -593,9 +593,9 @@ private static bool IsRequestBodyParameter(global::Microsoft.AspNetCore.Mvc.Mode
{
// Inlined schema
schema.Description = description;
if (propertyComment.Examples is { } examples && global::System.Linq.Enumerable.FirstOrDefault(examples) is { } jsonString)
if (propertyComment.Examples is { Count: > 0 } propertyExamples)
{
schema.Examples = [jsonString.Parse()!];
schema.Examples = propertyExamples.ConvertAll(example => example.Parse()!);
}
}
else
Expand Down
Loading