Skip to content

Commit 8d16ef3

Browse files
committed
[add] response schema
1 parent c2e3fa7 commit 8d16ef3

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/Simplify.Web.Swagger/ControllerActionsFactory.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private static ControllerAction CreateControllerAction(HttpMethod method, string
4242
Type = HttpMethodToOperationType(method),
4343
Path = route.StartsWith("/") ? route : "/" + route,
4444
Names = CreateNames(item.ControllerType),
45-
Responses = CreateResponses(item.ControllerType),
45+
Responses = CreateResponses(item.ControllerType, context),
4646
RequestBody = CreateRequestBody(item.ControllerType, context),
4747
IsAuthorizationRequired = item.Security != null && item.Security.IsAuthorizationRequired
4848
};
@@ -112,19 +112,19 @@ private static OpenApiRequestBody CreateRequestBody(Type controllerType, Documen
112112
return request;
113113
}
114114

115-
private static IDictionary<int, OpenApiResponse> CreateResponses(Type controllerType)
115+
private static IDictionary<int, OpenApiResponse> CreateResponses(Type controllerType, DocumentFilterContext context)
116116
{
117117
var items = new Dictionary<int, OpenApiResponse>();
118118

119119
var attributes = controllerType.GetCustomAttributes(typeof(ProducesResponseAttribute), false);
120120

121121
foreach (ProducesResponseAttribute item in attributes)
122-
items.Add(item.StatusCode, CreateResponse(item));
122+
items.Add(item.StatusCode, CreateResponse(item, context));
123123

124124
return items;
125125
}
126126

127-
private static OpenApiResponse CreateResponse(ProducesResponseAttribute producesResponse)
127+
private static OpenApiResponse CreateResponse(ProducesResponseAttribute producesResponse, DocumentFilterContext context)
128128
{
129129
var response = new OpenApiResponse();
130130

@@ -133,7 +133,9 @@ private static OpenApiResponse CreateResponse(ProducesResponseAttribute produces
133133
.Value;
134134

135135
foreach (var item in producesResponse.ContentTypes.Distinct())
136-
response.Content.Add(item, new OpenApiMediaType());
136+
response.Content.Add(item, producesResponse.Type is null
137+
? new OpenApiMediaType()
138+
: new () {Schema = context.SchemaGenerator.GenerateSchema(producesResponse.Type, context.SchemaRepository)});
137139

138140
return response;
139141
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using System.Net.Mime;
2+
using Microsoft.AspNetCore.Mvc;
23
using Simplify.Web;
34
using Simplify.Web.Attributes;
45
using Simplify.Web.Json.Responses;
6+
using Simplify.Web.Swagger;
57
using TesterApp.ViewModels;
68

79
namespace TesterApp.Controllers.Api.v1.Groups;
810

911
[Get("/api/v1/groups")]
1012
[ApiVersion("1.0")]
11-
[Produces("application/json")]
12-
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IList<GroupViewModel>))]
13-
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
13+
[ProducesResponse(StatusCodes.Status200OK, typeof(IList<GroupViewModel>), MediaTypeNames.Application.Json)]
14+
[ProducesResponse(StatusCodes.Status500InternalServerError)]
1415
public class GetMultipleController : Simplify.Web.Controller
1516
{
1617
public override ControllerResponse Invoke()

0 commit comments

Comments
 (0)