Skip to content
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

JSchemaGenerationProvider for MemberProperty not executed if annotation exists on metadatatype buddy class #327

Open
JeffBarnard opened this issue Oct 12, 2023 · 2 comments

Comments

@JeffBarnard
Copy link

I want to use a custom JSchemaGenerationProvider to add some properties that aren't supported out of the box, such as "readOnly".

I am using ef-core database first tools to generate portable POCO classes.
I am then extending these classes by adding 'buddy classes' to annotate them for the validation rules.

    public partial class Model 
    {
        public string Name { get; set; }
    }

    [MetadataType(typeof(IModelMeta))]   
    public partial class Model : IModelMeta
    {
       
    }

    public interface IModelMeta
    {
        [JSchemaGenerationProvider(typeof(CustomSchemaProvider))]        
        [ReadOnly(true)] // "readOnly": true   
        string Name { get; set; }
    }

In the above example, my CustomSchemaProvider is not executed.

Even if I do not use an interface and use a local class instead, it still does not work:

[MetadataType(typeof(ModelMeta))]   
public partial class Model
{
    public class ModelMeta
    {
        [JSchemaGenerationProvider(typeof(CustomSchemaProvider))]
        [ReadOnly(true)] // "readOnly": true   
        public string Name { get; set; }

It only works if I move the annotation to the origin class definition, but I cannot do this because my base partial POCO classes are generated by tooling (efpt).

@JeffBarnard JeffBarnard changed the title JSchemaGenerationProvider for MemberProperty not executed if annotation exist on metadatatype buddy class JSchemaGenerationProvider for MemberProperty not executed if annotation exists on metadatatype buddy class Oct 12, 2023
@JeffBarnard
Copy link
Author

JeffBarnard commented Oct 12, 2023

I have other code that detects metadata attributes and it basically looks like this:

var metadataType = type.GetCustomAttributes(typeof(MetadataTypeAttribute), true)
 .OfType<MetadataTypeAttribute>().FirstOrDefault();

if (metadataType != null) 
{
    PropertyInfo[] properties = metadataType.MetadataClassType.GetProperties(BindingFlags.Public | ...);

Perhaps there is a workaround, a method that I could override to fetch the metadata properties, then I could apply a similar technique there.

@JeffBarnard
Copy link
Author

When using the .net ValidationContext to validate our objects, the metadata classes need to be registered in order for that api to detect them. I don't know why they require us to do this, but perhaps this is a useful reference:

foreach (MetadataTypeAttribute attrib in type.GetCustomAttributes(typeof(MetadataTypeAttribute), true))
{
    System.ComponentModel.TypeDescriptor.AddProviderTransparent(
        new AssociatedMetadataTypeTypeDescriptionProvider(type, attrib.MetadataClassType), type);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant