コード例 #1
0
        private IEnumerable <string> InferResponseContentTypes(ApiDescription apiDescription, ApiResponseType apiResponseType)
        {
            // If there's no associated model, return an empty list (i.e. no content)
            if (apiResponseType.ModelMetadata == null)
            {
                return(Enumerable.Empty <string>());
            }

            // If there's content types explicitly specified via ProducesAttribute, use them
            var explicitContentTypes = apiDescription.CustomAttributes().OfType <ProducesAttribute>()
                                       .SelectMany(attr => attr.ContentTypes)
                                       .Distinct();

            if (explicitContentTypes.Any())
            {
                return(explicitContentTypes);
            }

            // If there's content types surfaced by ApiExplorer, use them
            var apiExplorerContentTypes = apiResponseType.ApiResponseFormats
                                          .Select(responseFormat => responseFormat.MediaType)
                                          .Distinct();

            if (apiExplorerContentTypes.Any())
            {
                return(apiExplorerContentTypes);
            }

            return(Enumerable.Empty <string>());
        }
コード例 #2
0
        private OpenApiOperation GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)
        {
            try
            {
                var operation = new OpenApiOperation
                {
                    Tags        = GenerateOperationTags(apiDescription),
                    OperationId = _options.OperationIdSelector(apiDescription),
                    Parameters  = GenerateParameters(apiDescription, schemaRepository),
                    RequestBody = GenerateRequestBody(apiDescription, schemaRepository),
                    Responses   = GenerateResponses(apiDescription, schemaRepository),
                    Deprecated  = apiDescription.CustomAttributes().OfType <ObsoleteAttribute>().Any()
                };

                apiDescription.TryGetMethodInfo(out MethodInfo methodInfo);
                var filterContext = new OperationFilterContext(apiDescription, _schemaGenerator, schemaRepository, methodInfo);
                foreach (var filter in _options.OperationFilters)
                {
                    filter.Apply(operation, filterContext);
                }

                return(operation);
            }
            catch (Exception ex)
            {
                throw new SwaggerGeneratorException(
                          message: $"Failed to generate Operation for action - {apiDescription.ActionDescriptor.DisplayName}. See inner exception",
                          innerException: ex);
            }
        }
コード例 #3
0
 public static void GetAdditionalMetadata(this ApiDescription apiDescription,
                                          out MethodInfo methodInfo,
                                          out IEnumerable <object> customAttributes)
 {
     methodInfo       = apiDescription.MethodInfo();
     customAttributes = apiDescription.CustomAttributes();
 }
コード例 #4
0
        private IEnumerable <string> InferRequestContentTypes(ApiDescription apiDescription)
        {
            // If there's content types explicitly specified via ConsumesAttribute, use them
            var explicitContentTypes = apiDescription.CustomAttributes().OfType <ConsumesAttribute>()
                                       .SelectMany(attr => attr.ContentTypes)
                                       .Distinct();

            if (explicitContentTypes.Any())
            {
                return(explicitContentTypes);
            }

            // If there's content types surfaced by ApiExplorer, use them
            var apiExplorerContentTypes = apiDescription.SupportedRequestFormats
                                          .Select(format => format.MediaType)
                                          .Where(x => x != null)
                                          .Distinct();

            if (apiExplorerContentTypes.Any())
            {
                return(apiExplorerContentTypes);
            }

            return(Enumerable.Empty <string>());
        }
コード例 #5
0
        private OpenApiOperation GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)
        {
            var operation = new OpenApiOperation
            {
                Tags        = GenerateOperationTags(apiDescription),
                OperationId = _options.OperationIdSelector(apiDescription),
                Parameters  = GenerateParameters(apiDescription, schemaRepository),
                RequestBody = GenerateRequestBody(apiDescription, schemaRepository),
                Responses   = GenerateResponses(apiDescription, schemaRepository),
                Deprecated  = apiDescription.CustomAttributes().OfType <ObsoleteAttribute>().Any()
            };

            var filterContext = new OperationFilterContext(apiDescription, _schemaGenerator, schemaRepository, apiDescription.MethodInfo());

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

            return(operation);
        }