public void Apply_SetsDescriptionAndExample_FromPropertySummaryAndExampleTags()
        {
            var requestBody = new OpenApiRequestBody
            {
                Content = new Dictionary <string, OpenApiMediaType>
                {
                    ["application/json"] = new OpenApiMediaType {
                        Schema = new OpenApiSchema {
                            Type = "string"
                        }
                    }
                }
            };
            var bodyParameterDescription = new ApiParameterDescription
            {
                ModelMetadata = ModelMetadataFactory.CreateForProperty(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringProperty))
            };
            var filterContext = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

            Subject().Apply(requestBody, filterContext);

            Assert.Equal("Summary for StringProperty", requestBody.Description);
            Assert.NotNull(requestBody.Content["application/json"].Example);
            Assert.Equal("\"Example for StringProperty\"", requestBody.Content["application/json"].Example.ToJson());
        }
        public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
        {
            var bodyParameterDescription = context.BodyParameterDescription;

            if (bodyParameterDescription == null)
            {
                return;
            }

            var propertyInfo = bodyParameterDescription.PropertyInfo();

            if (propertyInfo != null)
            {
                ApplyPropertyAnnotations(requestBody, propertyInfo);
                return;
            }

            var parameterInfo = bodyParameterDescription.ParameterInfo();

            if (parameterInfo != null)
            {
                ApplyParamAnnotations(requestBody, parameterInfo);
                return;
            }
        }
Example #3
0
 public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
 {
     requestBody.Extensions["x-name"] = new RequestBodyNameExtension
     {
         Name = context.BodyParameterDescription.Name,
     };
 }
        public void Apply_SetsDescriptionAndExample_FromUnderlyingGenericTypeActionParamTag()
        {
            var requestBody = new OpenApiRequestBody
            {
                Content = new Dictionary <string, OpenApiMediaType>
                {
                    ["application/json"] = new OpenApiMediaType {
                        Schema = new OpenApiSchema {
                            Type = "string"
                        }
                    }
                }
            };
            var parameterInfo = typeof(FakeConstructedControllerWithXmlComments)
                                .GetMethod(nameof(FakeConstructedControllerWithXmlComments.ActionWithParamTags))
                                .GetParameters()[0];
            var bodyParameterDescription = new ApiParameterDescription
            {
                ParameterDescriptor = new ControllerParameterDescriptor {
                    ParameterInfo = parameterInfo
                }
            };
            var filterContext = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

            Subject().Apply(requestBody, filterContext);

            Assert.Equal("Description for param1", requestBody.Description);
            Assert.NotNull(requestBody.Content["application/json"].Example);
            Assert.Equal("\"Example for param1\"", requestBody.Content["application/json"].Example.ToJson());
        }
        public void Apply_SetsDescription_FromPropertySummaryTag()
        {
            var requestBody = new OpenApiRequestBody();
            var bodyParameterDescription = new ApiParameterDescription
            {
                ModelMetadata = ModelMetadataFactory.CreateForProperty(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringProperty))
            };
            var filterContext = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

            Subject().Apply(requestBody, filterContext);

            Assert.Equal("Summary for StringProperty", requestBody.Description);
        }
Example #6
0
        public void Apply_EnrichesParameterMetadata_IfPropertyDecoratedWithSwaggerRequestBodyAttribute()
        {
            var requestBody = new OpenApiRequestBody();
            var bodyParameterDescription = new ApiParameterDescription
            {
                ModelMetadata = ModelMetadataFactory.CreateForProperty(typeof(SwaggerAnnotatedType), nameof(SwaggerAnnotatedType.StringWithSwaggerRequestBodyAttribute))
            };
            var context = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

            Subject().Apply(requestBody, context);

            Assert.Equal("Description for StringWithSwaggerRequestBodyAttribute", requestBody.Description);
            Assert.True(requestBody.Required);
        }
        public void Apply_SetsDescription_FromUnderlyingGenericTypeActionParamTag()
        {
            var requestbody   = new OpenApiRequestBody();
            var parameterInfo = typeof(ConstructedControllerWithXmlComments)
                                .GetMethod(nameof(ConstructedControllerWithXmlComments.ActionWithGenericTypeParameter))
                                .GetParameters()[0];
            var bodyParameterDescription = new ApiParameterDescription
            {
                ParameterDescriptor = new ControllerParameterDescriptor {
                    ParameterInfo = parameterInfo
                }
            };
            var filterContext = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

            Subject().Apply(requestbody, filterContext);

            Assert.Equal("Description for param", requestbody.Description);
        }
Example #8
0
        public void Apply_EnrichesRequestBodyMetadata_IfParameterDecoratedWithSwaggerRequestBodyAttribute()
        {
            var requestBody   = new OpenApiRequestBody();
            var parameterInfo = typeof(FakeControllerWithSwaggerAnnotations)
                                .GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerRequestBodyAttribute))
                                .GetParameters()[0];
            var bodyParameterDescription = new ApiParameterDescription
            {
                ParameterDescriptor = new ControllerParameterDescriptor {
                    ParameterInfo = parameterInfo
                }
            };
            var context = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

            Subject().Apply(requestBody, context);

            Assert.Equal("Description for param", requestBody.Description);
            Assert.True(requestBody.Required);
        }
        public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
        {
            // Even if it's v2 or request body shouldn't be named
            // we want name it because of bugs and different approaches in generation tools
            var bodyName = _swaggerOptions.Schema.NameAndOrderRequestBody
                ? context.BodyParameterDescription.Name
                : "body";

            requestBody.Extensions.Add("x-name", new OpenApiString(bodyName));
            requestBody.Extensions.Add("x-codegen-request-body-name", new OpenApiString(bodyName));
            requestBody.Extensions.Add("x-ms-requestBody-name", new OpenApiString(bodyName));

            if (_swaggerOptions.Schema.NameAndOrderRequestBody)
            {
                var bodyPosition = context.BodyParameterDescription.ParameterInfo().Position;
                requestBody.Extensions.Add("x-position", new OpenApiInteger(bodyPosition));
                requestBody.Extensions.Add("x-ms-requestBody-index", new OpenApiInteger(bodyPosition));
            }
        }
Example #10
0
        public void Apply_DoesNotModifyTheRequiredFlag_IfNotSpecifiedWithSwaggerParameterAttribute()
        {
            var requestBody = new OpenApiRequestBody {
                Required = true
            };
            var parameterInfo = typeof(FakeControllerWithSwaggerAnnotations)
                                .GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerRequestbodyAttributeDescriptionOnly))
                                .GetParameters()[0];
            var bodyParameterDescription = new ApiParameterDescription
            {
                ParameterDescriptor = new ControllerParameterDescriptor {
                    ParameterInfo = parameterInfo
                }
            };
            var context = new RequestBodyFilterContext(bodyParameterDescription, null, null, null);

            Subject().Apply(requestBody, context);

            Assert.True(requestBody.Required);
        }
        public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
        {
            var bodyParameterDescription = context.BodyParameterDescription;

            if (bodyParameterDescription == null)
            {
                return;
            }

            var properties = bodyParameterDescription.Type.GetProperties();

            foreach (var propertyInfo in properties)
            {
                ApplyPropertyTags(requestBody, propertyInfo);
                return;
            }
            // var parameterInfo = bodyParameterDescription.ParameterInfo();
            // if (parameterInfo != null)
            // {
            //     ApplyParamTags(requestBody, parameterInfo);
            //     return;
            // }
        }
Example #12
0
 /// <inheritdoc/>
 public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
 {
     requestBody.Description = requestBody.Description.SingleSpacesNoLineBreak();
 }
 public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
 {
     requestBody.Extensions.Add("X-foo", new OpenApiString("bar"));
     requestBody.Extensions.Add("X-docName", new OpenApiString(context.DocumentName));
 }
 public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
 {
     requestBody.Extensions.Add("X-foo", new OpenApiString("bar"));
 }
 public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
 {
     requestBody.Required = true;
 }
 public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
 {
     requestBody.Extensions.Add("x-purpose", new OpenApiString("test"));
 }