Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping inconsistency on boolean properties in JSON objects #31256

Closed
Mavox opened this issue Jul 13, 2023 · 3 comments
Closed

Mapping inconsistency on boolean properties in JSON objects #31256

Mavox opened this issue Jul 13, 2023 · 3 comments
Assignees
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@Mavox
Copy link

Mavox commented Jul 13, 2023

Hello, tried to see if this issue was reported already, but couldn't find anything (though it's most likely related to an open issue somewhere).

I have encountered an issue when an entity with a nullable boolean property is mapped to a JSON column via the ToJson method.
When saving the entity to the database, the boolean property is saved in a string format { "MyBooleanProperty": "false" }. But when reading the entity from the database an exception is thrown.

Exception:

System.Text.Json.JsonException: The JSON value could not be converted to System.Nullable`1[System.Boolean]. Path: $ | LineNumber: 0 | BytePositionInLine: 6. ---> System.InvalidOperationException: Cannot get the value of a token type 'String' as a boolean.

Stack trace:

ThrowHelper.ThrowInvalidOperationException_ExpectedBoolean(JsonTokenType tokenType)
Utf8JsonReader.GetBoolean()
BooleanConverter.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
NullableConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
--- End of inner exception stack trace ---
ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
lambda_method137(Closure , QueryContext , Object[] , JsonElement )
lambda_method136(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
Enumerator.MoveNext()
Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
lambda_method139(Closure , QueryContext )
QueryCompiler.Execute[TResult](Expression query)
EntityQueryProvider.Execute[TResult](Expression expression)
Queryable.First[TSource](IQueryable`1 source)
UnitTest1.Json_boolean_test() line 33

I previously reported a similar issue related to enums in issue #30598 that was a duplicate of issue #30330 . It was stated in issue #30598 that the issue in #30330 did not only affect enums, but all non string values, leading me to belive it might be related to this issue. The difference in this case is that there are no converters involved (at least not explicitly provided by me 😄).

Code/reproduction

Here is a small solution containing a single unit test that recreates the issue (access to a database is required to run, see connection string on line 56): EFJsonBooleanIssues.zip

Versions

EF Core version: 7.0.7
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
IDE: Visual Studio Professional 2022 17.5.5

@maumar maumar self-assigned this Jul 17, 2023
@maumar
Copy link
Contributor

maumar commented Jul 17, 2023

works fine on current bits, closing

@maumar maumar closed this as completed Jul 17, 2023
@Mavox
Copy link
Author

Mavox commented Jul 17, 2023

You did not get the exception in the example code? Then I must have missed something, I will check again.
Thank you for your time.

@maumar
Copy link
Contributor

maumar commented Jul 17, 2023

I didn't get the exception - the code executes just fine, producing the following sql:

SELECT TOP(1) [m].[Id], [m].[MyJsonProperty]
FROM [MyEntity] AS [m]

and the following result:

Id: 1, MyJsonProperty: {"MyBooleanProperty":true}

just to clarify, i'm trying on current dev bits in main branch (i.e. future 8.0) - it must have gotten fixed as part of some other issue, but not necessarily patched into 7.0.x, so the bug report was completely valid.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2023
@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants