dapr / dapr/components-contrib

MongoDB Query Serialization Issue [Query API]

Open
#3,236 2 comments 0 reactions 1 assignee Claimed by @berndverst View on GitHub
kind/bug pinned
Dominant language
Go
Stars
602
Forks
580
Avg merge
4d 9h
Merged PRs (30d)
6

Description

## Expected Behavior
Results from a MongoDB query should return the datatypes that were originally serialized.

## Actual Behavior
Results from a MongoDB query will have integers returned as decimals which causes serialization issues. Serialized data such as `{"title": null, "comments": null, "id": 1, "name": "TestUser" }`, will be returned as `{"title": null, "comments": null, "id": 1.0, "name": "TestUser" }`.

## Steps to Reproduce the Problem
[AuthorCommentCollector.zip](https://github.com/dapr/components-contrib/files/13397599/AuthorCommentCollector.zip) contains a simple application which can be used to reproduce the issue.

Open the project using Visual Studio 2022, change the startup project to docker-compose
![image](https://github.com/dapr/components-contrib/assets/54407796/ece9bda0-22ed-4ae9-a373-453da3aa530a) and run the project.

Once containers have all be started open the Https folder
![image](https://github.com/dapr/components-contrib/assets/54407796/9e4c5975-df44-42d9-98e4-ed41e71fd3af) and open the `AuthorsService.http` file.

Use the http post to add an author
![image](https://github.com/dapr/components-contrib/assets/54407796/d8669d57-2399-4e9d-8c0f-91b84a2a603a)

Next use the http get to query author by name
![image](https://github.com/dapr/components-contrib/assets/54407796/83da49a7-227c-49ba-bce2-ec0ddeab6679)

Which will result in the following exception:
```
System.InvalidOperationException: The type 'System.Threading.ExecutionContext&' of property 'Context' on type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Collections.Generic.IEnumerable`1[AuthorsService.Models.Author],AuthorsService.Services.AuthorsStorage+d__1]' is invalid for serialization or deserialization because it is a pointer type, is a ref struct, or contains generic parameters that have not been replaced by specific types.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_CannotSerializeInvalidType(Type typeToConvert, Type declaringType, MemberInfo memberInfo)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.CreatePropertyInfo(JsonTypeInfo typeInfo, Type typeToConvert, MemberInfo memberInfo, JsonSerializerOptions options, Boolean shouldCheckForRequiredKeyword, Boolean hasJsonIncludeAttribute)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.AddMembersDeclaredBySuperType(JsonTypeInfo typeInfo, Type currentType, Boolean constructorHasSetsRequiredMembersAttribute, PropertyHierarchyResolutionState& state)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.PopulateProperties(JsonTypeInfo typeInfo)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.CreateTypeInfoCore(Type type, JsonConverter converter, JsonSerializerOptions options)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetTypeInfo(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(Type type)
at System.Text.Json.JsonSerializerOptions.CachingContext.CreateCacheEntry(Type type, CachingContext context)
--- End of stack trace from previous location ---
at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
at System.Text.Json.JsonSerializerOptions.GetTypeInfoForRootType(Type type, Boolean fallBackToNearestAncestorType)
at System.Text.Json.JsonSerializerOptions.TryGetPolymorphicTypeInfoForRootType(Object rootValue, JsonTypeInfo& polymorphicTypeInfo)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.SerializeAsync(Stream utf8Json, T rootValue, CancellationToken cancellationToken, Object rootValueBoxed)
at Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.WriteAsJsonAsyncSlow[TValue](Stream body, TValue value, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteTaskResult[T](Task`1 task, HttpContext httpContext)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
```

Alternate method would be to use docker compose up, then use a browser to `http://localhost:9000/swagger/index.html`, use swagger document to add and query author.

Thanks!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.