zijianhuang / webapiclientgen

Strongly Typed Client API Generators generate strongly typed client APIs in C# .NET and in TypeScript for jQuery and Angular 2+ from ASP.NET Web API and .NET Core Web API
MIT License
168 stars 38 forks source link

System.Text.Json to handle null return for DateOnly #126

Closed zijianhuang closed 6 months ago

zijianhuang commented 1 year ago

In branch NetJson

with System.Text.Json

        public async Task<System.Nullable<System.DateOnly>> PostDateOnlyNullableAsync(System.Nullable<System.DateOnly> d, Action<System.Net.Http.Headers.HttpRequestHeaders> handleHeaders = null)
        {
            var requestUri = "api/DateTypes/DateOnlyNullable";
            using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
            var contentJson = JsonSerializer.Serialize(d, jsonSerializerSettings);
            var content = new StringContent(contentJson, System.Text.Encoding.UTF8, "application/json");
            httpRequestMessage.Content = content;
            handleHeaders?.Invoke(httpRequestMessage.Headers);
            var responseMessage = await client.SendAsync(httpRequestMessage);
            try
            {
                responseMessage.EnsureSuccessStatusCodeEx();
                var contentString = await responseMessage.Content.ReadAsStringAsync();
                return JsonSerializer.Deserialize<System.Nullable<System.DateOnly>>(contentString, jsonSerializerSettings);
            }
            finally
            {
                responseMessage.Dispose();
            }
        }

with NewtonSoft.Json

        public async Task<System.Nullable<System.DateOnly>> PostDateOnlyNullableAsync(System.Nullable<System.DateOnly> d, Action<System.Net.Http.Headers.HttpRequestHeaders> handleHeaders = null)
        {
            var requestUri = "api/DateTypes/DateOnlyNullable";
            using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
            using var requestWriter = new System.IO.StringWriter();
            var requestSerializer = JsonSerializer.Create(jsonSerializerSettings);
            requestSerializer.Serialize(requestWriter, d);
            var content = new StringContent(requestWriter.ToString(), System.Text.Encoding.UTF8, "application/json");
            httpRequestMessage.Content = content;
            handleHeaders?.Invoke(httpRequestMessage.Headers);
            var responseMessage = await client.SendAsync(httpRequestMessage);
            try
            {
                responseMessage.EnsureSuccessStatusCodeEx();
                var stream = await responseMessage.Content.ReadAsStreamAsync();
                using JsonReader jsonReader = new JsonTextReader(new System.IO.StreamReader(stream));
                var serializer = JsonSerializer.Create(jsonSerializerSettings);
                return serializer.Deserialize<System.Nullable<System.DateOnly>>(jsonReader);
            }
            finally
            {
                responseMessage.Dispose();
            }
        }

This can be fixed.

Totally 58 failed. Some of which can be fixed at my end. Maybe during Xmas. Otherwise, programmers may stick to NewtonSoft.Json.

zijianhuang commented 6 months ago

all good now