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

MaxLengthAttributeEdmPropertyConvention to allow inheritance from MaxLengthAttribute #44

Open
sherlock1982 opened this issue Oct 21, 2024 · 1 comment

Comments

@sherlock1982
Copy link

sherlock1982 commented Oct 21, 2024

MaxLengthAttributeEdmPropertyConvention takes into account MaxLength attribute and sets MaxLength property where appropriate.
I have a number of pattern-attributes like this (only an example):

class ShortLengthStringAttribute : MaxLengthAttribute
{
    public ShortLengthString() : base(50)
    {
        this.ErrorMessage = "Max length is 50 characters";
    }
};
class LongLengthStringAttribute : MaxLengthAttribute
{
    public LongLengthString() : base(255)
    {
        this.ErrorMessage = "Max length is 255 characters";
    }
};

In this case MaxLengthAttributeEdmPropertyConvention ignores my attributes. Is it possible to allow descendants of MaxLengthAttribute here?

    public MaxLengthAttributeEdmPropertyConvention()
        : base(attribute => attribute.GetType() == typeof(MaxLengthAttribute), allowMultiple: false)
    {
    }

Assemblies affected

Microsoft.OData.ModelBuilder.dll 2.0.0

Reproduce steps

Make a model with

 [ShortLengthString] string ShortString { get; set; }

Expected result

MaxLength should be automatically set to 50 in the model.

Actual result

It is not set

Additional detail

I know there are workarounds. For example I can do it manually. Or I can do something like this:

        builder.OnModelCreating = (builder) =>
        {
            foreach (var structType in builder.StructuralTypes)
            {
                foreach (var property in structType.Properties.OfType<LengthPropertyConfiguration>())
                {
                    var pp = property.PropertyInfo.GetCustomAttribute<MaxLengthAttribute>();
                    if (pp != null)
                    {
                        property.MaxLength = pp.Length;
                    }
                }
            }
        };

But I don't think it is natural.

@WanjohiSammy
Copy link

WanjohiSammy commented Oct 22, 2024

@sherlock1982 Kindly share a repro that we can use to investigate.

I am not able to reproduce this issue with the steps that you shared above.

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

No branches or pull requests

2 participants