0

I have some code like below:

namespace BSN.Commons.Responses
{
    //
    // Summary:
    //     Generic response type for command/query services to return the results.
    [DataContract]
    public class Response<T> where T : class
    {
        //
        // Summary:
        //     Corresponding HttpStatusCode.
        [DataMember(Order = 1)]
        [JsonConverter(typeof(JsonForceDefaultConverter<ResponseStatusCode>))]
        public ResponseStatusCode StatusCode { get; set; }

        //
        // Summary:
        //     Data payload.
        [DataMember(Order = 2)]
        public T Data { get; set; }

        //
        // Summary:
        //     Human-readable message for the End-User.
        [DataMember(Order = 3)]
        public string Message { get; set; }

        //
        // Summary:
        //     Invalid items of the request object.
        [DataMember(Order = 4)]
        public IList<InvalidItem> InvalidItems { get; set; }

        //
        // Summary:
        //     Distinction between successful and unsuccessful result.
        public bool IsSuccess
        {
            get
            {
                if (StatusCode >= ResponseStatusCode.OK)
                {
                    return StatusCode <= (ResponseStatusCode)299;
                }

                return false;
            }
        }
    }
}

And my problem is NSwag-generated code like below:

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
    public partial class OverTheTopCustomerApplicationViewModelResponse
    {
        [Newtonsoft.Json.JsonProperty("statusCode", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public ResponseStatusCode StatusCode { get; set; }

        [Newtonsoft.Json.JsonProperty("data", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public OverTheTopCustomerApplicationViewModel Data { get; set; }

        [Newtonsoft.Json.JsonProperty("message", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Message { get; set; }

        [Newtonsoft.Json.JsonProperty("invalidItems", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public System.Collections.Generic.ICollection<InvalidItem> InvalidItems { get; set; }

        [Newtonsoft.Json.JsonProperty("isSuccess", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public bool IsSuccess { get; set; }

    }

As you can see in the above generated code, Data is tagged with Required, but as you can see in my original code, I do not say Required or anything about this (in this situation the null object passing cause an error / exception :( and I do not want this).

How can I resolve this problem?

sorosh_sabz
  • 2,356
  • 2
  • 32
  • 53

0 Answers0