title: Web API method parameter considered required when using [CanBeNull]
body: <p>After migrating one of our apps to .NET 6.0, I've noticed a strange behavior. Here's my method signature (all parameters are passed through query string parameters):</p>
  <pre><code>[HttpGet(&quot;search&quot;)]
  public async Task&lt;ActionResult&lt;Paginated&lt;Worker&gt;&gt;&gt; SearchAsync(string? name, ...) {...
  </code></pre>
  <p>Now, I was using resharper and it was automatically replacing the <code>?</code> with the <code>CanBeNullAttribute</code>:</p>
  <pre><code>[HttpGet(&quot;search&quot;)]
  public async Task&lt;ActionResult&lt;Paginated&lt;Worker&gt;&gt;&gt; SearchAsync([CanBeNull]string name, ...) {...
  </code></pre>
  <p>I've also had nullable references enabled on the project (<code>&lt;Nullable&gt;enable&lt;/Nullable&gt;</code>) and I'm using NewsoftJson as my json converter.</p>
  <p>After publishing the project, I've noticed that I was getting a 400 when <code>name</code> was not set to valid string (ex.: when the url looks like <code>/search?otherparameter=hhh&amp;...</code> or <code>/search?name=&amp;otherparameter=lll</code>): <code>The name field is required.</code>.</p>
  <p>Now, I've noticed that:</p>
  <ol>
  <li>Disabling nullable reference types at the project level makes everything work out</li>
  <li>Disabling r# replacement of <code>?</code> with <code>[CanBeNull]</code> while enabling nullable references at the project makes everything work out too.</li>
  </ol>
  <p>I always thought that nullable reference types were a static thing, ie, would only be used by the compiler to ensure that null exceptions were a thing of the past. However, the previous behavior disproves that theory, though, since it seems like it's being used my the converter when checking the parameters' values.</p>
  <p>Btw, why does resharper insists in replacing the <code>?</code> with the <code>[CanBeNull]</code> if the results aren't the same?</p>
  <p><strong>edit</strong>: I've found this <a href="https://www.dajbych.net/how-c-nullable-reference-types-affects-your-asp-net-core-routing" rel="nofollow noreferrer">link</a> that confirms the behavior but still no official doc which explain how a static analysis feature ends up changing the runtime behavior..</p>
  <p>Thanks.</p>