44using System . Text . RegularExpressions ;
55using Microsoft . OpenApi . Models ;
66using Simplify . Web . Meta ;
7+ using Swashbuckle . AspNetCore . SwaggerGen ;
78
89namespace Simplify . Web . Swagger
910{
@@ -25,23 +26,24 @@ public class ControllerActionsFactory
2526 /// Creates controller actions from Simplify.Web controller meta data
2627 /// </summary>
2728 /// <returns></returns>
28- public static IEnumerable < ControllerAction > CreateControllerActionsFromControllersMetaData ( ) =>
29+ public static IEnumerable < ControllerAction > CreateControllerActionsFromControllersMetaData ( DocumentFilterContext context ) =>
2930 ControllersMetaStore . Current . ControllersMetaData
3031 . Where ( x => x . ExecParameters != null )
31- . SelectMany ( CreateControllerActions ) ;
32+ . SelectMany ( item => CreateControllerActions ( item , context ) ) ;
3233
33- private static IEnumerable < ControllerAction > CreateControllerActions ( IControllerMetaData item ) =>
34+ private static IEnumerable < ControllerAction > CreateControllerActions ( IControllerMetaData item , DocumentFilterContext context ) =>
3435 item . ExecParameters !
3536 . Routes
36- . Select ( x => CreateControllerAction ( x . Key , x . Value , item ) ) ;
37+ . Select ( x => CreateControllerAction ( x . Key , x . Value , item , context ) ) ;
3738
38- private static ControllerAction CreateControllerAction ( HttpMethod method , string route , IControllerMetaData item ) =>
39+ private static ControllerAction CreateControllerAction ( HttpMethod method , string route , IControllerMetaData item , DocumentFilterContext context ) =>
3940 new ControllerAction
4041 {
4142 Type = HttpMethodToOperationType ( method ) ,
4243 Path = route . StartsWith ( "/" ) ? route : "/" + route ,
4344 Names = CreateNames ( item . ControllerType ) ,
4445 Responses = CreateResponses ( item . ControllerType ) ,
46+ RequestBody = CreateRequestBody ( item . ControllerType , context ) ,
4547 IsAuthorizationRequired = item . Security != null && item . Security . IsAuthorizationRequired
4648 } ;
4749
@@ -92,6 +94,24 @@ private static OperationType HttpMethodToOperationType(HttpMethod method) =>
9294 _ => OperationType . Get
9395 } ;
9496
97+ private static OpenApiRequestBody CreateRequestBody ( Type controllerType , DocumentFilterContext context )
98+ {
99+ var request = new OpenApiRequestBody ( ) ;
100+ var attributes = controllerType . GetCustomAttributes ( typeof ( ProducesRequestBodyAttribute ) , false ) ;
101+
102+ if ( attributes . Length > 0 )
103+ {
104+ var item = ( ProducesRequestBodyAttribute ) attributes . First ( ) ;
105+
106+ request . Content = new Dictionary < string , OpenApiMediaType >
107+ {
108+ [ "application/json" ] = new ( ) { Schema = context . SchemaGenerator . GenerateSchema ( item . Model , context . SchemaRepository ) }
109+ } ;
110+ }
111+
112+ return request ;
113+ }
114+
95115 private static IDictionary < int , OpenApiResponse > CreateResponses ( Type controllerType )
96116 {
97117 var items = new Dictionary < int , OpenApiResponse > ( ) ;
0 commit comments