question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Null" string passed to controllers, despite option for custom null value

See original GitHub issue

The Null value used for query parameters which are null option does not work for a parameter of type string.

I created a GET method with this signature, so I have a string, a DateTime? and a DateTime:

    public async Task<ActionResult<bool>> HasOauthMapping(string accessToken, DateTime nonNullableDate, DateTime? nullableDate)

The generated typescript (Angular) code is as follows (with "foo" used for the ‘substitute’ null value):

  if (accessToken !== undefined)
        url_ += "accessToken=" + encodeURIComponent("" + accessToken) + "&"; 

    if (nonNullableDate === null)
        throw new Error("The parameter 'nonNullableDate' cannot be null.");
    else if (nonNullableDate !== undefined)
        url_ += "nonNullableDate=" + encodeURIComponent(nonNullableDate ? "" + xnonNullableDate.toJSON() : "foo") + "&"; 

    if (nullableDate !== undefined)
        url_ += "nullableDate=" + encodeURIComponent(nullableDate ? "" + nullableDate.toJSON() : "foo") + "&"; 
    url_ = url_.replace(/[?&]$/, "");

As you can see the value "foo" is correctly substituted for both DateTime parameters but for a string parameter a null value gets converted to "" + null or the actual string "null".

I can pass undefined but I’d much prefer for the same logic to apply for all data types.

This is similar to this issue https://github.com/RSuter/NSwag/issues/1168

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
adamjones1commented, Jun 11, 2019

I would find it very useful if you could customise the “null string” that the Null value used for query parameters which are null promises to do, rather than have it hard-coded to "". In many situations the empty string is a perfectly valid input which is not semantically the same as null, and you need to be able to distinguish between the two. Or you may simply be attempting to connect with a service beyond your control which uses a different convention for representing nulls than this.

Common options include:

  • The URL-encoded null value (/endpoint?foo=%00&bar=1);
  • A parameter name without a value (/endpoint?foo&bar=1 - no equals sign)
  • Using "" as you say (/endpoint?foo=&bar=1);
  • And using a non-empty designated null string outside the space of valid input strings (/endpoint?foo=#NULL#&bar=1 if # is an illegal character, say)

APIs vary so support for any of these cases, not just the "" case, would be tremendous.

3reactions
RicoSutercommented, Dec 20, 2018

I think we should send “” instead of “null” when passing a null parameter, right? “null” does not make sense in any case…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Controller is populating objects with null value despite ...
Controller is populating objects with null value despite them not being bound · The correct approach is to get the data model based...
Read more >
Apex Component Controller returning Null for simple string ...
VF Page (no controller). I want to pass a string value from the page to the component. <apex:page lightningStylesheets= ...
Read more >
Use empty string, null or remove empty property in API ...
Json translates null/missing properties to/from F# option types only and will otherwise ... Empty string still is a value, it is just empty....
Read more >
Understanding null safety
Since Null is no longer a subtype, no type except the special Null class permits the value null . We've made all types...
Read more >
C# 11.0 preview: parameter null checking
C# 11.0 has a new syntax, !!, which makes it easier to detect and reject null arguments. It is simple, but misunderstood.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found