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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fixture cannot create Byte Array with RegularExpressionAttribute #1348

Open
2 tasks
RonSijm opened this issue May 23, 2022 · 2 comments
Open
2 tasks
Labels
bug data annotations An issue related to data annotations triage Something that's being investigated

Comments

@RonSijm
Copy link

RonSijm commented May 23, 2022

when a Byte Array has a RegularExpressionAttribute, the Fixture seems to treat it as string.

Scenario

public class TestFixtureCanCreateByteArray
{
    [Fact]
    public void CanCreateByteArray()
    {
        var fixture = new Fixture();
        var result = fixture.Create<TestClass>();
    }
}

public class TestClass
{
    [System.ComponentModel.DataAnnotations.RegularExpression(@"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$")]
    public byte[] invoice { get; set; }
}

Expected Behavior

An array of bytes is generated matching the regular expression

Actual Behavior

Inner exception messages:
	System.ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Byte[]'.

Tasks

  • Updated all AutoFixture and related libraries
  • Confirmed in support forums if behavior is expected

More Information

I encountered this after generating a client library based on a swagger for Hubspot.
Swagger API: https://api.hubspot.com/api-catalog-public/v1/apis/crm/v3/extensions/accounting line 1407

  "InvoicePdfResponse" : {
    "required" : [ "invoice" ],
    "type" : "object",
    "properties" : {
      "@result" : {
        "type" : "string",
        "description" : "Designates if the response is a success ('OK') or failure ('ERR').",
        "example" : "OK",
        "enum" : [ "OK", "ERR" ]
      },
      "invoice" : {
        "pattern" : "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$",
        "type" : "string",
        "description" : "The bytes of the invoice PDF.",
        "format" : "byte"
      }
    },
    "description" : "A response that contains the PDF of an invoice"
  },
@RonSijm RonSijm added the bug label May 23, 2022
@aivascu
Copy link
Member

aivascu commented May 25, 2022

@RonSijm thank you for raising this, however I could not find any information about the RegularExpression attribute supporting neither byte nor char arrays.
I wrote these tests to check and it doesn't seem that the attribute can support values of the required types. Perhaps you're using a framework that implements custom validation support?

[Theory]
[InlineData("[0-9]+", new [] {'0', '1'})]
[InlineData("[A-Z]+", new [] {'A', 'Z'})]
[InlineData("[a-z]+", new [] {'d', 'c'})]
public void AttributeValidatesByteArray(string pattern, char[] value)
{
    var attribute = new RegularExpressionAttribute(pattern);
    object bytes = value.Select(x => (byte)x).ToArray();
    Validator.ValidateValue(bytes, new ValidationContext(this, null, null), new[] { attribute });
}

[Theory]
[InlineData("[0-9]+", new [] {'0', '1'})]
[InlineData("[A-Z]+", new [] {'A', 'Z'})]
[InlineData("[a-z]+", new [] {'d', 'c'})]
public void AttributeValidatesCharArray(string pattern, char[] value)
{
    var attribute = new RegularExpressionAttribute(pattern);
    Validator.ValidateValue(value, new ValidationContext(this, null, null), new[] { attribute });
}

[Theory]
[InlineData("[0-9]+", "01")]
[InlineData("[A-Z]+", "AZ")]
[InlineData("[a-z]+", "dc")]
public void AttributeValidatesString(string pattern, string value)
{
    var attribute = new RegularExpressionAttribute(pattern);
    Validator.ValidateValue(value, new ValidationContext(this, null, null), new[] { attribute });
}

@aivascu aivascu added the triage Something that's being investigated label May 25, 2022
@RonSijm
Copy link
Author

RonSijm commented May 26, 2022

I'm not 100% sure what the attribute is supposed to do (As I mentioned, I'm implementing a client based on the hubspot swagger link that I provided, but besides that I'm not familiar with what the endpoint expects)

If I had to guess, maybe the string form of the byte array is supposed to match the regex

Perhaps you're using a framework that implements custom validation support?

I used NSwag toolchain v13.11.3.0 to generate the client library. I don't know if the attribute is actually going to be validated by anything. I guess NSwag just added it because the pattern is specified in the swagger file.

In msdn: https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.regularexpressionattribute?view=net-6.0 their example seems to show:

[RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Characters are not allowed.")]
public object FirstName;

Using it on an object, and not specifically a string (Which I'm also not sure of of how that's supposed to work...

Anyways, if it's not valid, I suppose Fixture should just ignore it, instead of throw

@aivascu aivascu added xUnit Any issues related to xUnit data annotations An issue related to data annotations triage Something that's being investigated and removed triage Something that's being investigated xUnit Any issues related to xUnit labels Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug data annotations An issue related to data annotations triage Something that's being investigated
Projects
None yet
Development

No branches or pull requests

2 participants