Ejemplo n.º 1
0
        private static OpenApiDocument CreateDocument(params Assembly[] assemblies)
        {
            var swaggerGenerator = new WebApiOpenApiDocumentGenerator(SettingsFactory.WebApiOpenApiDocumentGeneratorSettings);
            var controllers      = assemblies.SelectMany(x => x.GetTypes().Where(c => typeof(ControllerBase).IsAssignableFrom(c)));

            return(swaggerGenerator.GenerateForControllersAsync(controllers).Result);
        }
Ejemplo n.º 2
0
        public async Task When_operations_have_no_tags_they_are_grouped_into_one_client()
        {
            // Arrange
            var generator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings());
            var document  = await generator.GenerateForControllersAsync(new List <Type>() { typeof(PointControllerA), typeof(PointControllerB) });

            // Remove tags
            foreach (var path in document.Paths.Values)
            {
                foreach (var operation in path.Values)
                {
                    operation.Tags.Clear();
                }
            }
            var codeGenerator = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
            {
                OperationNameGenerator = new MultipleClientsFromFirstTagAndPathSegmentsOperationNameGenerator()
            });

            // Act
            var code = codeGenerator.GenerateFile();

            // Assert
            Assert.IsTrue(code.Contains("export class Client"));
            Assert.IsTrue(!code.Contains("export class PointControllerAClient"));
            Assert.IsTrue(!code.Contains("export class PointControllerBClient"));
        }
Ejemplo n.º 3
0
        /// <summary>Generates the Swagger specification.</summary>
        /// <param name="context">The context.</param>
        /// <returns>The Swagger specification.</returns>
        protected virtual async Task <string> GenerateDocumentAsync(IOwinContext context)
        {
            var settings  = _settings.CreateGeneratorSettings(null, null);
            var generator = new WebApiOpenApiDocumentGenerator(settings);
            var document  = await generator.GenerateForControllersAsync(_controllerTypes);

            if (_settings.MiddlewareBasePath != null)
            {
                document.Host = context.Request.Host.Value ?? "";
                document.Schemes.Add(context.Request.Scheme == "http" ? OpenApiSchema.Http : OpenApiSchema.Https);
                document.BasePath = context.Request.PathBase.Value?.Substring(0, context.Request.PathBase.Value.Length - (_settings.MiddlewareBasePath?.Length ?? 0)) ?? "";
            }
            else
            {
                document.Servers.Clear();
                document.Servers.Add(new OpenApiServer
                {
                    Url = context.Request.GetServerUrl()
                });
            }

            _settings.PostProcess?.Invoke(document);
            var schemaJson = document.ToJson();

            return(schemaJson);
        }
Ejemplo n.º 4
0
        private static async Task <OpenApiDocument> GenerateSwaggerDocument()
        {
            var generator   = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings());
            var assembly    = Assembly.GetAssembly(typeof(Web.Program));
            var controllers = WebApiOpenApiDocumentGenerator.GetControllerClasses(assembly);

            return(await generator.GenerateForControllersAsync(controllers));
        }
Ejemplo n.º 5
0
        public async Task When_operations_have_different_tags_they_are_grouped_into_different_clients()
        {
            // Arrange
            var generator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings());
            var document  = await generator.GenerateForControllersAsync(new List <Type>() { typeof(PointControllerA), typeof(PointControllerB) });

            var codeGenerator = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
            {
                OperationNameGenerator = new MultipleClientsFromFirstTagAndPathSegmentsOperationNameGenerator()
            });

            // Act
            var code = codeGenerator.GenerateFile();

            // Assert
            Assert.IsTrue(code.Contains("export class PointControllerAClient"));
            Assert.IsTrue(code.Contains("export class PointControllerBClient"));
        }
Ejemplo n.º 6
0
        public async Task When_there_are_duplicate_paths_through_inheritance_then_the_base_method_is_ignored()
        {
            //// Arrange
            var generator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings());

            //// Act
            var document = await generator.GenerateForControllersAsync(new Type[] { typeof(StandardController), typeof(SpecificController) });

            var json = document.ToJson();

            //// Assert
            Assert.AreEqual(4, document.Operations.Count());

            Assert.IsTrue(document.Operations.Any(o => o.Path == "/api/common/standard/export"));
            Assert.IsTrue(document.Operations.Any(o => o.Path == "/api/common/standard/foo"));

            Assert.IsTrue(document.Operations.Any(o => o.Path == "/api/whatever/specific/export"));
            Assert.IsTrue(document.Operations.Any(o => o.Path == "/api/whatever/specific/foo"));
        }
Ejemplo n.º 7
0
        /// <summary>Generates the Swagger specification.</summary>
        /// <param name="context">The context.</param>
        /// <returns>The Swagger specification.</returns>
        protected virtual async Task <string> GenerateDocumentAsync(IOwinContext context)
        {
            var settings  = _settings.CreateGeneratorSettings(null, null);
            var generator = new WebApiOpenApiDocumentGenerator(settings);
            var document  = await generator.GenerateForControllersAsync(_controllerTypes);

            if (_settings.MiddlewareBasePath != null)
            {
                document.Host = context.Request.Host.Value ?? "";
                document.Schemes.Add(context.Request.Scheme == "http" ? OpenApiSchema.Http : OpenApiSchema.Https);
                document.BasePath = context.Request.PathBase.Value?.Substring(0, context.Request.PathBase.Value.Length - (_settings.MiddlewareBasePath?.Length ?? 0)) ?? "";
            }

            // iiQ Custom
            //else
            //{
            //    document.Servers.Clear();
            //    document.Servers.Add(new OpenApiServer
            //    {
            //        Url = context.Request.GetServerUrl()
            //    });
            //}

            // iiQ Custom
            var updated = new Dictionary <string, OpenApiPathItem>();

            foreach (var kv in document.Paths)
            {
                updated.Add(kv.Key.Replace("/v1.0", ""), kv.Value);
            }
            document.Paths.Clear();
            foreach (var kv in updated)
            {
                document.Paths.Add(kv);
            }

            _settings.PostProcess?.Invoke(document);
            var schemaJson = document.ToJson();

            return(schemaJson);
        }
        public async Task When_no_IncludedVersions_are_defined_then_all_routes_are_available_and_replaced()
        {
            // Arrange
            var settings  = new WebApiOpenApiDocumentGeneratorSettings();
            var generator = new WebApiOpenApiDocumentGenerator(settings);

            // Act
            var document = await generator.GenerateForControllersAsync(new List <Type>
            {
                typeof(VersionedControllerV1),
                typeof(VersionedControllerV2)
            });

            // Assert
            Assert.AreEqual(4, document.Paths.Count);

            Assert.IsTrue(document.Paths.ContainsKey("/api/v1/foo"));
            Assert.IsTrue(document.Paths.ContainsKey("/api/v1/bar"));

            Assert.IsTrue(document.Paths.ContainsKey("/api/v2/foo"));
            Assert.IsTrue(document.Paths.ContainsKey("/api/v2/bar"));
        }
        public async Task When_IncludedVersions_are_set_then_only_these_are_available_in_document()
        {
            // Arrange
            var settings = new WebApiOpenApiDocumentGeneratorSettings();

            settings.OperationProcessors.TryGet <ApiVersionProcessor>().IncludedVersions = new[] { "1" };

            var generator = new WebApiOpenApiDocumentGenerator(settings);

            // Act
            var document = await generator.GenerateForControllersAsync(new List <Type>
            {
                typeof(VersionedControllerV1),
                typeof(VersionedControllerV2)
            });

            var json = document.ToJson();

            // Assert
            Assert.AreEqual(2, document.Paths.Count);

            Assert.IsTrue(document.Paths.ContainsKey("/api/v1/foo"));
            Assert.IsTrue(document.Paths.ContainsKey("/api/v1/bar"));
        }