ODataException: When writing a JSON response... when an OData function returns an entity
@xuzhg is already working on this.
Since Mar 3, 2021.
- 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
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.