コード例 #1
0
        public void Apply_WhenRequestIsANullableEnum_ShouldNotThrowException()
        {
            // Arrange
            serviceProvider.GetService(typeof(IExamplesProvider <Title?>)).Returns(new TitleExample());
            var titleParameter = new BodyParameter {
                In = "body", Schema = new Schema {
                    Ref = "#/definitions/Title"
                }
            };
            var operation = new Operation {
                OperationId = "foobar", Parameters = new[] { titleParameter }
            };
            var parameterDescriptions = new List <ApiParameterDescription>()
            {
                new ApiParameterDescription {
                    Type = typeof(Title?)
                }
            };
            var filterContext = FilterContextFor(typeof(FakeActions), nameof(FakeActions.RequestTakesANullableEnum), parameterDescriptions);

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

            // Assert
            var actualParameterExample = Enum.Parse(typeof(Title), titleParameter.Schema.Example.ToString());
            var expectedExample        = new TitleExample().GetExamples().Value;

            actualParameterExample.ShouldBe(expectedExample);
        }
        public void WhenRequestIsANullableEnum_ShouldNotThrowException()
        {
            // Arrange
            serviceProvider.GetService(typeof(IExamplesProvider <Title?>)).Returns(new TitleExample());
            var requestBody = new OpenApiRequestBody
            {
                Content = new Dictionary <string, OpenApiMediaType>
                {
                    { "application/json", new OpenApiMediaType()
                      {
                          Schema = new OpenApiSchema {
                              Reference = new OpenApiReference {
                                  Id = "definitions/Title"
                              }
                          }
                      } }
                }
            };
            var operation = new OpenApiOperation {
                OperationId = "foobar", RequestBody = requestBody
            };

            var parameterDescriptions = new List <ApiParameterDescription>()
            {
                new ApiParameterDescription {
                    Type = typeof(Title?)
                }
            };
            var filterContext = FilterContextFor(typeof(FakeActions), nameof(FakeActions.RequestTakesANullableEnum), parameterDescriptions);

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

            // Assert
            var actualParameterExample = Enum.Parse(typeof(Title), ((OpenApiRawString)requestBody.Content["application/json"].Example).Value);
            var expectedExample        = new TitleExample().GetExamples().Value;

            actualParameterExample.ShouldBe(expectedExample);

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

            actualSchemaExample.ShouldBe(expectedExample);
        }