Skip to content

MetadataTypeAttribute still not supported in wasm, workaround dotnet/aspnetcore/39938 is not working #119529

@tipoman9

Description

@tipoman9

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions