Skip to content

Commit d7130a2

Browse files
authored
Add Description to ProducesResponseType (and others) for better OpenAPI document creation (#58193)
* Started adding Description to several attributes * Add new properties to unshipped apis * Some more progress of adding Description in some places * Small improvements based on API review comments * Added missing modifier to property * Make changes in unshipped.txt so the http project builds * Add Description to OpenApiRouteHandlerBuilderExtensions and extra constructor and static methods to ProducesResponseTypeMetadata for correct description overloads * Changed code to follow overload rules and fix compile issues * Fix minor typo * Remove code from OpenApiRouteHandlerBUilderExtensions based on Safiaś comment Also removed new constructors based on Safiaś comment * Fix incorrect XML Comment * Fixed some more Public API issues * Add unit test * Add some more unit tests * Remove unnecessary set from interface * Remove unnecessary change in public api shipped file * Also update unshipped file so the build succeeds! * Apply the new Description in response models
1 parent 1d98312 commit d7130a2

File tree

19 files changed

+173
-7
lines changed

19 files changed

+173
-7
lines changed

src/Http/Http.Abstractions/src/Metadata/IProducesResponseTypeMetadata.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public interface IProducesResponseTypeMetadata
1818
/// </summary>
1919
int StatusCode { get; }
2020

21+
/// <summary>
22+
/// Gets the description of the response.
23+
/// </summary>
24+
string? Description { get; }
25+
2126
/// <summary>
2227
/// Gets the content types supported by the metadata.
2328
/// </summary>

src/Http/Http.Abstractions/src/Metadata/ProducesResponseTypeMetadata.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ private ProducesResponseTypeMetadata(int statusCode, Type? type, IEnumerable<str
6868
/// </summary>
6969
public int StatusCode { get; private set; }
7070

71+
/// <summary>
72+
/// Gets or sets the description of the response.
73+
/// </summary>
74+
public string? Description { get; set; }
75+
7176
/// <summary>
7277
/// Gets or sets the content types associated with the response.
7378
/// </summary>

src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata.HasTryParse.get ->
1111
Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata.IsOptional.get -> bool
1212
Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata.Name.get -> string!
1313
Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata.ParameterInfo.get -> System.Reflection.ParameterInfo!
14+
Microsoft.AspNetCore.Http.ProducesResponseTypeMetadata.Description.get -> string?
15+
Microsoft.AspNetCore.Http.ProducesResponseTypeMetadata.Description.set -> void
16+
Microsoft.AspNetCore.Http.Metadata.IProducesResponseTypeMetadata.Description.get -> string?

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ private static RequestDelegateFactoryContext CreateFactoryContext(RequestDelegat
274274
var serviceProvider = options?.ServiceProvider ?? options?.EndpointBuilder?.ApplicationServices ?? EmptyServiceProvider.Instance;
275275
var endpointBuilder = options?.EndpointBuilder ?? new RdfEndpointBuilder(serviceProvider);
276276
var jsonSerializerOptions = serviceProvider.GetService<IOptions<JsonOptions>>()?.Value.SerializerOptions ?? JsonOptions.DefaultSerializerOptions;
277-
var formDataMapperOptions = new FormDataMapperOptions();;
277+
var formDataMapperOptions = new FormDataMapperOptions();
278278

279279
var factoryContext = new RequestDelegateFactoryContext
280280
{

src/Mvc/Mvc.Abstractions/src/ApiExplorer/ApiResponseType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public class ApiResponseType
3333
/// </remarks>
3434
public Type? Type { get; set; }
3535

36+
/// <summary>
37+
/// Gets or sets the description of the response.
38+
/// </summary>
39+
public string? Description { get; set; }
40+
3641
/// <summary>
3742
/// Gets or sets the HTTP response status code.
3843
/// </summary>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Mvc.ApiExplorer.ApiResponseType.Description.get -> string?
3+
Microsoft.AspNetCore.Mvc.ApiExplorer.ApiResponseType.Description.set -> void

src/Mvc/Mvc.Api.Analyzers/test/TestFiles/SymbolApiResponseMetadataProviderTest/GetResponseMetadataTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
@@ -60,6 +60,8 @@ public class CustomResponseTypeAttribute : Attribute, IApiResponseMetadataProvid
6060

6161
public int StatusCode { get; set; }
6262

63+
public string Description { get; set; }
64+
6365
public void SetContentTypes(MediaTypeCollection contentTypes)
6466
{
6567
}

src/Mvc/Mvc.ApiExplorer/src/ApiResponseTypeProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,14 @@ internal static Dictionary<int, ApiResponseType> ReadResponseMetadata(
166166

167167
var statusCode = metadataAttribute.StatusCode;
168168

169+
var description = metadataAttribute.Description;
170+
169171
var apiResponseType = new ApiResponseType
170172
{
171173
Type = metadataAttribute.Type,
172174
StatusCode = statusCode,
173175
IsDefaultResponse = metadataAttribute is IApiDefaultResponseMetadataProvider,
176+
Description = description
174177
};
175178

176179
if (apiResponseType.Type == typeof(void))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Mvc.ApiExplorer.ApiResponseType.Description.get -> string? (forwarded, contained in Microsoft.AspNetCore.Mvc.Abstractions)
3+
Microsoft.AspNetCore.Mvc.ApiExplorer.ApiResponseType.Description.set -> void (forwarded, contained in Microsoft.AspNetCore.Mvc.Abstractions)

src/Mvc/Mvc.ApiExplorer/test/DefaultApiDescriptionProviderTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,8 @@ public void GetApiDescription_PopulatesResponseInformation_WhenSetByFilter(strin
11071107
var action = CreateActionDescriptor(methodName);
11081108
var filter = new ContentTypeAttribute("text/*")
11091109
{
1110-
Type = typeof(Order)
1110+
Type = typeof(Order),
1111+
Description = "Example"
11111112
};
11121113

11131114
action.FilterDescriptors = new List<FilterDescriptor>
@@ -1124,6 +1125,7 @@ public void GetApiDescription_PopulatesResponseInformation_WhenSetByFilter(strin
11241125
Assert.NotNull(responseTypes.ModelMetadata);
11251126
Assert.Equal(200, responseTypes.StatusCode);
11261127
Assert.Equal(typeof(Order), responseTypes.Type);
1128+
Assert.Equal("Example", responseTypes.Description);
11271129

11281130
foreach (var responseFormat in responseTypes.ApiResponseFormats)
11291131
{
@@ -2874,6 +2876,8 @@ public ContentTypeAttribute(string mediaType)
28742876

28752877
public Type Type { get; set; }
28762878

2879+
public string Description { get; set; }
2880+
28772881
public void SetContentTypes(MediaTypeCollection contentTypes)
28782882
{
28792883
contentTypes.Clear();

0 commit comments

Comments
 (0)