OData / OData/AspNetCoreOData

ODataException: When writing a JSON response... when an OData function returns an entity

Open
#102 7 comments 0 reactions 1 assignee View on GitHub

@xuzhg is already working on this.

Since Mar 3, 2021.

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

Description

Describe the bug

Calling an OData controller function returning an entity raises an ODataException:

When writing a JSON response, a user model must be specified and the entity set and entity type must be passed to the ODataMessageWriter.CreateODataResourceWriter method or the ODataResourceSerializationInfo must be set on the ODataResource or ODataResourceSet that is being written.
at Microsoft.OData.ODataResourceTypeContext.ValidateAndReturn[T](T value)

Assemblies affected

Microsoft.AspNetCore.OData v8.0.0-beta
Microsoft.EntityFrameworkCore v5.0.3

Reproduce steps

Take the attached Visual Studio 2019 project "WebApplicationCoreOdata4-entity" and run it. In the launched browser, type for url:
https://localhost:44353/api/books/GetBookByPressId(PressId=2)

Expected result

The response of odata contening the entity, ie book which PressId field is equal to 2.

Actual result

An ODataException is raised:

Microsoft.OData.ODataException: When writing a JSON response, a user model must be specified and the entity set and entity type must be passed to the ODataMessageWriter.CreateODataResourceWriter method or the ODataResourceSerializationInfo must be set on the ODataResource or ODataResourceSet that is being written.
at Microsoft.OData.ODataResourceTypeContext.ValidateAndReturn[T](T value)
at Microsoft.OData.ODataResourceTypeContext.get_NavigationSourceName()
at Microsoft.OData.ODataContextUrlInfo.Create(ODataResourceTypeContext typeContext, ODataVersion version, Boolean isSingle, ODataUri odataUri)
at Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.<>c__DisplayClass11_0.b__0()
at Microsoft.OData.JsonLight.ODataJsonLightSerializer.WriteContextUriProperty(ODataPayloadKind payloadKind, Func`1 contextUrlInfoGen, ODataContextUrlInfo parentContextUrlInfo, String propertyName)
at Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteResourceContextUri(ODataResourceTypeContext typeContext, ODataContextUrlInfo parentContextUrlInfo)
at Microsoft.OData.JsonLight.ODataJsonLightWriter.StartResource(ODataResource resource)
at Microsoft.OData.ODataWriterCore.<>c__DisplayClass121_0.b__0()
at Microsoft.OData.ODataWriterCore.InterceptException(Action action)
at Microsoft.OData.ODataWriterCore.WriteStartResourceImplementation(ODataResource resource)
at Microsoft.OData.ODataWriterCore.<>c__DisplayClass49_0.b__0()
at Microsoft.OData.TaskUtils.GetTaskForSynchronousOperation(Action synchronousOperation)
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSerializer.WriteResourceAsync(Object graph, ODataWriter writer, ODataSerializerContext writeContext, IEdmTypeReference expectedType)
at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSerializer.WriteObjectInlineAsync(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)
at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSerializer.WriteObjectAsync(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
at Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatterHelper.WriteToStreamAsync(Type type, Object value, IEdmModel model, ODataVersion version, Uri baseAddress, MediaTypeHeaderValue contentType, HttpRequest request, IHeaderDictionary requestHeaders, ODataSerializerProvider serializerProvider)

Additional detail

None

Attachments

WebApplicationCoreOdata4-entity.zip

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.