OData / OData/AspNetCoreOData

Support record as request payload type

Open
#1,001 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

Assemblies affected
ASP.NET Core OData 8.2

Describe the bug
Does not support record as request payload type.

Reproduce steps
Clone this repo and run the app.
https://github.com/aetos382/RecordSupport

Data Model

public record Product(
    string Id,
    string Name);
public record Product(
    string Id,
    string Name);
public record Customer(
    string Id,
    string Name);

EDM (CSDL) Model

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
  <edmx:DataServices>
    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="RecordSupport.Models">
      <EntityType Name="Order">
        <Key>
          <PropertyRef Name="Id"/>
        </Key>
        <Property Name="Id" Type="Edm.String" Nullable="false"/>
        <Property Name="Name" Type="Edm.String" Nullable="false"/>
      </EntityType>
      <EntityType Name="Customer">
        <Key>
          <PropertyRef Name="Id"/>
        </Key>
        <Property Name="Id" Type="Edm.String" Nullable="false"/>
        <Property Name="Name" Type="Edm.String" Nullable="false"/>
      </EntityType>
    </Schema>
    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default">
      <EntityContainer Name="Container">
        <EntitySet Name="Orders" EntityType="RecordSupport.Models.Order"/>
        <EntitySet Name="Customers" EntityType="RecordSupport.Models.Customer"/>
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

Request/Response

POST http://localhost:xxx/api/Products
Content-Type: application/json

{
  "Id": "P1",
  "Name": "P1"
}
POST http://localhost:xxx/api/Orders
Content-Type: application/json

{
  "Id": "O1",
  "Name": "O1"
}
POST http://localhost:xxx/api/Customers
Content-Type: application/json

{
  "Id": "C1",
  "Name": "C1"
}

Expected behavior
All request payloads are successfully deserialized and passed as arguments to the controller's action method.

Actual behavior
The request for Products succeeds. This controller does not use any OData functionality.
For Orders, the action method is called, but the argument is always passed null.
For Customers, no action methods are called.

Additional context
Requests to Orders and Customers seem to raise the following exceptions.

Cannot dynamically create an instance of type 'RecordSupport.Models.Order'. Reason: No parameterless constructor defined.

   at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at Microsoft.AspNetCore.OData.Formatter.Deserialization.ODataResourceDeserializer.CreateResourceInstance(IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)
   at Microsoft.AspNetCore.OData.Formatter.Deserialization.ODataResourceDeserializer.ReadResource(ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext)
   at Microsoft.AspNetCore.OData.Formatter.Deserialization.ODataResourceDeserializer.ReadInline(Object item, IEdmTypeReference edmType, ODataDeserializerContext readContext)
   at Microsoft.AspNetCore.OData.Formatter.Deserialization.ODataResourceDeserializer.<ReadAsync>d__2.MoveNext()
   at Microsoft.AspNetCore.OData.Formatter.ODataInputFormatter.<ReadFromStreamAsync>d__10.MoveNext()
   at Microsoft.AspNetCore.OData.Formatter.ODataInputFormatter.LoggerError(HttpContext context, Exception ex)
   at Microsoft.AspNetCore.OData.Formatter.ODataInputFormatter.<ReadFromStreamAsync>d__10.MoveNext()
   at Microsoft.AspNetCore.OData.Formatter.ODataInputFormatter.<ReadRequestBodyAsync>d__8.MoveNext()

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported RecordSupport reproduction and the ODataResourceDeserializer.CreateResourceInstance stack-trace entry. Verify the Orders and Customers POST requests, then trace record payload construction and confirm that all request payloads are deserialized and passed to their controller actions without null arguments or constructor errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.