コード例 #1
0
        public void SetsRequestExamples_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.AnnotatedWithSwaggerRequestExampleAttribute));

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

            // Assert
            var actualExample   = JsonConvert.DeserializeObject <PersonRequest>(((OpenApiRawString)requestBody.Content["application/json"].Example).Value);
            var expectedExample = new PersonRequestExample().GetExamples();

            actualExample.ShouldMatch(expectedExample);

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

            actualSchemaExample.ShouldMatch(expectedExample);
        }
コード例 #2
0
        public void ThenWillFallbackToSystemTextJson()
        {
            var value       = new PersonRequestExample().GetExamples();
            var contentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");

            sut.Serialize(value, contentType)
            .ShouldBe("{\"Title\":3,\"Age\":24,\"FirstName\":\"Dave\",\"Income\":null,\"Children\":null,\"Job\":null}");
        }