public async Task SwaggerOperationProcessorAttributesOnActions_AreDiscoveredAndExecuted()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = GetApiDescriptionGroups(typeof(ActionWithSwaggerOperationProcessor));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            Assert.Equal("Hello from action", document.Info.Title);
        }
        public async Task ControllersWithSwaggerIgnoreAttribute_AreIgnored()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = GetApiDescriptionGroups(typeof(ControllerWithSwaggerIgnoreAttribute));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            Assert.Empty(document.Operations);
        }
        public async Task SwaggerOperationAttribute_AreUsedToCalculateOperationId_IfPresent()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = GetApiDescriptionGroups(typeof(ActionWithSwaggerOperationAttribute));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            var operation = Assert.Single(document.Operations);

            Assert.Equal("CustomOperationId", operation.Operation.OperationId);
        }
Example #4
0
        protected async Task <OpenApiDocument> GenerateDocumentAsync(AspNetCoreOpenApiDocumentGeneratorSettings settings, params Type[] controllerTypes)
        {
            var generator = new AspNetCoreOpenApiDocumentGenerator(settings);
            var provider  = TestServer.Host.Services.GetRequiredService <IApiDescriptionGroupCollectionProvider>();

            var controllerTypeNames = controllerTypes.Select(t => t.FullName);
            var groups = new ApiDescriptionGroupCollection(provider.ApiDescriptionGroups.Items
                                                           .Select(i => new ApiDescriptionGroup(i.GroupName, i.Items.Where(u => controllerTypeNames.Contains(((ControllerActionDescriptor)u.ActionDescriptor).ControllerTypeInfo.FullName)).ToList())).ToList(),
                                                           provider.ApiDescriptionGroups.Version);

            var document = await generator.GenerateAsync(groups);

            return(document);
        }
        public async Task ActionsWithSwaggerIgnoreAttribute_AreIgnored()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = GetApiDescriptionGroups(typeof(ActionWithSwaggerIgnoreAttribute));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            var operationDescription = Assert.Single(document.Operations);

            Assert.Equal("/test1", operationDescription.Path);
            Assert.Equal(OpenApiOperationMethod.Get, operationDescription.Method);
        }
        public async Task FormFileParametersAreDiscovered()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = GetApiDescriptionGroups(typeof(ControllerWithParameters));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            var operation = Assert.Single(document.Operations, o => o.Path == "/" + nameof(ControllerWithParameters.FileParameter)).Operation;
            var parameter = Assert.Single(operation.Parameters);

            Assert.Equal(OpenApiParameterKind.FormData, parameter.Kind);
            Assert.Equal("multipart/form-data", Assert.Single(operation.Consumes));
        }
        public async Task FromHeaderParametersAreDiscovered()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = GetApiDescriptionGroups(typeof(ControllerWithParameters));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            var operation = Assert.Single(document.Operations, o => o.Path == "/" + nameof(ControllerWithParameters.FromHeaderParameter)).Operation;
            var parameter = Assert.Single(operation.Parameters);

            Assert.Equal(OpenApiParameterKind.Header, parameter.Kind);
            Assert.Equal("headerParameter", parameter.Name);
        }
        public async Task When_no_IncludedVersions_are_defined_then_all_routes_are_available_and_replaced()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = GetApiDescriptionGroups(typeof(ControllerWithReCodeAttribute));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            var operation = Assert.Single(document.Operations).Operation;

            Assert.True(operation.ExtensionData.ContainsKey("x-code-samples"));
            var extenstionData = (IList <ReDocCodeSampleAttribute.ReDocCodeSample>)operation.ExtensionData["x-code-samples"];

            Assert.Equal(2, extenstionData.Count);
        }
        public async Task SwaggerResponseAttributesOnActionsAreDiscovered()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = GetApiDescriptionGroups(typeof(ActionWithSwaggerResponseAttribute));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            var operation = Assert.Single(document.Operations);

            Assert.Single(operation.Operation.Responses);
            var response   = operation.Operation.Responses["201"];
            var definition = document.Definitions.First(f => f.Value == response.Schema?.ActualSchema);

            Assert.Equal(nameof(TestModel), definition.Key);
        }
        public async Task FromFormParametersAreDiscovered()
        {
            //// Arrange
            var generator = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings {
                RequireParametersWithoutDefault = true
            });
            var apiDescriptions = GetApiDescriptionGroups(typeof(ControllerWithParameters));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            var operation = Assert.Single(document.Operations, o => o.Path == "/" + nameof(ControllerWithParameters.FromFormParameter)).Operation;
            var parameter = Assert.Single(operation.Parameters);

            Assert.Equal(OpenApiParameterKind.FormData, parameter.Kind);
            Assert.Equal("formParameter", parameter.Name);
            Assert.True(parameter.IsRequired);
        }
        public async Task ParametersWithSwaggerIgnoreAttribute_AreIgnored()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = GetApiDescriptionGroups(typeof(ParameterWithSwaggerIgnoreAttribute));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            var operationDescription = Assert.Single(document.Operations);

            Assert.Equal("/{id}", operationDescription.Path);
            Assert.Equal(OpenApiOperationMethod.Post, operationDescription.Method);

            var parameter = Assert.Single(operationDescription.Operation.Parameters);

            Assert.Equal("id", parameter.Name);
            Assert.Equal(OpenApiParameterKind.Path, parameter.Kind);
        }
Example #12
0
        public async Task WhenSystemTextOptionsIsUsed_ThenOptionsAreConverted()
        {
            // Arrange
            var services = new ServiceCollection()
                           .AddLogging();

            services.AddControllers()
            .AddJsonOptions(opt => opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));

            services.AddOpenApiDocument();
            var serviceProvider = services.BuildServiceProvider();

            // Act
            var registration = serviceProvider.GetRequiredService <OpenApiDocumentRegistration>();
            var generator    = new AspNetCoreOpenApiDocumentGenerator(registration.Settings);
            await generator.GenerateAsync(serviceProvider);

            // Assert
            Assert.Contains(registration.Settings.SerializerSettings.Converters, c => c is StringEnumConverter);
        }
        public async Task When_generating_swagger_all_apidescriptions_are_discovered_for_2_1_applications()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = Get2_1_ApiDescriptionGroups(typeof(TestController));

            //// Act
            var document = await generator.GenerateAsync(apiDescriptions);

            //// Assert
            var operationDescription = Assert.Single(document.Operations);

            Assert.Equal("/test", operationDescription.Path);
            Assert.Equal(OpenApiOperationMethod.Get, operationDescription.Method);

            var operation = operationDescription.Operation;

            Assert.Equal("Test_FindModel", operation.OperationId);

            var parameter = Assert.Single(operation.Parameters);

            Assert.Equal("id", parameter.Name);
            Assert.Equal(OpenApiParameterKind.Path, parameter.Kind);
            Assert.True(parameter.IsRequired);
            Assert.Equal(NJsonSchema.JsonObjectType.Integer, parameter.Type);

            Assert.Equal(2, operation.Responses.Count);
            var response   = operation.Responses["200"];
            var definition = document.Definitions.First(f => f.Value == response.Schema?.ActualSchema);

            Assert.Equal(nameof(TestModel), definition.Key);

            response   = operation.Responses["default"];
            definition = document.Definitions.First(f => f.Value == response.Schema?.ActualSchema);
            Assert.Equal(nameof(ProblemDetails), definition.Key);
        }
        public async Task SwaggerDocumentIsGeneratedForCustomCreatedApiDescriptions()
        {
            //// Arrange
            var generator       = new AspNetCoreOpenApiDocumentGenerator(new AspNetCoreOpenApiDocumentGeneratorSettings());
            var apiDescriptions = new[]
            {
                new ApiDescription
                {
                    ActionDescriptor = new ActionDescriptor(),
                },
                new ApiDescription
                {
                    ActionDescriptor = new ControllerActionDescriptor
                    {
                        ControllerTypeInfo = typeof(CustomController).GetTypeInfo(),
                        MethodInfo         = typeof(CustomController).GetMethod(nameof(CustomController.FindModel)),
                        AttributeRouteInfo = new AttributeRouteInfo
                        {
                            Template = "api/test/{id}",
                        },
                        ControllerName = "Test",
                        ActionName     = "Find",
                    },
                    HttpMethod            = "Get",
                    RelativePath          = "api/test/{id}",
                    ParameterDescriptions =
                    {
                        new ApiParameterDescription
                        {
                            Name   = "id",
                            Source = BindingSource.Path,
                            Type   = typeof(int),
                        },
                    },
                    SupportedResponseTypes =
                    {
                        new ApiResponseType
                        {
                            Type       = typeof(TestModel),
                            StatusCode = 200,
                        }
                    },
                },
                new ApiDescription
                {
                    ActionDescriptor = new ControllerActionDescriptor
                    {
                        ControllerTypeInfo = typeof(CustomController).GetTypeInfo(),
                        MethodInfo         = typeof(CustomController).GetMethod(nameof(CustomController.Delete)),
                        ControllerName     = "Test",
                        ActionName         = "DeleteModel",
                    },
                    HttpMethod            = "Delete",
                    RelativePath          = "api/test",
                    ParameterDescriptions =
                    {
                        new ApiParameterDescription
                        {
                            Name   = "id",
                            Source = BindingSource.Query,
                            Type   = typeof(int),
                        },
                    },
                    SupportedResponseTypes =
                    {
                        new ApiResponseType
                        {
                            Type       = typeof(StatusCodeResult),
                            StatusCode = 201,
                        }
                    },
                },
                new ApiDescription
                {
                    ActionDescriptor = new ControllerActionDescriptor
                    {
                        ControllerTypeInfo = typeof(CustomController).GetTypeInfo(),
                        MethodInfo         = typeof(CustomController).GetMethod(nameof(CustomController.Update)),
                        AttributeRouteInfo = new AttributeRouteInfo
                        {
                            Template = "api/test/{id}",
                        },
                        ControllerName = "Test",
                        ActionName     = "Update",
                    },
                    HttpMethod            = "Put",
                    RelativePath          = "api/test/{id}",
                    ParameterDescriptions =
                    {
                        new ApiParameterDescription
                        {
                            Type   = typeof(int),
                            Name   = "id",
                            Source = BindingSource.Path,
                        },
                        new ApiParameterDescription
                        {
                            Type   = typeof(TestModel),
                            Name   = "model",
                            Source = BindingSource.Body,
                        },
                    },
                    SupportedResponseTypes =
                    {
                        new ApiResponseType
                        {
                            Type       = typeof(Task <TestModel>),
                            StatusCode = 200,
                        }
                    },
                },
            };
            var apiDescriptionGroupCollection = new ApiDescriptionGroupCollection(
                new[] { new ApiDescriptionGroup(string.Empty, apiDescriptions) },
                version: 1);

            //// Act
            var document = await generator.GenerateAsync(apiDescriptionGroupCollection);

            //// Assert
            Assert.Collection(
                document.Operations.OrderBy(o => o.Method.ToString()),
                operation =>
            {
                Assert.Equal("/api/test", operation.Path);
                Assert.Equal(OpenApiOperationMethod.Delete, operation.Method);
            },
                operation =>
            {
                Assert.Equal("/api/test/{id}", operation.Path);
                Assert.Equal(OpenApiOperationMethod.Get, operation.Method);

                Assert.Single(operation.Operation.Responses);
                var response   = operation.Operation.Responses["200"];
                var definition = document.Definitions.First(f => f.Value == response.Schema?.ActualSchema);
                Assert.Equal(nameof(TestModel), definition.Key);
            },
                operation =>
            {
                Assert.Equal("/api/test/{id}", operation.Path);
                Assert.Equal(OpenApiOperationMethod.Put, operation.Method);
            });
        }