Example #1
0
        public void SetsMultipleRequestExamples_FromMethodAttributes()
        {
            // Arrange
            var requestBody = new OpenApiRequestBody
            {
                Content = new Dictionary <string, OpenApiMediaType>
                {
                    { "application/json", new OpenApiMediaType() }
                }
            };
            var operation = new OpenApiOperation {
                OperationId = "foobar", RequestBody = requestBody
            };
            var filterContext = FilterContextFor(typeof(FakeActions), nameof(FakeActions.AnnotatedWithSwaggerRequestMultipleExamplesAttribute));

            // Act
            sut.Apply(operation, filterContext);

            // Assert
            var actualExamples   = requestBody.Content["application/json"].Examples;
            var expectedExamples = new PersonRequestMultipleExamples().GetExamples();

            actualExamples.ShouldAllMatch(expectedExamples, ExampleAssertExtensions.ShouldMatch);

            // Assert SerializeAsV2
            var actualSchemaExample = JsonConvert.DeserializeObject <PersonRequest>(((OpenApiRawString)filterContext.SchemaRepository.Schemas["PersonRequest"].Example).Value);

            actualSchemaExample.ShouldMatch(expectedExamples.First().Value);
        }
Example #2
0
        public void SetsMultipleRequestExamples()
        {
            // Arrange
            serviceProvider.GetService(typeof(IMultipleExamplesProvider <PersonRequest>)).Returns(new PersonRequestMultipleExamples());
            var requestBody = new OpenApiRequestBody
            {
                Content = new Dictionary <string, OpenApiMediaType>
                {
                    { "application/json", new OpenApiMediaType() }
                }
            };
            var operation = new OpenApiOperation {
                OperationId = "foobar", RequestBody = requestBody
            };
            var parameterDescriptions = new List <ApiParameterDescription>()
            {
                new ApiParameterDescription {
                    Type = typeof(PersonRequest)
                }
            };
            var filterContext = FilterContextFor(typeof(FakeActions), nameof(FakeActions.PersonRequestUnannotated), parameterDescriptions);

            // Act
            sut.Apply(operation, filterContext);

            // Assert
            var actualExamples   = requestBody.Content["application/json"].Examples;
            var expectedExamples = new PersonRequestMultipleExamples().GetExamples();

            actualExamples.ShouldAllMatch(expectedExamples, ExampleAssertExtensions.ShouldMatch);
            actualExamples["Dave"].Description.ShouldBe("Here's a description");
            actualExamples["Angela"].Description.ShouldBeNull();

            // Assert SerializeAsV2
            var actualSchemaExample = JsonConvert.DeserializeObject <PersonRequest>(((OpenApiString)filterContext.SchemaRepository.Schemas["PersonRequest"].Example).Value);

            actualSchemaExample.ShouldMatch(expectedExamples.First().Value);
        }