Skip to content

Identifiable<string> causes exceptions in Post #382

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

Closed
Nihimon opened this issue Aug 23, 2018 · 2 comments · Fixed by #395
Closed

Identifiable<string> causes exceptions in Post #382

Nihimon opened this issue Aug 23, 2018 · 2 comments · Fixed by #395
Labels
Milestone

Comments

@Nihimon
Copy link

Nihimon commented Aug 23, 2018

Description

I created a prototype Api using a simple model class inheriting Identifiable and a custom IResourceService (to avoid using EF) and tested the Post and primary Get methods. Everything worked great.

I then tried to change the model class to inherit Identifiable (to match our production system). The Gets work fine, but the Post threw exceptions. First, it threw a NullReferenceException in GetStringId before the Controller Action was called. I set the Id in the constructor and then it returned 403 Forbidden.

I also tested using Identifiable which worked as expected.

I created a demo solution (basic Asp.Net Core 2.1 Api only adding JsonApiDotNetCore) to demonstrate the problem. I made changes in Startup.cs, and added three Controllers (and related classes).

You can find the demo solution at JsonApiDotNetCore-Bug-Reproduction.

Note that my-ints and my-guids work as expected, while my-strings fails with the exceptions above.

Environment

Visual Studio 2017 Professional 15.8.1

  • JsonApiDotNetCore Version: 2.5.1
  • Other Relevant Package Versions: Microsoft.NETCore.App 2.1.0, Microsoft.AspNetCore.App 2.1.1
@jaredcnance
Copy link
Contributor

jaredcnance commented Aug 23, 2018

Thanks for reporting this and the reproduction. The null ref is thrown here:

var stringValue = value.ToString();

I'm unsure why this wasn't caught by our tests right now. As a quick fix, you can add this line to your model and things should work for you.

protected override string GetStringId(object value) => value?.ToString() ?? string.Empty;

I set the Id in the constructor and then it returned 403 Forbidden

By setting the Id in the constructor, the controller believes the id was set by the client and since AllowClientGeneratedIds is disabled by default, that is why you received a 403.

if (!_jsonApiContext.Options.AllowClientGeneratedIds && !string.IsNullOrEmpty(entity.StringId))
return Forbidden();

@Nihimon
Copy link
Author

Nihimon commented Aug 23, 2018

That makes sense. Thanks for the quick reply and the workaround.

@jaredcnance jaredcnance added this to the v3.0 milestone Sep 4, 2018
milosloub pushed a commit to milosloub/JsonApiDotNetCore that referenced this issue Sep 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants