private SwaggerGenerator Subject(
            Action <OrleansApiDescriptionGroupCollectionProvider> setupApis = null,
            Action <SwaggerGeneratorSettings> configure = null)
        {
            var apiDescriptionsProvider = new OrleansApiDescriptionGroupCollectionProvider(Options.Create <OrleansSwaggerGenOptions>(new OrleansSwaggerGenOptions()
            {
                GrainAssembly = typeof(IGrainTestService).Assembly
            }));

            setupApis?.Invoke(apiDescriptionsProvider);

            var options = new SwaggerGeneratorSettings();

            options.SwaggerDocs.Add("v1", new Info {
                Title = "API", Version = "v1"
            });

            configure?.Invoke(options);

            return(new SwaggerGenerator(
                       apiDescriptionsProvider,
                       new SchemaRegistryFactory(new JsonSerializerSettings(), new SchemaRegistrySettings()),
                       options
                       ));
        }
コード例 #2
0
        public static SwaggerGeneratorSettings Clone(this SwaggerGeneratorSettings setting)
        {
            var s = new SwaggerGeneratorSettings
            {
                SwaggerDocs                      = setting.SwaggerDocs,
                DocInclusionPredicate            = setting.DocInclusionPredicate,
                IgnoreObsoleteActions            = setting.IgnoreObsoleteActions,
                TagSelector                      = setting.TagSelector,
                SortKeySelector                  = setting.SortKeySelector,
                DescribeAllParametersInCamelCase = setting.DescribeAllParametersInCamelCase,
            };

            foreach (var def in setting.SecurityDefinitions)
            {
                s.SecurityDefinitions.Add(def.Key, def.Value);
            }
            foreach (var operationFilter in setting.OperationFilters)
            {
                s.OperationFilters.Add(operationFilter);
            }
            foreach (var documentFilter in setting.DocumentFilters)
            {
                s.DocumentFilters.Add(documentFilter);
            }
            return(s);
        }
コード例 #3
0
 //private readonly SwaggerGeneratorOptions _options;
 public SharpPlugSwaggerGenerator(IApiDescriptionGroupCollectionProvider apiDescriptionsProvider, ISchemaRegistryFactory schemaRegistryFactory, SwaggerGeneratorSettings settings, IOptions <SharpPlugRouterOptions> sharpPlugRouteroptions)
 {
     _apiDescriptionsProvider = apiDescriptionsProvider;
     _schemaRegistryFactory   = schemaRegistryFactory;
     _sharpPlugRouteroptions  = sharpPlugRouteroptions;
     _settings = settings ?? new SwaggerGeneratorSettings();
 }
コード例 #4
0
        public static void Init(TestContext testContext)
        {
            var options = new SwaggerGeneratorSettings();

            options.DocInclusionPredicate = (version, api) => true;
            options.OperationFilters.Add(new RemoveReadOnlyFromUriParametersOperationFilter());
            options.OperationFilters.Add(new RemoveBindNoneFromUriParametersOperationFilter());
            options.OperationFilters.Add(new AddAsObjectAnnotationOperationFilter());
            options.OperationFilters.Add(new HandleGridViewDataSetReturnType());
            options.SwaggerDocs.Add("v1", new Info()
            {
                Title = "Test API", Version = "v1"
            });

            var serviceCollection = new ServiceCollection()
                                    .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>()
                                    .AddSingleton <IHostingEnvironment, HostingEnvironment>()
                                    .AddLogging();

            serviceCollection.AddMvc(setup => {
                setup.Conventions.Add(new ApiExplorerVisibilityEnabledConvention());
            })
            .AddApplicationPart(typeof(CompaniesController).Assembly);

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var apiDescriptionGroupCollectionProvider = serviceProvider.GetService <IApiDescriptionGroupCollectionProvider>();

            var schemaRegistryFactory = new SchemaRegistryFactory(new JsonSerializerSettings(), new SchemaRegistrySettings());
            var generator             = new SwaggerGenerator(apiDescriptionGroupCollectionProvider, schemaRegistryFactory, options);

            document = generator.GetSwagger("v1");
        }
コード例 #5
0
        /// <summary>
        ///     Generates a Swagger (Open API) specification at the given path using the specified settings
        /// </summary>
        /// <param name="outputFile">File path for the generated API specification</param>
        /// <param name="configure">Optional settings to further control the specification</param>
        /// <returns>The metadata source</returns>
        /// <example>
        ///     <code><![CDATA[NSwag.FromWebApiAssembly("./apicontroller.dll").ToSwaggerSpecification("./api.json");]]></code>
        /// </example>
        public AssemblySource ToSwaggerSpecification(FilePath outputFile, Action <SwaggerGeneratorSettings> configure = null)
        {
            var settings = new SwaggerGeneratorSettings();

            configure?.Invoke(settings);
            ToSwaggerSpecification(outputFile, settings);
            return(this);
        }
コード例 #6
0
 /// <summary>Initializes a new instance of the <see cref="DocumentProcessorContext" /> class.</summary>
 /// <param name="document">The document.</param>
 /// <param name="controllerTypes">The controller types.</param>
 /// <param name="schemaResolver">The schema resolver.</param>
 /// <param name="schemaGenerator">The schema generator.</param>
 /// <param name="settings">The settings.</param>
 public DocumentProcessorContext(SwaggerDocument document, IEnumerable <Type> controllerTypes,
                                 JsonSchemaResolver schemaResolver, JsonSchemaGenerator schemaGenerator, SwaggerGeneratorSettings settings)
 {
     Document        = document;
     ControllerTypes = controllerTypes;
     SchemaResolver  = schemaResolver;
     SchemaGenerator = schemaGenerator;
     Settings        = settings;
 }
コード例 #7
0
        private void GenerateTypeSwagger(FilePath outputFile, SwaggerGeneratorSettings settings)
        {
            var args = Runner.GetToolArguments();

            args.Append("types2swagger");
            args.Append($"/assembly:{Source.GetRelativePath(Environment.WorkingDirectory)}");
            args.Append($"/output:{outputFile.FullPath}");
            args.Append($"/defaultenumhandling:{(settings.EnumAsString ? "String" : "Integer")}");
            args.Append($"/defaultpropertynamehandling:{(settings.CamelCaseProperties ? "CamelCase" : "Default")}");
            args.AddSwitch("ReferencePaths", string.Join(",", settings.AssemblyPaths.Select(a => a.FullPath)));
            Runner.Run(args);
        }
コード例 #8
0
 /// <summary>
 ///     Generates a Swagger (Open API) specification at the given path using the specified settings
 /// </summary>
 /// <param name="outputFile">File path for the generated API specification</param>
 /// <param name="settings">Settings to further control the spec generation process</param>
 /// <returns>The metadata source</returns>
 /// <example>
 ///     <code><![CDATA[NSwag.FromAssembly("./assembly.dll").ToSwaggerSpecification("./swagger.json");]]></code>
 ///     <code><![CDATA[NSwag.FromWebApiAssembly("./apicontroller.dll").ToSwaggerSpecification("./swagger.json");]]></code>
 /// </example>
 public AssemblySource ToSwaggerSpecification(FilePath outputFile, SwaggerGeneratorSettings settings)
 {
     settings = settings ?? new SwaggerGeneratorSettings();
     if (Mode == AssemblyMode.Normal)
     {
         GenerateTypeSwagger(outputFile, settings);
     }
     else
     {
         GenerateWebApiSwagger(outputFile, settings);
     }
     return(this);
 }
コード例 #9
0
 /// <summary>Initializes a new instance of the <see cref="AspNetCoreOperationProcessorContext" /> class.</summary>
 /// <param name="document">The document.</param>
 /// <param name="operationDescription">The operation description.</param>
 /// <param name="controllerType">Type of the controller.</param>
 /// <param name="methodInfo">The method information.</param>
 /// <param name="swaggerGenerator">The swagger generator.</param>
 /// <param name="schemaResolver">The schema resolver.</param>
 /// <param name="settings">The sett</param>
 /// <param name="allOperationDescriptions">All operation descriptions.</param>
 /// <param name="schemaGenerator">The schema generator.</param>
 public AspNetCoreOperationProcessorContext(
     SwaggerDocument document,
     SwaggerOperationDescription operationDescription,
     Type controllerType,
     MethodInfo methodInfo,
     SwaggerGenerator swaggerGenerator,
     JsonSchemaGenerator schemaGenerator,
     JsonSchemaResolver schemaResolver,
     SwaggerGeneratorSettings settings,
     IList <SwaggerOperationDescription> allOperationDescriptions)
     : base(document, operationDescription, controllerType, methodInfo, swaggerGenerator, schemaGenerator, schemaResolver, settings, allOperationDescriptions)
 {
 }
コード例 #10
0
        public static void Init(TestContext testContext)
        {
            var knownTypesOptions = Options.Create(new DotvvmApiOptions());

            knownTypesOptions.Value.AddKnownType(typeof(Company <string>));

            var options = new SwaggerGeneratorSettings {
                DocInclusionPredicate = (version, api) => true,
                OperationFilters      =
                {
                    new RemoveReadOnlyFromUriParametersOperationFilter(),
                    new RemoveBindNoneFromUriParametersOperationFilter(),
                    new AddAsObjectOperationFilter(knownTypesOptions)
                },
                DocumentFilters =
                {
                    new HandleKnownTypesDocumentFilter(knownTypesOptions)
                },
                SwaggerDocs =
                {
                    { "v1", new Info()
                        {
                            Title = "Test API", Version = "v1"
                        } }
                }
            };

            var serviceCollection = new ServiceCollection()
                                    .AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>()
                                    .AddSingleton <IHostingEnvironment, HostingEnvironment>()
                                    .AddLogging();

            serviceCollection.AddMvc(setup => setup.Conventions.Add(new ApiExplorerVisibilityEnabledConvention()))
            .AddApplicationPart(typeof(CompaniesController).Assembly);

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var apiDescriptionGroupCollectionProvider = serviceProvider.GetService <IApiDescriptionGroupCollectionProvider>();

            var schemaSettings = new SchemaRegistrySettings()
            {
                SchemaFilters =
                {
                    new AddTypeToModelSchemaFilter()
                }
            };

            var schemaRegistryFactory = new SchemaRegistryFactory(new JsonSerializerSettings(), schemaSettings);
            var generator             = new SwaggerGenerator(apiDescriptionGroupCollectionProvider, schemaRegistryFactory, options);

            document = generator.GetSwagger("v1");
        }
コード例 #11
0
ファイル: SwaggerGenOptions.cs プロジェクト: lzgscode/Ahoy
        public SwaggerGenOptions()
        {
            _swaggerGeneratorSettings = new SwaggerGeneratorSettings();
            _schemaRegistrySettings   = new SchemaRegistrySettings();

            _xmlDocFactories            = new List <Func <XPathDocument> >();
            _operationFilterDescriptors = new List <FilterDescriptor <IOperationFilter> >();
            _documentFilterDescriptors  = new List <FilterDescriptor <IDocumentFilter> >();
            _schemaFilterDescriptors    = new List <FilterDescriptor <ISchemaFilter> >();

            // Enable Annotations
            OperationFilter <SwaggerAttributesOperationFilter>();
            SchemaFilter <SwaggerAttributesSchemaFilter>();
        }
コード例 #12
0
        public SharpPlugSwaggerGenOptions()
        {
            SwaggerGeneratorSettings = new SwaggerGeneratorSettings();
            SchemaRegistrySettings   = new SchemaRegistrySettings();

            XmlDocFactories            = new List <Func <XPathDocument> >();
            OperationFilterDescriptors = new List <FilterDescriptor <IOperationFilter> >();
            DocumentFilterDescriptors  = new List <FilterDescriptor <IDocumentFilter> >();
            SchemaFilterDescriptors    = new List <FilterDescriptor <ISchemaFilter> >();

            // Enable Annotations
            //OperationFilter<SwaggerAttributesOperationFilter>();
            //OperationFilter<SwaggerResponseAttributeFilter>();
            //SchemaFilter<SwaggerAttributesSchemaFilter>();
        }
コード例 #13
0
        private void GenerateWebApiSwagger(FilePath outputFile, SwaggerGeneratorSettings settings)
        {
            var args = Runner.GetToolArguments();

            args.Append("webapi2swagger");
            args.AddSwitch("assembly", Source.FullPath)
            .AddSwitch("output", outputFile.FullPath)
            .AddSwitch("DefaultEnumHandling", settings.EnumAsString ? "String" : "Integer")
            .AddSwitch("ReferencePaths", string.Join(",", settings.AssemblyPaths.Select(a => a.FullPath)), true)
            .AddSwitch("DefaultPropertyNameHandling", settings.CamelCaseProperties ? "CamelCase" : "Default")
            .AddSwitch("ServiceBasePath", settings.BasePath)
            .AddSwitch("InfoTitle", settings.ApiTitle, true)
            .AddSwitch("InfoDescription", settings.ApiDescription, true)
            .AddSwitch("DefaultUrlTemplate", settings.DefaultUrlTemplate);
            Runner.Run(args);
        }
コード例 #14
0
        private void GenerateTypeSwagger(FilePath outputFile, SwaggerGeneratorSettings settings)
        {
            var genSettings = settings.JsonSettings as AssemblyTypeToSwaggerGeneratorSettings ??
                              SettingsFactory.GetAssemblyToSwaggerSettings();

            genSettings.AssemblyPath                = Source.MakeAbsolute(Environment).FullPath;
            genSettings.DefaultEnumHandling         = settings.EnumAsString ? EnumHandling.String : EnumHandling.Integer;
            genSettings.DefaultPropertyNameHandling = settings.CamelCaseProperties
                ? PropertyNameHandling.CamelCase
                : PropertyNameHandling.Default;
            genSettings.ReferencePaths = settings.AssemblyPaths.Select(a => a.FullPath).ToArray();
            var gen     = new AssemblyTypeToSwaggerGenerator(genSettings);
            var service = gen.Generate(gen.GetClasses());

            using (var stream = new StreamWriter(FileSystem.GetFile(outputFile).OpenWrite()))
            {
                stream.WriteAsync(service.ToJson());
            }
        }
コード例 #15
0
 /// <summary>Initializes a new instance of the <see cref="OperationProcessorContext" /> class.</summary>
 /// <param name="document">The document.</param>
 /// <param name="operationDescription">The operation description.</param>
 /// <param name="controllerType">Type of the controller.</param>
 /// <param name="methodInfo">The method information.</param>
 /// <param name="swaggerGenerator">The swagger generator.</param>
 /// <param name="schemaResolver">The schema resolver.</param>
 /// <param name="settings">The settings.</param>
 /// <param name="allOperationDescriptions">All operation descriptions.</param>
 /// <param name="schemaGenerator">The schema generator.</param>
 public OperationProcessorContext(
     SwaggerDocument document,
     SwaggerOperationDescription operationDescription,
     Type controllerType,
     MethodInfo methodInfo,
     SwaggerGenerator swaggerGenerator,
     JsonSchemaGenerator schemaGenerator,
     JsonSchemaResolver schemaResolver,
     SwaggerGeneratorSettings settings,
     IList <SwaggerOperationDescription> allOperationDescriptions)
 {
     Document                 = document;
     OperationDescription     = operationDescription;
     ControllerType           = controllerType;
     MethodInfo               = methodInfo;
     SwaggerGenerator         = swaggerGenerator;
     SchemaResolver           = schemaResolver;
     Settings                 = settings;
     SchemaGenerator          = schemaGenerator;
     AllOperationDescriptions = allOperationDescriptions;
 }
コード例 #16
0
        private SwaggerGenerator Subject(
            Action <FakeApiDescriptionGroupCollectionProvider> setupApis = null,
            Action <SwaggerGeneratorSettings> configure = null)
        {
            var apiDescriptionsProvider = new FakeApiDescriptionGroupCollectionProvider();

            setupApis?.Invoke(apiDescriptionsProvider);

            var options = new SwaggerGeneratorSettings();

            options.SwaggerDocs.Add("v1", new Info {
                Title = "API", Version = "v1"
            });

            configure?.Invoke(options);

            return(new SwaggerGenerator(
                       apiDescriptionsProvider,
                       new SchemaRegistryFactory(new JsonSerializerSettings(), new SchemaRegistrySettings()),
                       options
                       ));
        }
コード例 #17
0
        private void GenerateWebApiSwagger(FilePath outputFile, SwaggerGeneratorSettings settings)
        {
            var genSettings = settings.JsonSettings as WebApiAssemblyToSwaggerGeneratorSettings ??
                              SettingsFactory.GetWebApiToSwaggerSettings();

            genSettings.AssemblyPaths               = new [] { Source.MakeAbsolute(Environment).FullPath };
            genSettings.DefaultUrlTemplate          = settings.DefaultUrlTemplate;
            genSettings.DefaultEnumHandling         = settings.EnumAsString ? EnumHandling.String : EnumHandling.Integer;
            genSettings.DefaultPropertyNameHandling = settings.CamelCaseProperties
                ? PropertyNameHandling.CamelCase
                : PropertyNameHandling.Default;
            genSettings.NullHandling   = NullHandling.Swagger;
            genSettings.ReferencePaths = settings.AssemblyPaths.Select(a => a.FullPath).ToArray();
            var gen     = new WebApiAssemblyToSwaggerGenerator(genSettings);
            var service = gen.GenerateForControllers(gen.GetControllerClasses());

            service.BasePath         = settings.BasePath ?? "";
            service.Info.Title       = settings.ApiTitle ?? "";
            service.Info.Description = settings.ApiDescription ?? "";
            using (var stream = new StreamWriter(FileSystem.GetFile(outputFile).OpenWrite()))
            {
                stream.WriteAsync(service.ToJson()).Wait();
            }
        }
コード例 #18
0
 /// <summary>Initializes a new instance of the <see cref="OperationResponseProcessorBase"/> class.</summary>
 /// <param name="settings">The settings.</param>
 public OperationResponseProcessorBase(SwaggerGeneratorSettings settings)
 {
     _settings = settings;
 }
コード例 #19
0
 /// <summary>Appends the OAuth2 security scheme and requirement to the document's security definitions.</summary>
 /// <remarks>Adds a <see cref="SecurityDefinitionAppender"/> document processor with the given arguments.</remarks>
 /// <param name="settings">The settings.</param>
 /// <param name="name">The name/key of the security scheme/definition.</param>
 /// <param name="scopeNames">The scope names to add to as security requirement with the scheme name in the 'security' property (can be an empty list).</param>
 /// <param name="swaggerSecurityScheme">The Swagger security scheme.</param>
 public static SwaggerGeneratorSettings AddSecurity(this SwaggerGeneratorSettings settings, string name, IEnumerable <string> scopeNames, SwaggerSecurityScheme swaggerSecurityScheme)
 {
     settings.DocumentProcessors.Add(new SecurityDefinitionAppender(name, scopeNames, swaggerSecurityScheme));
     return(settings);
 }