|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Text.Json.Nodes; |
| 5 | +using Microsoft.OpenApi.Models; |
| 6 | + |
| 7 | +namespace Microsoft.AspNetCore.OpenApi.SourceGenerators.Tests; |
| 8 | + |
| 9 | +[UsesVerify] |
| 10 | +public partial class AdditionalTextsTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public async Task CanHandleXmlForSchemasInAdditionalTexts() |
| 14 | + { |
| 15 | + var source = """ |
| 16 | +using System; |
| 17 | +using Microsoft.AspNetCore.Builder; |
| 18 | +using Microsoft.Extensions.DependencyInjection; |
| 19 | +using ClassLibrary; |
| 20 | +
|
| 21 | +var builder = WebApplication.CreateBuilder(); |
| 22 | +
|
| 23 | +builder.Services.AddOpenApi(); |
| 24 | +
|
| 25 | +var app = builder.Build(); |
| 26 | +
|
| 27 | +app.MapPost("/todo", (Todo todo) => { }); |
| 28 | +app.MapPost("/project", (Project project) => { }); |
| 29 | +app.MapPost("/board", (ProjectBoard.BoardItem boardItem) => { }); |
| 30 | +app.MapPost("/project-record", (ProjectRecord project) => { }); |
| 31 | +app.MapPost("/todo-with-description", (TodoWithDescription todo) => { }); |
| 32 | +app.MapPost("/type-with-examples", (TypeWithExamples typeWithExamples) => { }); |
| 33 | +
|
| 34 | +app.Run(); |
| 35 | +"""; |
| 36 | + |
| 37 | + var librarySource = """ |
| 38 | +using System; |
| 39 | +
|
| 40 | +namespace ClassLibrary; |
| 41 | +
|
| 42 | +/// <summary> |
| 43 | +/// This is a todo item. |
| 44 | +/// </summary> |
| 45 | +public class Todo |
| 46 | +{ |
| 47 | + public int Id { get; set; } |
| 48 | + public string Name { get; set; } |
| 49 | + public string Description { get; set; } |
| 50 | +} |
| 51 | +
|
| 52 | +/// <summary> |
| 53 | +/// The project that contains <see cref="Todo"/> items. |
| 54 | +/// </summary> |
| 55 | +public record Project(string Name, string Description); |
| 56 | +
|
| 57 | +public class ProjectBoard |
| 58 | +{ |
| 59 | + /// <summary> |
| 60 | + /// An item on the board. |
| 61 | + /// </summary> |
| 62 | + public class BoardItem |
| 63 | + { |
| 64 | + public string Name { get; set; } |
| 65 | + } |
| 66 | +} |
| 67 | +
|
| 68 | +/// <summary> |
| 69 | +/// The project that contains <see cref="Todo"/> items. |
| 70 | +/// </summary> |
| 71 | +/// <param name="Name">The name of the project.</param> |
| 72 | +/// <param name="Description">The description of the project.</param> |
| 73 | +public record ProjectRecord(string Name, string Description); |
| 74 | +
|
| 75 | +public class TodoWithDescription |
| 76 | +{ |
| 77 | + /// <summary> |
| 78 | + /// The identifier of the todo. |
| 79 | + /// </summary> |
| 80 | + public int Id { get; set; } |
| 81 | + /// <value> |
| 82 | + /// The name of the todo. |
| 83 | + /// </value> |
| 84 | + public string Name { get; set; } |
| 85 | + /// <summary> |
| 86 | + /// A description of the todo. |
| 87 | + /// </summary> |
| 88 | + /// <value>Another description of the todo.</value> |
| 89 | + public string Description { get; set; } |
| 90 | +} |
| 91 | +
|
| 92 | +public class TypeWithExamples |
| 93 | +{ |
| 94 | + /// <example>true</example> |
| 95 | + public bool BooleanType { get; set; } |
| 96 | + /// <example>42</example> |
| 97 | + public int IntegerType { get; set; } |
| 98 | + /// <example>1234567890123456789</example> |
| 99 | + public long LongType { get; set; } |
| 100 | + /// <example>3.14</example> |
| 101 | + public double DoubleType { get; set; } |
| 102 | + /// <example>3.14</example> |
| 103 | + public float FloatType { get; set; } |
| 104 | + /// <example>2022-01-01T00:00:00Z</example> |
| 105 | + public DateTime DateTimeType { get; set; } |
| 106 | + /// <example>2022-01-01</example> |
| 107 | + public DateOnly DateOnlyType { get; set; } |
| 108 | + /// <example>Hello, World!</example> |
| 109 | + public string StringType { get; set; } |
| 110 | + /// <example>2d8f1eac-b5c6-4e29-8c62-4d9d75ef3d3d</example> |
| 111 | + public Guid GuidType { get; set; } |
| 112 | + /// <example>12:30:45</example> |
| 113 | + public TimeOnly TimeOnlyType { get; set; } |
| 114 | + /// <example>P3DT4H5M</example> |
| 115 | + public TimeSpan TimeSpanType { get; set; } |
| 116 | + /// <example>255</example> |
| 117 | + public byte ByteType { get; set; } |
| 118 | + /// <example>3.14159265359</example> |
| 119 | + public decimal DecimalType { get; set; } |
| 120 | + /// <example>https://example.com</example> |
| 121 | + public Uri UriType { get; set; } |
| 122 | +} |
| 123 | +"""; |
| 124 | + var references = new Dictionary<string, List<string>> |
| 125 | + { |
| 126 | + { "ClassLibrary", [librarySource] } |
| 127 | + }; |
| 128 | + |
| 129 | + var generator = new XmlCommentGenerator(); |
| 130 | + await SnapshotTestHelper.Verify(source, generator, references, out var compilation, out var additionalAssemblies); |
| 131 | + await SnapshotTestHelper.VerifyOpenApi(compilation, additionalAssemblies, document => |
| 132 | + { |
| 133 | + var path = document.Paths["/todo"].Operations[OperationType.Post]; |
| 134 | + var todo = path.RequestBody.Content["application/json"].Schema; |
| 135 | + Assert.Equal("This is a todo item.", todo.Description); |
| 136 | + |
| 137 | + path = document.Paths["/project"].Operations[OperationType.Post]; |
| 138 | + var project = path.RequestBody.Content["application/json"].Schema; |
| 139 | + Assert.Equal("The project that contains Todo items.", project.Description); |
| 140 | + |
| 141 | + path = document.Paths["/board"].Operations[OperationType.Post]; |
| 142 | + var board = path.RequestBody.Content["application/json"].Schema; |
| 143 | + Assert.Equal("An item on the board.", board.Description); |
| 144 | + |
| 145 | + path = document.Paths["/project-record"].Operations[OperationType.Post]; |
| 146 | + project = path.RequestBody.Content["application/json"].Schema; |
| 147 | + |
| 148 | + Assert.Equal("The name of the project.", project.Properties["name"].Description); |
| 149 | + Assert.Equal("The description of the project.", project.Properties["description"].Description); |
| 150 | + |
| 151 | + path = document.Paths["/todo-with-description"].Operations[OperationType.Post]; |
| 152 | + todo = path.RequestBody.Content["application/json"].Schema; |
| 153 | + Assert.Equal("The identifier of the todo.", todo.Properties["id"].Description); |
| 154 | + Assert.Equal("The name of the todo.", todo.Properties["name"].Description); |
| 155 | + Assert.Equal("Another description of the todo.", todo.Properties["description"].Description); |
| 156 | + |
| 157 | + path = document.Paths["/type-with-examples"].Operations[OperationType.Post]; |
| 158 | + var typeWithExamples = path.RequestBody.Content["application/json"].Schema; |
| 159 | + |
| 160 | + var booleanTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["booleanType"].Example); |
| 161 | + Assert.True(booleanTypeExample.GetValue<bool>()); |
| 162 | + |
| 163 | + var integerTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["integerType"].Example); |
| 164 | + Assert.Equal(42, integerTypeExample.GetValue<int>()); |
| 165 | + |
| 166 | + var longTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["longType"].Example); |
| 167 | + Assert.Equal(1234567890123456789, longTypeExample.GetValue<long>()); |
| 168 | + |
| 169 | + // Broken due to https://github.com/microsoft/OpenAPI.NET/issues/2137 |
| 170 | + // var doubleTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["doubleType"].Example); |
| 171 | + // Assert.Equal("3.14", doubleTypeExample.GetValue<string>()); |
| 172 | + |
| 173 | + // var floatTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["floatType"].Example); |
| 174 | + // Assert.Equal(3.14f, floatTypeExample.GetValue<float>()); |
| 175 | + |
| 176 | + // var dateTimeTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateTimeType"].Example); |
| 177 | + // Assert.Equal(DateTime.Parse("2022-01-01T00:00:00Z", CultureInfo.InvariantCulture), dateTimeTypeExample.GetValue<DateTime>()); |
| 178 | + |
| 179 | + // var dateOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["dateOnlyType"].Example); |
| 180 | + // Assert.Equal(DateOnly.Parse("2022-01-01", CultureInfo.InvariantCulture), dateOnlyTypeExample.GetValue<DateOnly>()); |
| 181 | + |
| 182 | + var stringTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["stringType"].Example); |
| 183 | + Assert.Equal("Hello, World!", stringTypeExample.GetValue<string>()); |
| 184 | + |
| 185 | + var guidTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["guidType"].Example); |
| 186 | + Assert.Equal("2d8f1eac-b5c6-4e29-8c62-4d9d75ef3d3d", guidTypeExample.GetValue<string>()); |
| 187 | + |
| 188 | + var byteTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["byteType"].Example); |
| 189 | + Assert.Equal(255, byteTypeExample.GetValue<int>()); |
| 190 | + |
| 191 | + // Broken due to https://github.com/microsoft/OpenAPI.NET/issues/2137 |
| 192 | + // var timeOnlyTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeOnlyType"].Example); |
| 193 | + // Assert.Equal(TimeOnly.Parse("12:30:45", CultureInfo.InvariantCulture), timeOnlyTypeExample.GetValue<TimeOnly>()); |
| 194 | + |
| 195 | + // var timeSpanTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["timeSpanType"].Example); |
| 196 | + // Assert.Equal(TimeSpan.Parse("P3DT4H5M", CultureInfo.InvariantCulture), timeSpanTypeExample.GetValue<TimeSpan>()); |
| 197 | + |
| 198 | + // var decimalTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["decimalType"].Example); |
| 199 | + // Assert.Equal(3.14159265359m, decimalTypeExample.GetValue<decimal>()); |
| 200 | + |
| 201 | + var uriTypeExample = Assert.IsAssignableFrom<JsonNode>(typeWithExamples.Properties["uriType"].Example); |
| 202 | + Assert.Equal("https://example.com", uriTypeExample.GetValue<string>()); |
| 203 | + }); |
| 204 | + } |
| 205 | +} |
0 commit comments