Repository navigation
Closed
Description
I have a class with this:
class Dealer {
[JsonIgnore]
[StringLength( MaxNetworksLength )]
public string Networks {
get => _Networks;
set => _Networks = value ?? string.Empty;
}
public IEnumerable<string> NetworkCodeList {
get => !string.IsNullOrEmpty( Networks ) ? Networks?.Split( ',' ) : new string[0];
set => Networks = (value != null) ? string.Join( ",", value ) : string.Empty;
}
}
The Networks property is used by Dapper to read/write to and from the database.
The NetworkCodeList is sent/received to and from a web page.
I am getting an error when ASP.NET is Deserializing the object. This worked fine with Json.Net.
Any ideas what I can do here?
System.NotSupportedException: Collection was of a fixed size.
at System.SZArrayHelper.Add[T](T value)
at System.Text.Json.JsonSerializer.ApplyValueToEnumerable[TProperty](TProperty& value, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnReadEnumerable(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonPropertyInfo.ReadEnumerable(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.HandleValue(JsonTokenType tokenType, JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadAsync[TValue](Stream utf8Json, Type returnType, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext)
at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
Activity
am11 commentedon Oct 8, 2019
Minimal steps to repro: https://dotnetfiddle.net/hdhEDG. Toggle comments on line 10 and 11; it works with
get => null;and throws withget => Array.Empty<string>();ericstj commentedon Nov 6, 2019
Thank you for the report. We understand the issue and @layomia is working on a fix.
Moved this to 5.0 since there is a workaround: make the backing data an IEnumerable and have the string property be the one doing the transformation.
ahsonkhan commentedon Nov 13, 2019
Fixed in dotnet/corefx#42420. @WayneHiller, can you please verify that the issue you reported has been fixed by using the latest nightly build of .NET Core 5.0 (or by adding a PackageReference to the latest S.T.Json NuGet package, from this feed: https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json - version
5.0.0-alpha.1.19563.1or higher).WayneHiller commentedon Nov 13, 2019
I guess I need to wait until tomorrow to get version 5.0.0-alpha.1.19563.1, it is not avail yet
ericstj commentedon Nov 13, 2019
@WayneHiller it should be available. This worked for me:
Note, as @ahsonkhan mentioned, you'll need to pick it up from our nightly feed. Make sure you have a nuget.config with something like
https://github.com/dotnet/corefx/blob/1518fdc5cc94571bf3ce1bb95e1a60f789a67f18/NuGet.config#L9
WayneHiller commentedon Nov 13, 2019
@ericstj Thanks adding it directly to the csproj file worked. However I am still getting the error. Will aspnet core use the new System.Text.Json assembly by just adding it to the project?
ericstj commentedon Nov 14, 2019
It should, if you're still reproducing the issue can you share an isolated repro project for us to try?
WayneHiller commentedon Nov 14, 2019
It is pretty basic. I just created a new aspnet core 3 webapi project and added this controller:
Added the newest System.Text.Json to the project:
And then called POST to http://localhost:5000/test from Postman:
And the result is:
ericstj commentedon Nov 14, 2019
Confirmed that this isn't fixed. Looking at this issue and the fix, I don't think the fix was addressing the original issue but instead was addressing a related thing that @layomia found when investigating the original issue.
If I recall, @layomia mentioned that to fix the original issue we couldn't rely on the Getter to return back a mutable collection, even if we previously set a mutable collection with the setter. We needed to build up the muttable collection first, then only set once. I don't see the fix doing this.
layomia commentedon Nov 19, 2019
Thanks @ericstj. dotnet/corefx#42420 fixes a different issue (discovered while debugging this one) described in that PR's description and illustrated in https://dotnetfiddle.net/hdhEDG. I've updated the PR's description to make it less confusing.
This is indeed the fix for this issue, which is not implemented just yet.
Fix Collection was of a fixed size issue