OData / AspNetCoreOData

ASP.NET Core OData: A server library built upon ODataLib and ASP.NET Core
Other
454 stars 159 forks source link

Passing multiple id's in url returns single result, should return 404 Not Found #742

Open dnperfors opened 1 year ago

dnperfors commented 1 year ago

Assemblies affected ASP.NET Core OData 8.x (master)

Describe the bug When getting a single entity, but passing multiple ID's will return a single result (the first id found in the list). However, according to the specification this doesn't seem to be a valid url.

Reproduce steps

  1. Start the ODataRoutingSample
  2. Make a GET request to http://localhost:5000/products(1,2,3) or http://localhost:5000/products(1)(2)(3)
  3. The service returns a product with ID 1.

Data Model Data model: https://github.com/OData/AspNetCoreOData/blob/9f1547e92a6bbed936dcae705491049014873be7/sample/ODataRoutingSample/Models/Product.cs#L12-L25 Controller: https://github.com/OData/AspNetCoreOData/blob/9f1547e92a6bbed936dcae705491049014873be7/sample/ODataRoutingSample/Controllers/ProductsController.cs#L74-L85

EDM (CSDL) Model You probably know this EDM better than I do...

Request/Response

http://localhost:5000/> GET /products(1,2,3)
HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; charset=utf-8
Date: Tue, 15 Nov 2022 14:55:01 GMT
OData-Version: 4.0
Server: Kestrel
Transfer-Encoding: chunked

{
  "@odata.context": "http://localhost:5000/$metadata#Products/$entity",
  "Id": 1,
  "Category": "Goods",
  "Color": "Red",
  "CreatedDate": 4/16/2001 2:24:08 AM,
  "UpdatedDate": 2/16/2011 1:24:08 AM
}

Expected behavior I would expect a 404 Not Found, since:

I could expect a 400 Bad Request, since the URL is not a valid OData URL, but Not Found seems to be more appropriate.

dnperfors commented 1 year ago

Possibly related are calls to /products(1)(2)(3) (will test this when I get back to work tomorrow)

xuzhg commented 1 year ago

@dnperfors what's your Edm model (xml csdl)? and what's your controller/action?

dnperfors commented 1 year ago

@dnperfors what's your Edm model (xml csdl)? and what's your controller/action?

I am pretty sure I mentioned the ODataRoutingSample, without any changes...

But I have a smaller reproduction here:

Controller:

        [HttpGet]
        [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.None)]
        public IQueryable<Book> GetBooks() => books.AsQueryable();

        [HttpGet]
        [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.None)]
        public SingleResult<Book> GetBook(int key) => SingleResult.Create(books.Where(x => x.Id == key).AsQueryable());

Edm:

<?xml version="1.0" encoding="utf-8"?>
  <edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
    <edmx:DataServices>
      <Schema Namespace="eLibrary.WebApi.OData.Models" xmlns="http://docs.oasis-open.org/odata/ns/edm">
        <EntityType Name="Book">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Edm.Int32" Nullable="false" />
          <Property Name="Title" Type="Edm.String" Nullable="false" />
          <Property Name="Author" Type="Edm.String" Nullable="false" />
        </EntityType>
      </Schema>
      <Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
        <EntityContainer Name="Container">
          <EntitySet Name="Books" EntityType="eLibrary.WebApi.OData.Models.Book" />
        </EntityContainer>
      </Schema>
    </edmx:DataServices>
  </edmx:Edmx>

Possibly related are calls to /products(1)(2)(3)

The above URL type has the same behavior.