public void Given_OpenApiResponseWithBodyAttribute_With_Example_When_ToOpenApiMediaType_Invoked_Then_It_Should_Return_Result(Type example, int count)
        {
            var statusCode  = HttpStatusCode.OK;
            var contentType = "application/json";
            var bodyType    = typeof(object);
            var attribute   = new OpenApiResponseWithBodyAttribute(statusCode, contentType, bodyType)
            {
                Example = example,
            };
            var namingStrategy = new CamelCaseNamingStrategy();

            var result = OpenApiPayloadAttributeExtensions.ToOpenApiMediaType(attribute, namingStrategy);

            result.Examples.Should().NotBeNull();
            result.Examples.Should().HaveCount(count);

            if (count == 0)
            {
                return;
            }

            var instance = (dynamic)Activator.CreateInstance(example);
            var examples = (IDictionary <string, OpenApiExample>)instance.Build(namingStrategy).Examples;
            var first    = examples.First().Value;

            (result.Example as OpenApiString).Value.Should().Be((first.Value as OpenApiString).Value);
        }
        public void Given_OpenApiResponseWithBodyAttribute_When_ToOpenApiMediaType_Invoked_Then_It_Should_Return_Result(Type bodyType, string expected, bool items, bool additionalProperties, string underlyingType)
        {
            var statusCode     = HttpStatusCode.OK;
            var contentType    = "application/json";
            var attribute      = new OpenApiResponseWithBodyAttribute(statusCode, contentType, bodyType);
            var namingStrategy = new CamelCaseNamingStrategy();

            var result = OpenApiPayloadAttributeExtensions.ToOpenApiMediaType(attribute, namingStrategy);

            result.Schema.Type.Should().Be(expected);
            result.Schema.Deprecated.Should().BeFalse();
            if (items)
            {
                result.Schema.Items.Should().NotBeNull();
                result.Schema.Items.Type.Should().Be(underlyingType);
            }
            else
            {
                result.Schema.Items.Should().BeNull();
            }

            if (additionalProperties)
            {
                result.Schema.AdditionalProperties.Should().NotBeNull();
                result.Schema.AdditionalProperties.Type.Should().Be(underlyingType);
            }
            else
            {
                result.Schema.AdditionalProperties.Should().BeNull();
            }
        }
        /// <summary>
        /// Converts <see cref="OpenApiResponseWithBodyAttribute"/> to <see cref="OpenApiResponse"/>.
        /// </summary>
        /// <param name="attribute"><see cref="OpenApiResponseWithBodyAttribute"/> instance.</param>
        /// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
        /// <returns><see cref="OpenApiResponse"/> instance.</returns>
        public static OpenApiResponse ToOpenApiResponse(this OpenApiResponseWithBodyAttribute attribute, NamingStrategy namingStrategy = null)
        {
            attribute.ThrowIfNullOrDefault();

            var description = string.IsNullOrWhiteSpace(attribute.Description)
                                  ? $"Payload of {attribute.BodyType.GetOpenApiDescription()}"
                                  : attribute.Description;
            var mediaType = attribute.ToOpenApiMediaType <OpenApiResponseWithBodyAttribute>(namingStrategy);
            var content   = new Dictionary <string, OpenApiMediaType>()
            {
                { attribute.ContentType, mediaType }
            };
            var response = new OpenApiResponse()
            {
                Description = description,
                Content     = content
            };

            if (!string.IsNullOrWhiteSpace(attribute.Summary))
            {
                var summary = new OpenApiString(attribute.Summary);

                response.Extensions.Add("x-ms-summary", summary);
            }

            return(response);
        }
        /// <summary>
        /// Converts <see cref="OpenApiResponseWithBodyAttribute"/> to <see cref="OpenApiResponse"/>.
        /// </summary>
        /// <param name="attribute"><see cref="OpenApiResponseWithBodyAttribute"/> instance.</param>
        /// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
        /// <param name="collection"><see cref="VisitorCollection"/> instance.</param>
        /// <param name="version">OpenAPI spec version.</param>
        /// <returns><see cref="OpenApiResponse"/> instance.</returns>
        public static OpenApiResponse ToOpenApiResponse(this OpenApiResponseWithBodyAttribute attribute, NamingStrategy namingStrategy = null, VisitorCollection collection = null, OpenApiVersionType version = OpenApiVersionType.V2)
        {
            attribute.ThrowIfNullOrDefault();

            var description = string.IsNullOrWhiteSpace(attribute.Description)
                                  ? $"Payload of {attribute.BodyType.GetOpenApiDescription()}"
                                  : attribute.Description;
            var mediaType = attribute.ToOpenApiMediaType <OpenApiResponseWithBodyAttribute>(namingStrategy, collection, version);
            var content   = new Dictionary <string, OpenApiMediaType>()
            {
                { attribute.ContentType, mediaType }
            };
            var response = new OpenApiResponse()
            {
                Description = description,
                Content     = content,
            };

            if (attribute.CustomHeaderType.HasInterface <IOpenApiCustomResponseHeader>())
            {
                var header = Activator.CreateInstance(attribute.CustomHeaderType) as IOpenApiCustomResponseHeader;

                response.Headers = header.Headers;
            }

            if (!string.IsNullOrWhiteSpace(attribute.Summary))
            {
                var summary = new OpenApiString(attribute.Summary);

                response.Extensions.Add("x-ms-summary", summary);
            }

            return(response);
        }
コード例 #5
0
        public void Given_Parameters_When_Instantiated_Then_It_Should_Return_Value(HttpStatusCode statusCode, string contentType, Type bodyType)
        {
            var attribute = new OpenApiResponseWithBodyAttribute(statusCode, contentType, bodyType);

            attribute.StatusCode.Should().Be(statusCode);
            attribute.ContentType.Should().BeEquivalentTo(contentType);
            attribute.BodyType.Should().Be(bodyType);
        }
        public void Given_Value_Property_Should_Return_Value()
        {
            var statusCode  = HttpStatusCode.OK;
            var contentType = "Hello World";
            var bodyType    = typeof(object);
            var attribute   = new OpenApiResponseWithBodyAttribute(statusCode, contentType, bodyType);

            attribute.StatusCode.Should().Be(statusCode);
            attribute.ContentType.Should().BeEquivalentTo(contentType);
            attribute.BodyType.Should().Be(bodyType);
            attribute.Summary.Should().BeNullOrWhiteSpace();
            attribute.Description.Should().BeNullOrWhiteSpace();
        }
        public void Given_OpenApiResponseWithBodyAttribute_With_Deprecated_When_ToOpenApiMediaType_Invoked_Then_It_Should_Return_Result(bool deprecated)
        {
            var statusCode  = HttpStatusCode.OK;
            var contentType = "application/json";
            var bodyType    = typeof(object);
            var attribute   = new OpenApiResponseWithBodyAttribute(statusCode, contentType, bodyType)
            {
                Deprecated = deprecated,
            };
            var namingStrategy = new CamelCaseNamingStrategy();

            var result = OpenApiPayloadAttributeExtensions.ToOpenApiMediaType(attribute, namingStrategy);

            result.Schema.Deprecated.Should().Be(deprecated);
        }
コード例 #8
0
        public void Given_Properties_When_Instantiated_Then_It_Should_Return_Value(string summary, string description, bool deprecated, Type headerType)
        {
            var statusCode  = HttpStatusCode.OK;
            var contentType = "application/json";
            var bodyType    = typeof(object);
            var attribute   = new OpenApiResponseWithBodyAttribute(statusCode, contentType, bodyType)
            {
                Summary     = summary,
                Description = description,
                Deprecated  = deprecated,
                HeaderType  = headerType,
            };

            attribute.Summary.Should().Be(summary);
            attribute.Description.Should().Be(description);
            attribute.Deprecated.Should().Be(deprecated);
            attribute.HeaderType.Should().Be(headerType);
        }
コード例 #9
0
        public void Given_Properties_When_ToOpenApiResponse_Invoked_Then_It_Should_Return_Value(string summary, string description, Type headerType)
        {
            var statusCode  = HttpStatusCode.OK;
            var contentType = "application/json";
            var bodyType    = typeof(object);
            var attribute   = new OpenApiResponseWithBodyAttribute(statusCode, contentType, bodyType)
            {
                Summary     = summary,
                Description = description,
                HeaderType  = headerType,
            };

            var result = OpenApiResponseWithBodyAttributeExtensions.ToOpenApiResponse(attribute);

            result.Description.Should().Be(description);
            result.Content.Should().ContainKey(contentType);
            result.Content[contentType].Schema.Type.Should().Be(bodyType.Name.ToLowerInvariant());
            result.Extensions.Should().ContainKey("x-ms-summary");
            (result.Extensions["x-ms-summary"] as OpenApiString).Value.Should().Be(summary);
            result.Headers.Should().ContainKey("x-fake-header");
        }