Skip to content

Commit 5b6dbf1

Browse files
committed
[#1] add content type display
1 parent 4859d32 commit 5b6dbf1

6 files changed

Lines changed: 83 additions & 19 deletions

File tree

src/Simplify.Web.Swagger/ControllerActionsFactory.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text.RegularExpressions;
5-
using Microsoft.AspNetCore.Mvc;
65
using Microsoft.OpenApi.Models;
76
using Simplify.Web.Meta;
87

@@ -96,22 +95,25 @@ private static IDictionary<int, OpenApiResponse> CreateResponses(Type controller
9695
{
9796
var items = new Dictionary<int, OpenApiResponse>();
9897

99-
var attributes = controllerType.GetCustomAttributes(typeof(ProducesResponseTypeAttribute), false);
98+
var attributes = controllerType.GetCustomAttributes(typeof(ProducesResponseAttribute), false);
10099

101-
foreach (ProducesResponseTypeAttribute item in attributes)
100+
foreach (ProducesResponseAttribute item in attributes)
102101
items.Add(item.StatusCode, CreateResponse(item));
103102

104103
return items;
105104
}
106105

107-
private static OpenApiResponse CreateResponse(ProducesResponseTypeAttribute producesResponse)
106+
private static OpenApiResponse CreateResponse(ProducesResponseAttribute producesResponse)
108107
{
109108
var response = new OpenApiResponse();
110109

111110
response.Description = ResponseDescriptionMap
112111
.FirstOrDefault((entry) => Regex.IsMatch(producesResponse.StatusCode.ToString(), entry.Key))
113112
.Value;
114113

114+
foreach (var item in producesResponse.ContentTypes.Distinct())
115+
response.Content.Add(item, new OpenApiMediaType());
116+
115117
return response;
116118
}
117119

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Simplify.Web.Swagger;
5+
6+
/// <summary>
7+
/// A filter that specifies the type of the value and status code returned by the controller.
8+
/// </summary>
9+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
10+
public class ProducesResponseAttribute : Attribute
11+
{
12+
/// <summary>
13+
/// Initializes an instance of <see cref="ProducesResponseAttribute"/>.
14+
/// </summary>
15+
/// <param name="statusCode">The HTTP response status code.</param>
16+
/// <param name="contentType">The content type associated with the response.</param>
17+
/// <param name="additionalContentTypes">Additional content types supported by the response.</param>
18+
public ProducesResponseAttribute(int statusCode, string? contentType, params string[] additionalContentTypes) :
19+
this(statusCode, null, contentType, additionalContentTypes)
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Initializes an instance of <see cref="ProducesResponseAttribute"/>.
25+
/// </summary>
26+
/// <param name="statusCode">The HTTP response status code.</param>
27+
/// <param name="type">The <see cref="Type"/> of object that is going to be written in the response.</param>
28+
/// <param name="contentType">The content type associated with the response.</param>
29+
/// <param name="additionalContentTypes">Additional content types supported by the response.</param>
30+
public ProducesResponseAttribute(int statusCode, Type? type = null, string? contentType = null, params string[] additionalContentTypes)
31+
{
32+
StatusCode = statusCode;
33+
Type = type;
34+
35+
if (!string.IsNullOrEmpty(contentType))
36+
ContentTypes.Add(contentType!);
37+
38+
for (var i = 0; i < additionalContentTypes.Length; i++)
39+
{
40+
if (string.IsNullOrEmpty(additionalContentTypes[i]))
41+
continue;
42+
43+
ContentTypes.Add(additionalContentTypes[i]);
44+
}
45+
}
46+
47+
/// <summary>
48+
/// Gets the HTTP status code of the response.
49+
/// </summary>
50+
public int StatusCode { get; }
51+
52+
/// <summary>
53+
/// Gets the type of the value returned by a controller.
54+
/// </summary>
55+
public Type? Type { get; }
56+
57+
/// <summary>
58+
/// Gets the HTTP content types of the response
59+
/// </summary>
60+
public IList<string> ContentTypes { get; } = new List<string>();
61+
}

src/TesterApp/Controllers/Api/v1/Users/DeleteController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Simplify.Web;
33
using Simplify.Web.Attributes;
4+
using Simplify.Web.Swagger;
45

56
namespace TesterApp.Controllers.Api.v1.Users;
67

78
[Delete("/api/v1/users/{id:int}")]
89
[Authorize]
910
[ApiVersion("1.0")]
10-
[Produces("application/text")]
11-
[ProducesResponseType(StatusCodes.Status204NoContent)]
12-
[ProducesResponseType(StatusCodes.Status400BadRequest)]
13-
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
11+
[ProducesResponse(StatusCodes.Status204NoContent)]
12+
[ProducesResponse(StatusCodes.Status400BadRequest)]
13+
[ProducesResponse(StatusCodes.Status500InternalServerError)]
1414
public class DeleteController : Simplify.Web.Controller
1515
{
1616
public override ControllerResponse Invoke()

src/TesterApp/Controllers/Api/v1/Users/GetController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
using Simplify.Web;
33
using Simplify.Web.Attributes;
44
using Simplify.Web.Json.Responses;
5+
using Simplify.Web.Swagger;
56
using TesterApp.ViewModels;
67

78
namespace TesterApp.Controllers.Api.v1.Users;
89

910
[Get("/api/v1/users/{id:int}")]
1011
[ApiVersion("1.0")]
11-
[Produces("application/json")]
12-
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserViewModel))]
13-
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
12+
[ProducesResponse(StatusCodes.Status200OK, typeof(UserViewModel), "application/json")]
13+
[ProducesResponse(StatusCodes.Status500InternalServerError)]
1414
public class GetController : Simplify.Web.Controller
1515
{
1616
public override ControllerResponse Invoke() =>

src/TesterApp/Controllers/Api/v1/Users/GetMultipleController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
using Simplify.Web;
33
using Simplify.Web.Attributes;
44
using Simplify.Web.Json.Responses;
5+
using Simplify.Web.Swagger;
56
using TesterApp.ViewModels;
67

78
namespace TesterApp.Controllers.Api.v1.Users;
89

910
[Get("/api/v1/users")]
1011
[ApiVersion("1.0")]
11-
[Produces("application/json")]
12-
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IList<UserViewModel>))]
13-
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
12+
// [Produces("application/json")]
13+
[ProducesResponse(StatusCodes.Status200OK, typeof(IList<UserViewModel>), "application/json")]
14+
[ProducesResponse(StatusCodes.Status500InternalServerError)]
1415
public class GetMultipleController : Simplify.Web.Controller
1516
{
1617
public override ControllerResponse Invoke()
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using Simplify.Web;
1+
using System.Net.Mime;
2+
using Simplify.Web;
23
using Simplify.Web.Attributes;
4+
using Simplify.Web.Swagger;
35

46
namespace TesterApp.Controllers
57
{
68
[Get("status")]
9+
[ProducesResponse(StatusCodes.Status200OK, MediaTypeNames.Text.Plain)]
710
public class StatusController : Controller
811
{
9-
public override ControllerResponse Invoke()
10-
{
11-
return Content("Service is running!");
12-
}
12+
public override ControllerResponse Invoke() => Content("Service is running!", MediaTypeNames.Text.Plain);
1313
}
1414
}

0 commit comments

Comments
 (0)