/// <summary>
        /// Populate the specification documents for all document variant infos.
        /// </summary>
        /// <returns>The operation generation results from populating the specification documents.</returns>
        private IList <OperationGenerationDiagnostic> GenerateSpecificationDocuments(
            TypeFetcher typeFetcher,
            IList <XElement> operationElements,
            XElement operationConfigElement,
            string documentVariantElementName,
            out IDictionary <DocumentVariantInfo, OpenApiDocument> specificationDocuments)
        {
            specificationDocuments = new Dictionary <DocumentVariantInfo, OpenApiDocument>();

            var operationGenerationResults = new List <OperationGenerationDiagnostic>();

            var referenceRegistryManagerMap = new Dictionary <DocumentVariantInfo, ReferenceRegistryManager>();

            foreach (var operationElement in operationElements)
            {
                string        url;
                OperationType operationMethod;

                try
                {
                    url = OperationHandler.GetUrl(operationElement);
                }
                catch (InvalidUrlException e)
                {
                    operationGenerationResults.Add(
                        new OperationGenerationDiagnostic
                    {
                        Errors =
                        {
                            new GenerationError
                            {
                                ExceptionType = e.GetType().Name,
                                Message       = e.Message
                            }
                        },
                        OperationMethod = SpecificationGenerationMessages.OperationMethodNotParsedGivenUrlIsInvalid,
                        Path            = e.Url
                    });

                    continue;
                }

                try
                {
                    operationMethod = OperationHandler.GetOperationMethod(url, operationElement);
                }
                catch (InvalidVerbException e)
                {
                    operationGenerationResults.Add(
                        new OperationGenerationDiagnostic
                    {
                        Errors =
                        {
                            new GenerationError
                            {
                                ExceptionType = e.GetType().Name,
                                Message       = e.Message
                            }
                        },
                        OperationMethod = e.Verb,
                        Path            = url
                    });

                    continue;
                }

                var operationGenerationErrors = new List <GenerationError>();

                try
                {
                    AddOperation(
                        specificationDocuments,
                        referenceRegistryManagerMap,
                        operationGenerationErrors,
                        DocumentVariantInfo.Default,
                        operationElement,
                        operationConfigElement,
                        typeFetcher);
                }
                catch (Exception e)
                {
                    operationGenerationErrors.Add(
                        new GenerationError
                    {
                        ExceptionType = e.GetType().Name,
                        Message       = e.Message
                    });
                }

                var customElements = operationElement.Descendants(documentVariantElementName);
                foreach (var customElement in customElements)
                {
                    try
                    {
                        var documentVariantInfo = new DocumentVariantInfo
                        {
                            Categorizer = customElement.Name.LocalName.Trim(),
                            Title       = customElement.Value.Trim()
                        };

                        AddOperation(
                            specificationDocuments,
                            referenceRegistryManagerMap,
                            operationGenerationErrors,
                            documentVariantInfo,
                            operationElement,
                            operationConfigElement,
                            typeFetcher);
                    }
                    catch (Exception e)
                    {
                        operationGenerationErrors.Add(
                            new GenerationError
                        {
                            ExceptionType = e.GetType().Name,
                            Message       = e.Message
                        });
                    }
                }

                var operationGenerationResult = new OperationGenerationDiagnostic
                {
                    OperationMethod = operationMethod.ToString(),
                    Path            = url
                };

                if (operationGenerationErrors.Any())
                {
                    foreach (var error in operationGenerationErrors)
                    {
                        operationGenerationResult.Errors.Add(new GenerationError(error));
                    }
                }

                operationGenerationResults.Add(operationGenerationResult);
            }

            foreach (var documentVariantInfo in specificationDocuments.Keys)
            {
                referenceRegistryManagerMap[documentVariantInfo]
                .SchemaReferenceRegistry.References.CopyInto(
                    specificationDocuments[documentVariantInfo].Components.Schemas);
            }

            return(operationGenerationResults);
        }
コード例 #2
0
        /// <summary>
        /// Populate the specification documents for all document variant infos.
        /// </summary>
        /// <returns>The operation generation results from populating the specification documents.</returns>
        private IList <OperationGenerationDiagnostic> GenerateSpecificationDocuments(
            GenerationContext generationContext,
            IList <XElement> operationElements,
            XElement operationConfigElement,
            string documentVariantElementName,
            out IDictionary <DocumentVariantInfo, OpenApiDocument> specificationDocuments)
        {
            specificationDocuments = new Dictionary <DocumentVariantInfo, OpenApiDocument>();

            var operationGenerationResults = new List <OperationGenerationDiagnostic>();

            var referenceRegistryManagerMap = new Dictionary <DocumentVariantInfo, ReferenceRegistryManager>();

            foreach (var operationElement in operationElements)
            {
                string        url;
                OperationType operationMethod;
                OperationType operationMethod1 = OperationType.Trace;

                try
                {
                    url = OperationHandler.GetUrl(operationElement);
                }
                catch (InvalidUrlException e)
                {
                    operationGenerationResults.Add(
                        new OperationGenerationDiagnostic
                    {
                        Errors =
                        {
                            new GenerationError
                            {
                                ExceptionType = e.GetType().Name,
                                Message       = e.Message
                            }
                        },
                        OperationMethod = SpecificationGenerationMessages.OperationMethodNotParsedGivenUrlIsInvalid,
                        Path            = e.Url
                    });

                    continue;
                }
                bool doubleverb = false;
                try
                {
                    string safe = OperationHandler.GetOperationMethod(url, operationElement);
                    if (safe.Contains('/'))
                    {
                        string[] split = safe.Split('/');
                        doubleverb = true;
                        Enum.TryParse(split[0], true, out operationMethod);
                        Enum.TryParse(split[1], true, out operationMethod1);
                    }
                    else
                    {
                        Enum.TryParse(safe, true, out operationMethod);
                    }
                }
                catch (InvalidVerbException e)
                {
                    operationGenerationResults.Add(
                        new OperationGenerationDiagnostic
                    {
                        Errors =
                        {
                            new GenerationError
                            {
                                ExceptionType = e.GetType().Name,
                                Message       = e.Message
                            }
                        },
                        OperationMethod = e.Verb,
                        Path            = url
                    });

                    continue;
                }

                var operationGenerationErrors = new List <GenerationError>();

                try
                {
                    AddOperation(
                        specificationDocuments,
                        referenceRegistryManagerMap,
                        operationGenerationErrors,
                        DocumentVariantInfo.Default,
                        operationElement,
                        operationConfigElement,
                        generationContext);
                }
                catch (Exception e)
                {
                    operationGenerationErrors.Add(
                        new GenerationError
                    {
                        ExceptionType = e.GetType().Name,
                        Message       = e.Message
                    });
                }

                var customElements = operationElement.Descendants(documentVariantElementName);
                foreach (var customElement in customElements)
                {
                    try
                    {
                        var documentVariantInfo = new DocumentVariantInfo
                        {
                            Categorizer = customElement.Name.LocalName.Trim(),
                            Title       = customElement.Value.Trim()
                        };

                        AddOperation(
                            specificationDocuments,
                            referenceRegistryManagerMap,
                            operationGenerationErrors,
                            documentVariantInfo,
                            operationElement,
                            operationConfigElement,
                            generationContext);
                    }
                    catch (Exception e)
                    {
                        operationGenerationErrors.Add(
                            new GenerationError
                        {
                            ExceptionType = e.GetType().Name,
                            Message       = e.Message
                        });
                    }
                }

                if (doubleverb)
                {
                    doubleverb = false;
                    var operationGenerationResult = new OperationGenerationDiagnostic
                    {
                        OperationMethod = operationMethod.ToString(),
                        Path            = url
                    };
                    var operationGenerationResult1 = new OperationGenerationDiagnostic
                    {
                        OperationMethod = operationMethod1.ToString(),
                        Path            = url
                    };

                    if (operationGenerationErrors.Any())
                    {
                        foreach (var error in operationGenerationErrors)
                        {
                            operationGenerationResult.Errors.Add(new GenerationError(error));
                        }
                    }
                    operationGenerationResults.Add(operationGenerationResult);
                    operationGenerationResults.Add(operationGenerationResult1);
                }
                else
                {
                    var operationGenerationResult = new OperationGenerationDiagnostic
                    {
                        OperationMethod = operationMethod.ToString(),
                        Path            = url
                    };

                    if (operationGenerationErrors.Any())
                    {
                        foreach (var error in operationGenerationErrors)
                        {
                            operationGenerationResult.Errors.Add(new GenerationError(error));
                        }
                    }
                    operationGenerationResults.Add(operationGenerationResult);
                }
            }

            foreach (var documentVariantInfo in generationContext.VariantSchemaReferenceMap.Keys)
            {
                var references = generationContext.VariantSchemaReferenceMap[documentVariantInfo].ToDictionary(
                    k => k.Key,
                    k => k.Value);

                if (specificationDocuments.ContainsKey(documentVariantInfo))
                {
                    references.CopyInto(specificationDocuments[documentVariantInfo].Components.Schemas);
                }
            }

            foreach (var documentVariantInfo in specificationDocuments.Keys)
            {
                referenceRegistryManagerMap[documentVariantInfo]
                .SecuritySchemeReferenceRegistry.References.CopyInto(
                    specificationDocuments[documentVariantInfo].Components.SecuritySchemes);
            }

            return(operationGenerationResults);
        }