コード例 #1
0
        private Operation CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry)
        {
            var groupName = _options.GroupNameSelector(apiDescription);

            var parameters = apiDescription.ParameterDescriptions
                             .Where(paramDesc => paramDesc.Source.IsFromRequest)
                             .Select(paramDesc => CreateParameter(paramDesc, schemaRegistry))
                             .ToList();

            var responses = new Dictionary <string, Response>();

            if (!apiDescription.SupportedResponseTypes.Any())
            {
                responses.Add("204", new Response {
                    Description = "No Content"
                });
            }
            else
            {
                var description = apiDescription.SupportedResponseTypes.OrderBy(responseType => responseType.StatusCode).FirstOrDefault();
                responses.Add("200", CreateSuccessResponse(description.Type, schemaRegistry));
            }

            var operation = new Operation
            {
                Tags        = (groupName != null) ? new[] { groupName } : null,
                OperationId = apiDescription.FriendlyId(),
                Consumes    = apiDescription.SupportedRequestMediaTypes().ToList(),
                Produces    = apiDescription.SupportedResponseMediaTypes().ToList(),
                Parameters  = parameters.Any() ? parameters : null, // parameters can be null but not empty
                Responses   = responses,
                Deprecated  = apiDescription.IsObsolete()
            };

            var filterContext = new OperationFilterContext(apiDescription, schemaRegistry);

            foreach (var filter in _options.OperationFilters)
            {
                filter.Apply(operation, filterContext);
            }

            return(operation);
        }
コード例 #2
0
ファイル: SwaggerGenerator.cs プロジェクト: Gilmond/Ahoy
        private Operation CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry)
        {
            var groupName = _options.GroupNameSelector(apiDescription);

            var parameters = apiDescription.ParameterDescriptions
                             .Where(paramDesc => paramDesc.Source.IsFromRequest)
                             .Select(paramDesc => CreateParameter(paramDesc, schemaRegistry))
                             .ToList();

            var responses = apiDescription.SupportedResponseTypes
                            .DefaultIfEmpty(new ApiResponseType {
                StatusCode = 200
            })
                            .ToDictionary(
                apiResponseType => apiResponseType.StatusCode.ToString(),
                apiResponseType => CreateResponse(apiResponseType, schemaRegistry)
                );

            var operation = new Operation
            {
                Tags        = (groupName != null) ? new[] { groupName } : null,
                OperationId = apiDescription.FriendlyId(),
                Consumes    = apiDescription.SupportedRequestMediaTypes().ToList(),
                Produces    = apiDescription.SupportedResponseMediaTypes().ToList(),
                Parameters  = parameters.Any() ? parameters : null,                // parameters can be null but not empty
                Responses   = responses,
                Deprecated  = apiDescription.IsObsolete()
            };

            var filterContext = new OperationFilterContext(apiDescription, schemaRegistry);

            foreach (var filter in _options.OperationFilters)
            {
                filter.Apply(operation, filterContext);
            }

            return(operation);
        }