Uh oh!
There was an error while loading. Please reload this page.
Add tests for customizing source-gen contracts - #76531
Conversation
ghost
commented
Oct 3, 2022
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsAttempting to understand/codify behavior expectations when users attempt to modify contracts generated by the source generator.
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
3832bc5 to
a66a069Compare| _name = value; | ||
| // This setter should only be called by end users. | ||
| // Disable fast-path if a user modifies a property name. |
There was a problem hiding this comment.
If the user modifies source gen metadata, then they're necessarily using a contract resolver other than source generated JsonSerializerContext instances. In such cases I would expect that CanUseSerializeHandler is always flipped back to false:
Is there any particular case that was missed out and there's a failing test without this check?
There was a problem hiding this comment.
That would be this:
JsonTypeInfo<Person>typeInfo=SourceGenContext.Default.Person;foreach(JsonPropertyInfopropertyintypeInfo.Properties){property.Name=property.Name.ToUpperInvariant();}stringjson=JsonSerializer.Serialize(person,typeInfo);// Fail: regular-case is serialized instead (because fast-path is taken; metadata should be used instead since we have it).JsonTestHelper.AssertJsonEqual(@"{""FIRSTNAME"":""Jane"",""LASTNAME"":""Doe""}",json);Personperson=JsonSerializer.Deserialize(json,typeInfo);Assert.Equal("Jane",person.FirstName);Assert.Equal("Doe",person.LastName);There was a problem hiding this comment.
Oh wow. Fundamentally I think the problem here is that static metadata is mutable even though these should be locked for modification. We should try to service this.
cc @krwq
Uh oh!
There was an error while loading. Please reload this page.
layomia
commented
Oct 6, 2022
Closing this PR - the most succinct way to customize src-gen'd contracts IMO is by compisition, where a |
Attempting to understand/codify behavior expectations when users attempt to modify contracts generated by the source generator.