-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Length] Attribute isn't supported #2756
Comments
It turns out that the Project has not been updated for a year (at this point in time). This means that the current version for the project is .Net 7.0, while the |
If You stumbled upon this Issue and the Project hasn't yet migrated to .Net 8, then you can make use of the following SchemaFilter in Your Solution: internal sealed class LengthAttributeSupportSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.MemberInfo is null)
{
return;
}
var lengthAttributes = context.MemberInfo.GetInlineAndMetadataAttributes().OfType<LengthAttribute>();
foreach (var lengthAttribute in lengthAttributes)
{
ApplyLengthAttribute(schema, lengthAttribute);
}
}
private void ApplyLengthAttribute(OpenApiSchema schema, LengthAttribute lengthAttribute)
{
if (schema.Type == "array")
{
schema.MinItems = schema.MinItems is null
? lengthAttribute.MinimumLength
: Math.Max(schema.MinItems.Value, lengthAttribute.MinimumLength);
schema.MaxItems = schema.MaxItems is null
? lengthAttribute.MaximumLength
: Math.Min(schema.MaxItems.Value, lengthAttribute.MaximumLength);
}
else
{
schema.MinLength = schema.MinLength is null
? lengthAttribute.MinimumLength
: Math.Max(schema.MinLength.Value, lengthAttribute.MinimumLength);
schema.MaxLength = schema.MaxLength is null
? lengthAttribute.MaximumLength
: Math.Min(schema.MaxLength.Value, lengthAttribute.MaximumLength);
}
}
} Warning I needed |
I'm not closing this Issue, since the solution I've provided is temporary. I hope that the Project'll migrate to the .Net 8 eventually and we'll be able to add support for the |
ITNOA I need this feature too. |
This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made. |
I wanted to help you out by implementing this, but I need .NET 8 support for this... Guess I will start with reviewing the .NET 8 PR instead :) |
@satma0745 @dnperfors As you can see, .NET 8 added in PR #2799 and merge it. So do you can start implementing this feature? thanks |
@soroshsabz |
@soroshsabz Opened a PR: #2882 |
Thanks for adding the support for this - the change is available in Swashbuckle.AspNetCore 6.6.2. |
This issue is a feature request for a support of the
[Length]
attribute. Judging by the example in the Contribution guide, this issue falls into the Feature Requests category.The
System.ComponentModel.DataAnnotations
namespace has 3 (actually even more, but it is not within the scope of this Issue) Attributes responsible for the string length validation:[MinLength(length)]
- MS Docs[MaxLength(length)]
- MS Docs[Length(minimumLength, maximumLength)]
- MS DocsAnd while the first two are supported, the latter isn't.
Suppose, we have the following DTO definition:
Having such model SwaggerGen produces the following
OpenApi
specification:Where the
prop1
field has bothmaxLength
andminLength
values specified, but theprop2
field doesn't have them at all.The text was updated successfully, but these errors were encountered: