-
Notifications
You must be signed in to change notification settings - Fork 6k
Contract model example is incorrect - Field DerivedTypes cannot be set #45871
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
Comments
NOTE - looks like the VB example is correct - it's the C# example that's incorrect. |
@kezenator The example is actually correct and compiles without warning. Even though the collection initializer syntax makes it look like the property is being set, it's not actually being set. The |
Yes. I'm sure you are correct. The example you have shown (with a fixed list of Surely one of the key reasons to use the contract method is when the list of derived types is not known at compile time - e.g. as you actually say:
In this case - you cannot use the list initialization syntax, and need to use the Again - just to repeat - your example code IS NOT APPROPRIATE for the use case you explicitly mention in that section. |
The use case talks about the ability to add attribute annotations, and whether you call Add() or use a collection initializer, you still need to know the type names at compile time, so I'm not sure I follow your logic. @IEvangelist or @eiriktsarpalis do you want to weigh in here? |
Hi Genevieve, The problem with using the
As per the quote I included above - this is not practical in a number of cases - including when you are writing a library and you want a third-party to be able to register an additional type - a third-party user "can't" change the attributes on your base class. In the above example - imagine that class Extending the example above - here is a rough outline of the code I ended up with in my project.
Changing to use a collection initializer spread element might also be a good option.
|
Hi @kezenator — I follow what you're saying, and I understand the limitation you're calling attention to with the attributes in the base class not possibly knowing about external assemblies that aim to extend the base. That's what the contract model is for though, and that code example is correct. I know that the syntax can be misleading as it resembles a property assignment although it's a collection initializer. The VB example is identical in terms of functionality. Use the spread operator as you suggest, results in a compiler error as you cannot assign to a read-only If I understand your ask here, you'd prefer to see this code—is that right? public class PolymorphicTypeResolver : DefaultJsonTypeInfoResolver
{
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
{
JsonTypeInfo jsonTypeInfo = base.GetTypeInfo(type, options);
Type basePointType = typeof(BasePoint);
if (jsonTypeInfo.Type == basePointType)
{
jsonTypeInfo.PolymorphismOptions = new JsonPolymorphismOptions
{
TypeDiscriminatorPropertyName = "$point-type",
IgnoreUnrecognizedTypeDiscriminators = true,
UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FailSerialization
- DerivedTypes =
- {
- new JsonDerivedType(typeof(ThreeDimensionalPoint), "3d"),
- new JsonDerivedType(typeof(FourDimensionalPoint), "4d")
- }
};
+ jsonTypeInfo.PolymorphismOptions.DerivedTypes.Add(
+ new JsonDerivedType(typeof(ThreeDimensionalPoint), "3d"));
+ jsonTypeInfo.PolymorphismOptions.DerivedTypes.Add(
+ new JsonDerivedType(typeof(FourDimensionalPoint), "4d"));
}
return jsonTypeInfo;
}
} I respectfully disagree, as this suggestion is more verbose (and not necessary a major violation of LoD, but certainly leaning in that direction). I'm not sure if this is helpful, but I'm happy to continue to help where possible. Thank you for posting this issue, much appreciated. |
Type of issue
Code doesn't work
Description
The example in the "Configure polymorphism with the contract model" section is incorrect.
It suggest that you need to set the "DerivedTypes" property initialization - but property JsonPolymorphismOptions.DerivedTypes is get only.
Instead of:
It should be:
Page URL
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism
Content source URL
https://github.com/dotnet/docs/blob/main/docs/standard/serialization/system-text-json/polymorphism.md
Document Version Independent Id
17511810-6572-ad88-6eaf-4f9bded720b7
Platform Id
c3675880-a4e2-ce92-63f1-ddab72bb3c4c
Article author
@gewarren
Metadata
Related Issues
The text was updated successfully, but these errors were encountered: