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

JsonIgnore with condition incorrectly ignoring property when using OData $select query with [EnableQuery] endpoint #2825

Open
radderz opened this issue Dec 1, 2023 · 2 comments

Comments

@radderz
Copy link

radderz commented Dec 1, 2023

public class MyClass
{
	public Guid Id { get; set; }

	[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
	public Guid? SomeReferenceId { get; set; }
}

When using a class like above, on a standard ASP.NET Core controller like below:

[Route("api/[controller]")]
[ApiController]
public class MyClassController : Controller
{
	[HttpGet]
	[EnableQuery]
	public List<MyClass> GetAll()
	{
		return new List<MyClass>
		{
			new MyClass
			{
				Id = Guid.NewGuid()
			},
			new MyClass
			{
				Id = Guid.NewGuid(),
				SomeReferenceId = Guid.NewGuid()
			},
		}
	}
}

Doing a query like /api/MyClass?$select=someReferenceId will return an array of blank objects instead of 1 blank object and one with the someReferenceId.

@gathogojr
Copy link
Contributor

Thanks @radderz for reporting this issue. We currently don't evaluate conditions applied to the JsonIgnore attribute. Implementing support for that may also not be something that the team might consider in the short to medium term. As it is, the library supports only a small subset of the Newtonsoft.Json property attributes, mostly because of limited resources coupled with the challenge of keeping up with the evolution of a third-party library. What then remains to determine what is the "bare minimum" behaviour to implement for scenarios where a condition is applied to the JsonIgnore attribute:
i) Ignore the condition - meaning we don't serialize the property.
ii) Ignore attributes that have conditions - meaning we serialize the property.

@radderz
Copy link
Author

radderz commented Dec 13, 2023

Well I think option 2 is better than option 1 since more is better than missing. more just means larger packets and more cost on the deserialization side which is important in Blazor WASM.

The library that I would have through was being used is System.Text.Json rather than Newtonsoft.Json and the ignore attribute I am talking about is from System.Text.Json.

This isn't a third party library that I am talking about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants