Esempio n. 1
0
        private IDictionary <OperationType, OpenApiOperation> GenerateOperations(
            IEnumerable <ApiDescription> apiDescriptions,
            SchemaRepository schemaRepository)
        {
            var apiDescriptionsByMethod = apiDescriptions
                                          .OrderBy(_options.SortKeySelector)
                                          .GroupBy(apiDesc => apiDesc.HttpMethod);

            var operations = new Dictionary <OperationType, OpenApiOperation>();

            foreach (var group in apiDescriptionsByMethod)
            {
                var httpMethod = group.Key;

                if ((httpMethod == null) && (!_options.IgnoreHttpAttributeMissing))
                {
                    throw new SwaggerGeneratorException(string.Format(
                                                            "Ambiguous HTTP method for action - {0}. " +
                                                            "Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0",
                                                            group.First().ActionDescriptor.DisplayName));
                }

                if (group.Count() > 1 && _options.ConflictingActionsResolver == null)
                {
                    throw new SwaggerGeneratorException(string.Format(
                                                            "Conflicting method/path combination \"{0} {1}\" for actions - {2}. " +
                                                            "Actions require a unique method/path combination for Swagger/OpenAPI 3.0. Use ConflictingActionsResolver as a workaround",
                                                            httpMethod,
                                                            group.First().RelativePathSansQueryString(),
                                                            string.Join(",", group.Select(apiDesc => apiDesc.ActionDescriptor.DisplayName))));
                }

                var apiDescription = (group.Count() > 1) ? _options.ConflictingActionsResolver(group) : group.Single();

                if (httpMethod != null)
                {
                    operations.Add(OperationTypeMap[httpMethod.ToUpper()], GenerateOperation(apiDescription, schemaRepository));
                }
            }
            ;

            return(operations);
        }
Esempio n. 2
0
        private PathItem CreatePathItem(
            IEnumerable <ApiDescription> apiDescriptions,
            ISchemaRegistry schemaRegistry)
        {
            var pathItem = new PathItem();

            // Group further by http method
            var perMethodGrouping = apiDescriptions
                                    .GroupBy(apiDesc => apiDesc.HttpMethod);

            foreach (var group in perMethodGrouping)
            {
                var httpMethod = group.Key;

                if (httpMethod == null)
                {
                    throw new NotSupportedException(string.Format(
                                                        "Ambiguous HTTP method for action - {0}. " +
                                                        "Actions require an explicit HttpMethod binding for Swagger 2.0",
                                                        group.First().ActionDescriptor.DisplayName));
                }

                if (group.Count() > 1 && _options.ConflictingActionsResolver == null)
                {
                    throw new NotSupportedException(string.Format(
                                                        "HTTP method \"{0}\" & path \"{1}\" overloaded by actions - {2}. " +
                                                        "Actions require unique method/path combination for Swagger 2.0. Use ConflictingActionsResolver as a workaround",
                                                        httpMethod,
                                                        group.First().RelativePathSansQueryString(),
                                                        string.Join(",", group.Select(apiDesc => apiDesc.ActionDescriptor.DisplayName))));
                }

                var apiDescription = (group.Count() > 1) ? _options.ConflictingActionsResolver(group) : group.Single();

                switch (httpMethod)
                {
                case "GET":
                    pathItem.Get = CreateOperation(apiDescription, schemaRegistry);
                    break;

                case "PUT":
                    pathItem.Put = CreateOperation(apiDescription, schemaRegistry);
                    break;

                case "POST":
                    pathItem.Post = CreateOperation(apiDescription, schemaRegistry);
                    break;

                case "DELETE":
                    pathItem.Delete = CreateOperation(apiDescription, schemaRegistry);
                    break;

                case "OPTIONS":
                    pathItem.Options = CreateOperation(apiDescription, schemaRegistry);
                    break;

                case "HEAD":
                    pathItem.Head = CreateOperation(apiDescription, schemaRegistry);
                    break;

                case "PATCH":
                    pathItem.Patch = CreateOperation(apiDescription, schemaRegistry);
                    break;
                }
            }

            return(pathItem);
        }