WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

Optional parameter not working as expected, requires atleast one other parameter

Open RobbeSchepens opened this issue 8 years ago • 0 comments

I have been investigating optional parameters and I believe they don't work as intended by the OData 4.01 Committee Spec:

Optional Function Parameters – Function parameters can be annotated as optional. Optional parameters may be omitted when invoking the function.

As I understand it, I should be able to call the action without providing the optional parameter. Yet this only works when at least one additional parameter is present.

This is likely the same issue as https://github.com/OData/WebApi/issues/1678 yet I don't get a 500 error. I receive 204 No Content and 404 Not Found.

Assemblies affected

.NET Core 2.1.1 Microsoft.AspNet.OData 7.1

Reproduce steps

Check out the sample project I setup for functional parameters: https://github.com/RobbeSchepens/BookStoreOptionalParameters

I highly recommend looking at the BooksController which shows all tested queries with results written in the comments of the summary above each action: https://github.com/RobbeSchepens/BookStoreOptionalParameters/blob/master/BookStore/Controllers/BooksController.cs

Or create a sample project with .NET Core 2.1 and OData 7.1 and create an action with an optional parameter. Example:

        [EnableQuery]
        public IActionResult GetBookByAuthorWithoutRequiredParameter(string nameOptional = "Mark Michaelis")
        {
            return Ok(_db.Books.FirstOrDefault(c => c.Author == nameOptional));
        }

Expected result

I can call for odata/Books/GetBookByAuthorWithoutRequiredParameter and receive the book from author Mark Michaelis.

Actual result

404 Not Found: odata/Books/GetBookByAuthorWithoutRequiredParameter 204 No Content: odata/Books/GetBookByAuthorWithoutRequiredParameter(nameOptional=null)

Debugging that last get request shows that the optional parameter gets overwritten with null, so it's not taking the default value.

RobbeSchepens avatar Nov 29 '18 10:11 RobbeSchepens