Beispiel #1
0
        /// <summary>
        /// Generates an OpenAPI document per variant specified in configuration.
        /// In addition, a "default" variant document is generated, which contains no alterations based on
        /// variant metadata.
        /// </summary>
        /// <param name="openApiGeneratorConfig">The configuration that will be used to generate
        /// the document.</param>
        /// <param name="generationDiagnostic">The generation diagnostics.</param>
        /// <param name="openApiDocumentGenerationSettings">The optional Open API document generation settings.</param>
        /// <returns>Dictionary mapping document variant metadata to their respective OpenAPI document.</returns>
        public IDictionary <DocumentVariantInfo, OpenApiDocument> GenerateDocuments(
            OpenApiGeneratorConfig openApiGeneratorConfig,
            out GenerationDiagnostic generationDiagnostic,
            OpenApiDocumentGenerationSettings openApiDocumentGenerationSettings = null)
        {
            foreach (var assemblyPath in openApiGeneratorConfig.AssemblyPaths)
            {
                if (!File.Exists(assemblyPath))
                {
                    throw new FileNotFoundException(assemblyPath);
                }
            }

            if (openApiDocumentGenerationSettings == null)
            {
                openApiDocumentGenerationSettings = new OpenApiDocumentGenerationSettings(
                    new SchemaGenerationSettings(new DefaultPropertyNameResolver()));
            }

            var internalOpenApiGenerator = new InternalOpenApiGenerator(
                openApiGeneratorConfig.OpenApiGeneratorFilterConfig,
                openApiDocumentGenerationSettings);

            return(internalOpenApiGenerator.GenerateOpenApiDocuments(
                       openApiGeneratorConfig.AnnotationXmlDocuments,
                       openApiGeneratorConfig.AssemblyPaths,
                       openApiGeneratorConfig.AdvancedConfigurationXmlDocument?.ToString(),
                       openApiGeneratorConfig.OpenApiDocumentVersion,
                       openApiGeneratorConfig.OpenApiInfoDescription,
                       out generationDiagnostic));
        }
Beispiel #2
0
        /// <summary>
        /// Generates an OpenAPI document based on the provided configuration, but ignores any variant configuration
        /// that may be present.
        /// </summary>
        /// <param name="openApiGeneratorConfig">The configuration that will be used to generate
        /// the document.</param>
        /// <param name="generationDiagnostic">The generation diagnostics.</param>
        /// <param name="openApiDocumentGenerationSettings">The optional Open API document generation settings.</param>
        /// <returns>The generated OpenAPI document.</returns>
        public OpenApiDocument GenerateDocument(
            OpenApiGeneratorConfig openApiGeneratorConfig,
            out GenerationDiagnostic generationDiagnostic,
            OpenApiDocumentGenerationSettings openApiDocumentGenerationSettings = null)
        {
            var documents = GenerateDocuments(
                openApiGeneratorConfig,
                out generationDiagnostic,
                openApiDocumentGenerationSettings);

            return(documents?.Count == 0 ? null : documents[DocumentVariantInfo.Default]);
        }
        /// <summary>
        /// Creates a new instance of <see cref="InternalOpenApiGenerator"/>.
        /// </summary>
        /// <param name="openApiGeneratorFilterConfig">The configuration encapsulating all the filters
        /// that will be applied while generating/processing OpenAPI document from C# annotations.</param>
        /// <param name="openApiDocumentGenerationSettings">The settings to use for Open API document generation.
        /// </param>
        public InternalOpenApiGenerator(
            OpenApiGeneratorFilterConfig openApiGeneratorFilterConfig,
            OpenApiDocumentGenerationSettings openApiDocumentGenerationSettings)
        {
            if (openApiGeneratorFilterConfig == null)
            {
                throw new ArgumentNullException(nameof(openApiGeneratorFilterConfig));
            }
            _openApiDocumentGenerationSettings = openApiDocumentGenerationSettings
                                                 ?? throw new ArgumentNullException(nameof(openApiDocumentGenerationSettings));

            _documentFilters               = TypeCastFilters <IDocumentFilter>(openApiGeneratorFilterConfig.Filters);
            _documentConfigFilters         = TypeCastFilters <IDocumentConfigFilter>(openApiGeneratorFilterConfig.Filters);
            _operationFilters              = TypeCastFilters <IOperationFilter>(openApiGeneratorFilterConfig.Filters);
            _operationConfigFilters        = TypeCastFilters <IOperationConfigFilter>(openApiGeneratorFilterConfig.Filters);
            _preProcessingOperationFilters = TypeCastFilters <IPreProcessingOperationFilter>(
                openApiGeneratorFilterConfig.Filters);
            _postProcessingDocumentFilters = TypeCastFilters <IPostProcessingDocumentFilter>(
                openApiGeneratorFilterConfig.Filters);
        }