RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.74k stars 1.29k forks source link

formData values sent with content type json #1478

Open fubar-coder opened 6 years ago

fubar-coder commented 6 years ago

I have an API function that accepts two parameters from a content of type application/x-www-form-urlencoded. NSwag correctly recognizes it as formData, but it sets the content type to application/json-patch+json while still sending the properties as form data.

I tried to set acceptable content type with Consumes("application/x-www-form-urlencoded"), but it seems that it gets ignored by NSwag.

startewho commented 6 years ago

I have the same problem in the latest version.

startewho commented 6 years ago

I solve the problem by use custom optionprocess.

public class ConsumesSwaggerOperationProcessorAttribute : SwaggerOperationProcessorAttribute
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="comsues"></param>

        public ConsumesSwaggerOperationProcessorAttribute(params string[] comsues)
                    : base(typeof(ReDocCodeSampleAppender), comsues)
        {
        }

        internal class ReDocCodeSampleAppender : IOperationProcessor
        {
            private readonly string[] _comsues;

            public ReDocCodeSampleAppender(params string[] comsues)
            {
                _comsues = comsues;

            }

            public Task<bool> ProcessAsync(OperationProcessorContext context)
            {
                var currentConsumes = context.OperationDescription.Operation.Consumes;
                    if (currentConsumes == null)
                    {
                        currentConsumes = new List<string>();
                        context.OperationDescription.Operation.Consumes = currentConsumes;
                    }
                 _comsues.ToList().ForEach(
                    comsue =>
                    {
                        currentConsumes.Add(comsue);
                    }
                    );

                return Task.FromResult(true);
            }

        }

    }

and use it like :

 [ConsumesSwaggerOperationProcessorAttribute("application/x-www-form-urlencoded")]
        public NetResponse<User> Login([FromForm]UserAuthType authType, [FromForm]string identifier, [FromForm]string credential)
{
xxx
}

But i think that if NSwag identifer the .net core 's native

[Consumes("application/x-www-form-urlencoded", "application/json")]

and automtic add to the option's comsue.It will be cool and useful.

RicoSuter commented 6 years ago

I think the new AspNetCoreToSwaggerGenerator supports that (whereas the WebApiToSwaggerGenerator may not support this ConsumesAttribute)… Are you using the new generator?

RicoSuter commented 6 years ago

Hmm, this looks like a processor for the new generator...