Using Metadata attribute to add attribute to some property of the class does not work

71 Views Asked by At

i have auto generated class by protoc and i want to add JsonIgnore attribute for some of the properties of my class in the partial class because i don't want to reveal them in my REST API. I found this question and tried to implement according to the answer which talked about using Metadata attribute and creating appropriate class with the attribute .

So i created some WeatherForecast sample of ASP.NET app. I modified WeatherForecast class . So it looks as follows :

public partial class WeatherForecast
    {
        [JsonIgnore]
        public DateTime Date { get; set; }

        public int TemperatureC { get; set; }

        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

        public string? Summary { get; set; }
    }

I manually added JsonIgnore over Date property. After that i created partial class and metadata class for WeatherForecast class in different file.

    [MetadataType(typeof(WeatherForecastMetada))]
    public partial class WeatherForecast
    {
        
    }

    public partial class WeatherForecastMetada
    {
        [JsonIgnore]
        public string? Summary { get; set; }
    }

Unfortunately i don't see any impact of JsonIgnore on Summary property when i run the application. From other side i don't see Date property of the WeatherForecast as it supposed to be.

I tried to register WeatherForecast to TypeDescriptor but it doesn't help. What do i miss?

0

There are 0 best solutions below