RestCode / WebApiProxy

MIT License
199 stars 91 forks source link

Bug on Actions returning void #113

Open cbordeman opened 8 years ago

cbordeman commented 8 years ago

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

faniereynders commented 8 years ago

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

DigitalDel commented 8 years ago

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);
        }`
cbordeman commented 8 years ago

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.

faniereynders commented 8 years ago

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