Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable=Enabled results in required validation errors for parameters or bound properties with default values #18403

Closed
mountain65 opened this issue Jan 17, 2020 · 5 comments · Fixed by #38801
Assignees
Labels
affected-few This issue impacts only small number of customers area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels bug This issue describes a behavior which is not expected - a bug. cost: S Will take up to 2 days to complete enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-model-binding severity-minor This label is used by an internal tool
Milestone

Comments

@mountain65
Copy link

mountain65 commented Jan 17, 2020

I generated a new empty WebApplication, .NET Core 3.1 and added 8.0 and enable to my CSPROJ.

Then I added the following controller code:

    [ApiController]
    [Route("api/[controller]")]
    public class CountriesController : ControllerBase
    {
        // GET: api/<controller>
        [HttpGet]
        public IEnumerable<string> GetByLanguage(string language="nl")
        {
            Console.WriteLine(language);
            return new[] { "Nederland", "Duitsland", "Amerika"};
        }
    }

When I call this endpoint without a language parameter I get a 400 BadRequest with the followin g ProblemDetail:

    type: "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    title: "One or more validation errors occurred.",
    status: 400,
    traceId: "|a897675b-4df1a9d8146c8919.",
    errors: {
        language: [
            "The language field is required."
        ]
    }
}

When I remove the [ApiController] attribute I get the normal optional parameter behaviour once more, OR when I remove the Nullable=enabled option from the CSPROJ.

@javiercn javiercn added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jan 17, 2020
@javiercn
Copy link
Member

@mountain65 thanks for contacting us.

@pranavkm any ideas?

@mountain65
Copy link
Author

Updated the code to include de [ApiController] attribute

@mkArtakMSFT mkArtakMSFT added this to the Discussions milestone Jan 17, 2020
@pranavkm pranavkm changed the title Optional parameters on API controller are treated as required Nullable=Enabled results in required validation errors for parameters or bound properties with default values Jan 17, 2020
@pranavkm pranavkm removed their assignment Jan 17, 2020
@pranavkm pranavkm removed the question label Jan 17, 2020
@pranavkm
Copy link
Contributor

@mountain65, I admit, this is surprising behavior. Non-nullable reference types are treated as required. MVC executes binding and validation prior to inferring default values, consequently you end up getting validation errors for properties and parameters which is always meant to be non-null in the body of the action.

For now, I'd recommend stating that the type is non-null. Alternatively, you may turn off the behavior that implicitly applies non-nullable = Required, by configuring MvcOptions.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes as part of your application start:

services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);

@pranavkm pranavkm removed this from the Discussions milestone Jan 17, 2020
@mountain65
Copy link
Author

I will do that. Thanks.

@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label Jan 22, 2020
@mkArtakMSFT mkArtakMSFT added this to the Next sprint planning milestone Jan 22, 2020
@mkArtakMSFT
Copy link
Member

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@javiercn javiercn added affected-few This issue impacts only small number of customers enhancement This issue represents an ask for new feature or an enhancement to an existing one severity-minor This label is used by an internal tool labels Oct 9, 2020
@mkArtakMSFT mkArtakMSFT added area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels Oct 14, 2021
@brunolins16 brunolins16 added cost: S Will take up to 2 days to complete and removed investigate labels Dec 2, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Feb 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-few This issue impacts only small number of customers area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels bug This issue describes a behavior which is not expected - a bug. cost: S Will take up to 2 days to complete enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-model-binding severity-minor This label is used by an internal tool
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants