예제 #1
0
        private void GenerateNoContentResponse(IEndPointMethodHandler endPointMethodHandler, OpenApiResponses responses, bool success)
        {
            if (endPointMethodHandler.RouteInformation.HasBody)
            {
                if (!responses.ContainsKey("204"))
                {
                    var response = new OpenApiResponse {
                        Description = "No content"
                    };

                    if (success)
                    {
                        response.Description += " - successful";
                    }

                    responses.Add("204", response);
                }
            }
            else
            {
                if (!responses.ContainsKey("404"))
                {
                    responses.Add("404", new OpenApiResponse {
                        Description = "No resource found"
                    });
                }
            }
        }
예제 #2
0
        public void ApiConfigurationComplete(IServiceProvider serviceScope)
        {
            _emptyHandler =
                new NoParamsEndPointMethodHandler <object>(
                    new EndPointMethodConfiguration(
                        new RpcRouteInformation {
                Method = "OPTIONS", HasBody = false, RouteBasePath = "*"
            },
                        context => new object(),
                        new MethodInvokeInformation {
                MethodInvokeDelegate = context => null
            },
                        typeof(object)),
                    serviceScope.GetService(typeof(EndPointServices)) as EndPointServices);

            _optionsConfig = _configurationManager.GetConfiguration <OptionsMethodConfiguration>();

            foreach (var supportedMethod in _optionsConfig.SupportedMethods)
            {
                if (!string.IsNullOrEmpty(_methods))
                {
                    _methods += ", ";
                }

                _methods += supportedMethod;
            }
        }
예제 #3
0
        private IList <OpenApiParameter> GenerateParameters(IEndPointMethodHandler endPointMethodHandler,
                                                            XElement element)
        {
            var parameterList = new List <OpenApiParameter>();

            var urlParameterEnumerable = endPointMethodHandler.Configuration.Parameters.Where(p =>
                                                                                              p.ParameterSource == EndPointMethodParameterSource.PathParameter ||
                                                                                              p.ParameterSource == EndPointMethodParameterSource.QueryStringParameter);

            foreach (var configurationParameter in urlParameterEnumerable)
            {
                var apiParameter = new OpenApiParameter
                {
                    Name        = configurationParameter.Name,
                    Description = element.GetParameterSummary(configurationParameter.Name),
                    Schema      = _apiSchemaGenerator.GetSchemaType(configurationParameter.ParamType)
                };

                switch (configurationParameter.ParameterSource)
                {
                case EndPointMethodParameterSource.PathParameter:
                    apiParameter.In = ParameterLocation.Path;
                    break;

                case EndPointMethodParameterSource.QueryStringParameter:
                    apiParameter.In = ParameterLocation.Query;
                    break;
                }

                parameterList.Add(apiParameter);
            }

            return(parameterList);
        }
예제 #4
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="httpContext"></param>
 /// <param name="handler"></param>
 /// <param name="httpStatusCode"></param>
 public RequestExecutionContext(HttpContext httpContext, IEndPointMethodHandler handler, int httpStatusCode)
 {
     _continueRequest = true;
     HttpContext      = httpContext;
     Handler          = handler;
     HttpStatusCode   = httpStatusCode;
 }
예제 #5
0
        private void GenerateEmptySuccessResponse(IEndPointMethodHandler endPointMethodHandler, OpenApiResponses responses)
        {
            var successStatusCode = endPointMethodHandler.Configuration.SuccessStatusCode.ToString();
            var contentDictionary = new Dictionary <string, OpenApiMediaType>();
            var response          = new OpenApiResponse {
                Content = contentDictionary, Description = "Success"
            };

            responses.Add(successStatusCode, response);
        }
예제 #6
0
        private void GenerateSuccessResponse(IEndPointMethodHandler endPointMethodHandler, OpenApiResponses responses)
        {
            var successStatusCode = endPointMethodHandler.Configuration.SuccessStatusCode.ToString();
            var contentDictionary = new Dictionary <string, OpenApiMediaType>();
            var response          = new OpenApiResponse {
                Content = contentDictionary, Description = "Success"
            };
            OpenApiSchema responseSchema = null;

            var returnType = endPointMethodHandler.Configuration.DocumentationReturnType;

            if (returnType.IsConstructedGenericType &&
                (returnType.GetGenericTypeDefinition() == typeof(Task <>) ||
                 returnType.GetGenericTypeDefinition() == typeof(ValueTask <>)))
            {
                returnType = returnType.GenericTypeArguments[0];
            }

            if (endPointMethodHandler.Configuration.WrappedType != null)
            {
                responseSchema = new OpenApiSchema
                {
                    Properties = new Dictionary <string, OpenApiSchema>
                    {
                        { "result", _apiSchemaGenerator.GetSchemaType(returnType) }
                    }
                };
            }
            else
            {
                responseSchema = _apiSchemaGenerator.GetSchemaType(returnType);
            }

            if (string.IsNullOrEmpty(endPointMethodHandler.Configuration.RawContentType))
            {
                foreach (var supportedContentType in _contentSerializationService.SupportedContentTypes)
                {
                    contentDictionary[supportedContentType] = new OpenApiMediaType
                    {
                        Schema = responseSchema
                    };
                }
            }
            else
            {
                contentDictionary[endPointMethodHandler.Configuration.RawContentType] = new OpenApiMediaType
                {
                    Schema = responseSchema
                };
            }

            responses.Add(successStatusCode, response);
        }
예제 #7
0
        private OpenApiResponses GenerateResponses(IEndPointMethodHandler endPointMethodHandler, XElement element)
        {
            var responses  = new OpenApiResponses();
            var hasContent = false;

            if (endPointMethodHandler.Configuration.DocumentationReturnType != typeof(void) &&
                endPointMethodHandler.Configuration.DocumentationReturnType != typeof(Task) &&
                endPointMethodHandler.Configuration.DocumentationReturnType != typeof(ValueTask))
            {
                hasContent = true;

                GenerateSuccessResponse(endPointMethodHandler, responses);
            }

            GenerateNoContentResponse(endPointMethodHandler, responses, !hasContent);

            GenerateErrorResponse(endPointMethodHandler, responses);

            return(responses);
        }
예제 #8
0
        private void GenerateErrorResponse(IEndPointMethodHandler endPointMethodHandler, OpenApiResponses responses)
        {
            if (!responses.ContainsKey("500"))
            {
                var response = new OpenApiResponse {
                    Description = "Internal Server Error"
                };

                var contentDictionary = response.Content = new Dictionary <string, OpenApiMediaType>();
                var responseSchema    = _apiSchemaGenerator.GetSchemaType(_errorResultTypeCreator.GenerateErrorType());

                foreach (var supportedContentType in _contentSerializationService.SupportedContentTypes)
                {
                    contentDictionary[supportedContentType] = new OpenApiMediaType
                    {
                        Schema = responseSchema
                    };
                }

                responses.Add("500", response);
            }
        }
예제 #9
0
 public HeadEndPointMethodHandler(IEndPointMethodHandler getMethodHandler, IEndPointMethodConfigurationReadOnly configuration)
 {
     _getMethodHandler = getMethodHandler;
     Configuration     = configuration;
 }
예제 #10
0
        private OpenApiRequestBody GenerateRequestBody(IEndPointMethodHandler endPointMethodHandler, XElement element)
        {
            var requestParameter =
                endPointMethodHandler.Configuration.Parameters.FirstOrDefault(p =>
                                                                              p.ParameterSource == EndPointMethodParameterSource.PostBody);

            if (requestParameter != null)
            {
                var request = new OpenApiRequestBody
                {
                    Content = new Dictionary <string, OpenApiMediaType>()
                };

                var schema = _apiSchemaGenerator.GetSchemaType(requestParameter.ParamType);

                foreach (var schemaProperty in schema.Properties)
                {
                    schemaProperty.Value.Description = element.GetParameterSummary(schemaProperty.Key);
                }

                foreach (var contentType in _contentSerializationService.SupportedContentTypes)
                {
                    request.Content[contentType] = new OpenApiMediaType
                    {
                        Schema = schema
                    };
                }

                return(request);
            }

            var postParameterList = endPointMethodHandler.Configuration.Parameters.Where(p =>
                                                                                         p.ParameterSource == EndPointMethodParameterSource.PostParameter).ToList();

            if (postParameterList.Count > 0)
            {
                var request = new OpenApiRequestBody
                {
                    Content = new Dictionary <string, OpenApiMediaType>()
                };

                var schema = GeneratePostParameterSchema(postParameterList);

                schema.Xml = new OpenApiXml
                {
                    Name = "args"
                };

                foreach (var schemaProperty in schema.Properties)
                {
                    schemaProperty.Value.Description = element.GetParameterSummary(schemaProperty.Key);
                }

                foreach (var contentType in _contentSerializationService.SupportedContentTypes)
                {
                    request.Content[contentType] = new OpenApiMediaType
                    {
                        Schema = schema
                    };
                }

                return(request);
            }

            return(null);
        }
예제 #11
0
        private OpenApiOperation GenerateApiOperation(OpenApiDocument document,
                                                      IEndPointMethodHandler endPointMethodHandler)
        {
            var methodInfo = endPointMethodHandler.Configuration.InvokeInformation.MethodToInvoke;

            XElement element = null;

            if (methodInfo != null)
            {
                element = _xmlDocProvider.GetMethodDocumentation(methodInfo);
            }

            var operationId = endPointMethodHandler.RouteInformation.RouteBasePath.Replace("/", "") + "-" +
                              endPointMethodHandler.HttpMethod;

            var operation = new OpenApiOperation
            {
                Summary     = element.GetSummary(),
                Tags        = new List <OpenApiTag>(),
                Parameters  = GenerateParameters(endPointMethodHandler, element),
                OperationId = operationId
            };

            foreach (var tag in element.GetTags())
            {
                operation.Tags.Add(new OpenApiTag {
                    Name = tag
                });
            }

            if (methodInfo != null && methodInfo.DeclaringType != null)
            {
                var parentElement = _xmlDocProvider.GetTypeDocumentation(methodInfo.DeclaringType);

                if (parentElement != null)
                {
                    foreach (var tag in parentElement.GetTags())
                    {
                        operation.Tags.Add(new OpenApiTag {
                            Name = tag
                        });
                    }
                }
            }

            if (operation.Tags.Count == 0 &&
                _documentationOptions.AutoTag &&
                methodInfo?.DeclaringType != null)
            {
                var tagName = methodInfo.DeclaringType.Name;

                operation.Tags.Add(new OpenApiTag {
                    Name = tagName
                });

                if (document.Tags.All(tag => tag.Name != tagName))
                {
                    var parentElement = _xmlDocProvider.GetTypeDocumentation(methodInfo.DeclaringType);

                    var newTag = new OpenApiTag
                    {
                        Name        = tagName,
                        Description = parentElement.GetSummary()
                    };

                    document.Tags.Add(newTag);
                }
            }

            if (endPointMethodHandler.RouteInformation.HasBody)
            {
                operation.RequestBody = GenerateRequestBody(endPointMethodHandler, element);
            }

            operation.Responses = GenerateResponses(endPointMethodHandler, element);

            return(operation);
        }