-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Open
Labels
Milestone
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
It was stated that extending classes attributes would be supported by associating the metadata type to class in runtime like this.
TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Vehicle), typeof(VehicleRequirementsMetaData)), typeof(Vehicle));
dotnet/aspnetcore#39938 (comment)
The sample provided seems to work only for [Required] attribute.
When adding a different attribute , the latter gets ignored.
Expected Behavior
"VehileName": "My Truck",
"ManufID": "Cool Hybrids Inc"
Steps To Reproduce
public void TestExtendingMetadata() {
// Associate the metadata type VehicleRequirementsMetaData to the Vehicle class
TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Vehicle), typeof(VehicleRequirementsMetaData)), typeof(Vehicle));
// Create a valid vehicle object with both properties populated
var newTruck = new Vehicle {
Name = "My Truck",
Manufacturer = "Cool Hybrids Inc"
};
string jsonString = JsonSerializer.Serialize(newTruck);
Console.WriteLine(jsonString);
/*
{
"VehileName": "My Truck",
"Manufacturer": "Cool Hybrids Inc"
}
*/
}
public class Vehicle {
[JsonPropertyName("VehileName")]
public string Name { get; set; }
public string Manufacturer { get; set; }
}
public class VehicleRequirementsMetaData {
[JsonPropertyName("ManufID")]
public string Manufacturer { get; set; }
}
Exceptions (if any)
No response
.NET Version
7.0.400-preview.23330.10
Anything else?
No response
Reactions are currently unavailable