-
Notifications
You must be signed in to change notification settings - Fork 10.4k
React to new System.Text.Json features #9572
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
Conversation
src/Mvc/Mvc.Core/src/MvcOptions.cs
Outdated
|
||
// Ignore casing when serializing values. Setting this to true keeps parity with Json.NET's | ||
// behavior. | ||
PropertyNameCaseInsensitive = true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you feel about this? In the absence of this property, it is required to send camel-case values to successfully deserialize. Most of our tests use pascal-case when sending data.
We can update tests, but I'm not sure how sad users would be when random casing doesn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think true is the right default for MVC. Also it keeps closer to Json.NET so less friction when upgrading. If someone wants to be strict then they can always change the setting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just spoke to @rynowak offline and he's of the opinion that having it be case sensitive by default would be the right way to go. Users are always free to change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use the best default regardless of migration. You can always turn this ON for migrating.
I'm interested to hear arguments about case-insensitive is the right default for a REST API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made this case sensitive now and updated tests to be 🐫 cased
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm interested to hear arguments about case-insensitive is the right default for a REST API.
Nothing other than it slightly reduces issues when using an API and I don't see much disadvantage. I don't feel strongly about it. As long as it is configurable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could always revisit this once we hear what users have to say with the preview5 bits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing other than it slightly reduces issues when using an API and I don't see much disadvantage. I don't feel strongly about it. As long as it is configurable
APIs document their contract and are precise about what they support. JSON is a case sensitive format, and it's reasonable to follow along with that. I expect if we asked API users they would overwhelmingly support us following the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON being case sensitive in the browser is prior art, not a spec. There is no spec about how JSON should map to a .NET object 😋
When it comes to JSON my philosophy has been be flexible in what you accept and exact in what you produce. It appeals to software devs to be strict but when it comes time to use the new serializer I suspect a lot of people will turn case insensitive on to ease the way.
8f39208
to
c138325
Compare
fe648e5
to
e577bcc
Compare
e577bcc
to
dbe4b80
Compare
} | ||
|
||
[Fact] | ||
public override async Task Formatting_CollectionType() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove these tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They were overriding the baseline tests https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/test/Mvc.FunctionalTests/JsonOutputFormatterTestBase.cs#L117-L129 because they were different from what we would want (i.e did not do camel-case). Now that it matches the baseline, we can have it call the one on the base.
} | ||
|
||
[Fact] | ||
public virtual void RoundTripTest_StringThatIsNotCompliantGuid() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to verify, we are ok with this, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, this is fine. We just need to able to document "a" behavior so we don't find it confusing \ surprising.
} | ||
else | ||
{ | ||
deserializedValue = stringValue; | ||
deserializedValue = item.Value.GetString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will the JSON strings more often than not be GUID/DateTimes (or will they primarily be regular strings)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd expect the latter. It's not uncommon for users to JSON serialize on complex models so that they could put it in this dictionary. Is there a way to improve this?
@@ -194,8 +195,8 @@ protected async Task ReadAsync_ReadsValidArray_AsList(Type requestedType) | |||
|
|||
// Assert | |||
Assert.False(result.HasError); | |||
var integers = Assert.IsType<List<int>>(result.Model); | |||
Assert.Equal(new int[] { 0, 23, 300 }, integers); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What motivated this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good one. I should have brought this up - @layomia was there a reason System.Text.Json returns a type that wraps List<T>
rather than just returning a List<T>
? Not that it ultimately matters, but if I remember correctly we agreed on just doing a List<T>
like Json.NET does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also cc @steveharter
No description provided.