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

ASPNET CORE 3.1 - string Parameter with Default value THROWS ERROR #102

Closed cetindogu closed 4 years ago

cetindogu commented 4 years ago

My Web Api Method Signature; [HttpGet] IEnumerable AthletheSearch(int? take = 10, int skip = 0, string order = null, string sort = null, string search = null);

Auto Generated method's first line var requestUri = new Uri(this.baseUri, "api/Athlethe?"+(take.HasValue?"take="+take.Value.ToString():String.Empty)+"&skip="+skip+"&order="+Uri.EscapeDataString(order)+"&sort="+Uri.EscapeDataString(sort)+"&search="+Uri.EscapeDataString(search));

THROWS ERROR: ArgumentNullException: Value cannot be null. (Parameter 'stringToEscape') System.Uri.EscapeDataString(string stringToEscape)

when i used in my ASPNET CORE 3.1 MVC project client; HttpClient client = new HttpClient(); Uri uriBase = new Uri("https://localhost:44314/");

        Backend.Services.Controllers.Client.Athlethe srv =
            new Backend.Services.Controllers.Client.Athlethe(client, uriBase);
        var list = srv.**AthletheSearch**(10, 0, null, null, null);
        return View(list);
zijianhuang commented 4 years ago

In the past I had always used POST object for complex search query, so I don't need to worry the 2048 byte query limit. Anyway, are you happy with such generated codes?

        /// <summary>
        /// GET api/SuperDemo/AthletheSearch?take={take}&skip={skip}&order={order}&sort={sort}&search={search}
        /// </summary>
        public string AthletheSearch(System.Nullable<int> take, int skip, string order, string sort, string search)
        {
            var requestUri = "api/SuperDemo/AthletheSearch?" + (take.HasValue ? "take=" + take.Value.ToString() : "" ) + "&skip=" + skip + (order == null ? "" : "&order=" + Uri.EscapeDataString(order)) + (sort == null ? "" : "&sort=" + Uri.EscapeDataString(sort)) + (search == null ? "" : "&search=" + Uri.EscapeDataString(search));
            var responseMessage = this.client.GetAsync(requestUri).Result;
            try
            {
                responseMessage.EnsureSuccessStatusCodeEx();
                var stream = responseMessage.Content.ReadAsStreamAsync().Result;
                using (System.IO.StreamReader streamReader = new System.IO.StreamReader(stream))
                {
                    return streamReader.ReadToEnd(); ;
                }
            }
            finally
            {
                responseMessage.Dispose();
            }
        }
zijianhuang commented 4 years ago

Also, for TS client, what do you expect the behavior of different string value. In C#, a string parameter value could be:

  1. "value"
  2. ""
  3. null

In JS/TS

  1. "value"
  2. ""
  3. null
  4. undefined

Apparently null and undefined could be considered as undefined. And a simple way to check isNullOrUndefined is mentioned by mar10 @ https://stackoverflow.com/questions/2559318/how-to-check-for-an-undefined-or-null-variable-in-javascript

Are you happy with that?

zijianhuang commented 4 years ago

released 3.11.3