RestCode / WebApiProxy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug on Actions returning void

cbordeman opened this issue · comments

When an action in the REST service returns void ([ResponseType(typeof(void))] on the action), the C# T4 template CSharpProxyTemplate.tt generates the line:

return result.Content.ReadAsAsync<void>().Result;

...in the generated proxy C# source. Obviously, that doesn't compile!

Essentially the T4 template variable "concreteReturnType" needs to be checked for void and a) the method return type and b) the return statement modified. I haven't inspected the entire template.

Chris Bordeman

Hi @cbordeman, I don't think one should use ResponseType with void. Rather use concrete types.

I've also encountered this issue.

The auto generated Web API controller methods for PUT actions are decorated with [ResponseType(typeof(void))] by default when you create them based on an EF model.

How should the below (auto generated) method be changed to resolve this?

`        [ResponseType(typeof(void))]
        public IHttpActionResult PutPayment(int id, Payment payment)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != payment.PaymentID)
            {
                return BadRequest();
            }

            db.Entry(payment).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PaymentExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }`

As Del says, Visual Studio populates void in automatically generated webapi controllers.

Del, I suppose you could create a public class Void {} and use that instead. I'd much rather see the T4 adjusted though.

Del, you may want to check out NSwagStudio if this doesn't work.

I see. In this case it make sense for the T4 logic to be updated. Technically this should return an HTTPResponseMessage