Exception on IRestGateway with FromJson on POST response T

I am checking out the IRestGateway, and I have it working for GET and DELETE endpoints. However, when trying a POST, I get a null reference thrown on the line json.FromJson(), which seems to be because the response T is unknown.

The Send command of the POST is working, as I can see the newly created data in the external system, so the issue is that I am not mapping to Json response to the ReponseDto T.

Any ideas, what I am doing wrong?

namespace My.ServiceModel.Types

    public class TradeInformation
    {
        public int Id { get; set; } = 0;
        public string Reference { get; set; } = "";
        public string Status { get; set; } = "";
    }

namespace My.ServiceModel

  [Route("/trade", "POST")]
  public class CreateTrade : IPost, IReturn<CreateTradeResponse>
  {
      public string Reference { get; set; }
      public string Status { get; set; }
  }
    
  public class CreateTradeResponse : TradeInformation
  {
  }

namespace My.ServiceInterface

  public CreateTradeResponse Post(CreateTrade request)
  {
      return gateway.Send(request);
  }

  public class ExternalGateway : IRestGateway
      
    public T Send<T>(IReturn<T> request, string method, bool sendRequestBody = true, string idempotencyKey = null)
      {
          using (new ConfigScope())
          {
              var relativeUrl = request.ToUrl(method);
              var body = sendRequestBody ? request.GetDto().ToJson() : null;
              var json = Send(relativeUrl, method, body, idempotencyKey);
              var response = json.FromJson<T>();   // T seems to be unnown
              return response;
          }
      }

Wont be able to identify the issue tell from just the sample code, would need a stand-alone repro to be able to debug it. I’d remove the empty ConfigScope() since it’s not being used.

If the issue is deserializing the DTO, you should be able to create a stand-alone test that tries to deserialize a snapshot of the JSON response into the return Type where I’m assuming is where the issue is.

You can try enabling StrictMode so any internal deserialization errors are rethrown which might identify a swallowed exception, e.g:

Env.StrictMode = true;

Note:

var response = json.FromJson<T>();   // T seems to be unnown

T is always known in a generic method, you can view it it with typeof(T).Name.

Thank for the tips, they helped me resolve the issue, which, as anticipated, was not a ServiceStack problem.

For closure, the Json response from the external server was an array of T not a single T, so it was fixed with adding List to the return type:

namespace My.ServiceModel

  [Route("/trade", "POST")]
  public class CreateTrade : IPost, IReturn<List<CreateTradeResponse>>
  {
      public string Reference { get; set; }
      public string Status { get; set; }
  }
    
  public class CreateTradeResponse : List<TradeInformation>
  {
  }
1 Like