deadmann / AdvancedRestHandler

A utility code that can be used to request Data from services...
MIT License
5 stars 0 forks source link

Response object inheriting ArhResponse will returns null with empty response #7

Closed deadmann closed 3 years ago

deadmann commented 3 years ago

In term the response model is consuming the ArhResponse model, we check 3 type of models of data:

  1. if it is an ArhStringResponse, we create an object of this type, and let the system fill it later, when filling the request and response data
  2. if it is a generic ArhResponse<T> that uses type T, we again create an object of that type, then deserialize the model, and put the result in Result property of that model.
  3. but if the result is a model inheriting the ArhResponse, we deserialize the model and put it directly into the result, in case the response is a empty string for default JSON serializer, the result will (and should) be null.

inside:

private TResponse MakeResponse<TResponse>(string requestString, string responseString,
            HttpStatusCode statusCode, List<Type> fallbackTypes)
                    //It's Normal Deserialization (So we do not need to pre-initialize it)
                    if (typeof(TResponse) != typeof(ArhStringResponse) &&
                        !typeof(TResponse).IsSubclassOf(typeof(ArhResponse)))
                    {
                        result = DeserializeToObject<TResponse>(responseString);
                    }
                    //It's Special Deserialization using RhResponse
                    else
                    {
                        //It contains just string value, And will sits over RhStringResponse (So we need to initialize Model ourselves)
                        if (typeof(TResponse) == typeof(ArhStringResponse))
                        {
                            result = GenerateTResponseRhResponse<TResponse>();
                        }
                        //It contains model data, But sits inside RhResponse Model (So we need to initialize Model ourselves)
                        else if (typeof(TResponse).IsGenericType &&
                                 typeof(TResponse).GetGenericTypeDefinition() == typeof(ArhResponse<>))
                        {
                            result = GenerateTResponseRhResponse<TResponse>();
                            //if Generic Model is String
                            var genericTypeArgument = typeof(TResponse).GenericTypeArguments[0];
                            if (genericTypeArgument == typeof(string))
                            {
                                SetValueOnProperty(result, nameof(ArhResponse<object>.ResultModel), responseString);
                            }
                            //if Generic Model is Object
                            else
                            {
                                // ReSharper disable once PossibleNullReferenceException
                                object output =
                                    DeserializeToType(responseString, genericTypeArgument);

                                SetValueOnProperty(result, nameof(ArhResponse<object>.ResultModel), output);
                                //SetValueOnProperty(result, nameof(RhResponse<object>.ResultModel),
                                //    DeserializeToObject<TResponse>(jsonString);
                            }
                        }
                        //It contains model data, and uses an inherited version of RhResponse (So we do not need to pre-initialize it)
                        else
                        {

/*================= ERROR IN HERE =================
                            result = DeserializeToObject<TResponse>(responseString);

                        }
                    }

then later we use the following code to fill the object:

                finally
                {
                    if (result != null)
                    {
                        SetValueOnProperty(result, nameof(ArhResponse.ResponseStatusCode), statusCode);
                        SetValueOnProperty(result, nameof(ArhResponse.ResponseText), responseString);
                        SetValueOnProperty(result, nameof(ArhResponse.RequestText), requestString);
                    }
                }

if the result is null, we ignore the response and return the null value directly....

We should either fix this issue inside finally method, or inside the decision making if-else for the data inheriting the ArhResponse

deadmann commented 3 years ago

Fixed #8