-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Improve error message in scenaria where JsonIgnore
is combined with required
members.
#92330
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionIt's often necessary to use the Reproduction Stepsvoid Main() {
var dto = new DTO {
NotRequiredNumber = 1,
Number = 2
};
JsonSerializer.Serialize<DTO>(dto);
}
class DTO {
[JsonIgnore]
public required int Number { get; init; }
public int NotRequiredNumber { get; init; }
} Expected behaviorShould be able to have the JSON generated without the ignored property. Actual behaviorJsonPropertyInfo 'Number' defined in type 'UserQuery+DTO' is marked required but does not specify a setter. Regression?No Known WorkaroundsNo response Configuration
Other informationNo response
|
To be clear, are you only asking for this to be supported during serialization and not during deserialization? |
Annotating a property with What could be improved is the error message though, we should add better testing validating how the two features interact, specifically when the new ignore conditions are implemented in #66490. |
JsonIgnore
is combined with required
members.
That's correct, just during deserialization. |
During deserialization (going from JSON to C#)? (Your code shows serialization.) I think you have a DTO design flaw then. You're saying that you want to ignore Why do you want I would split the DTO into two models: a domain model for populating in C# with |
Sorry @gregsdennis, I meant serialization as you assumed. While you're technically 100% correct, I was hoping to not have to have another object. This is already a temporary object that's populated via AutoMapper and ProjectTo. While I don't want the caller to get the value, I do need it for the next query that's done in the same method. I was hoping to avoid having to do yet another mapping to strip out that one property. Think of something like this obviously non-compiling code. Need the internal ID for the next query but don't want to send it back. var items = await _context.SomeTable.ProjectTo<SomeObj>(...).ToArrayAsync();
var ids = items.Select(x => x.InternalIdentifier).ToArrayAsync();
var otherStuff = await _context.SomeOtherTable
.Where(x => ids.Contains(x.SomeTableIdentifier))
.ToArrayAsync();
var mapping = otherStuff
.ToLookup(x => x.InternalIdentifier)
.ToDictionary(x => x.Key, x => x.Select(...));
foreach (var item in items) {
item.Stuff = mapping[item.InternalIdentifier]; |
Sure, do what makes sense for you. But I agree that this is by-design behavior. |
I hit this. In my case, I have a custom deserializer, but wanted to utilize the JsonSerializer for serialization. That's why it made sense for |
@AArnott - How did you work around this? |
@TonyValenti I don't recall, sorry. But I can only imagine I avoided System.Text.Json even for serialization since it didn't work. I probably would have switched to MessagePack, since that's my go-to for serialization anyway as it's faster and more compact. |
As a work around I set the property via a constructor parameter, removed required, and marked it as JsonIgnore. |
Description
It's often necessary to use the
required
flag on a property that you don't want serialized via JSON. Adding theJsonIgnore
attribute to such a property results in anInvalidOperationException
.Reproduction Steps
Expected behavior
Should be able to have the JSON generated without the ignored property.
Actual behavior
JsonPropertyInfo 'Number' defined in type 'UserQuery+DTO' is marked required but does not specify a setter.
Regression?
No
Known Workarounds
No response
Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: