Bogus icon indicating copy to clipboard operation
Bogus copied to clipboard

Bogus.Tools.Analyzer detects read only properties as missing and generates RuleFor() for them

Open mpickers opened this issue 1 year ago • 1 comments

Bogus NuGet Package

35.6.0

.NET Version

net 6

Visual Studio Version

17.10.3

What operating system are you using?

Windows

What locale are you using with Bogus?

en_US

Problem Description

When using the Bogus Premium Analyzer, the Analyzer doesn't ignore read-only properties in either base class or top-level classes.

I believe it should really ignore these for both the analyzer and the code gen.

image

LINQPad Example or Reproduction Steps

public class TheBase
{
   public bool ReadOnlyProp => GetValue();

   public bool AnotherReadOnlyProp { get { return GetValue(); } }

   private bool GetValue()
   {
      return true;
   }
}

public class TheClass : TheBase
{
   public string MyProperty { get; set; } = string.Empty;

   public bool TopLevelReadOnlyProp { get { return true; } }
}

var f = new Faker<TheClass>();

Expected Behavior

The Analyzer should ignore any read only fields and the code generator should not output any rules for these read only properties.

Actual Behavior

Code is being generated for readonly fields.

      var f = new Faker<TheClass>()
         .RuleFor(t => t.MyProperty, f => f.Lorem.Word())
         .RuleFor(t => t.TopLevelReadOnlyProp, f => f.Random.Bool()) // Shouldn't be here
         .RuleFor(t => t.ReadOnlyProp, f => f.Random.Bool())  // Shouldn't be here
         .RuleFor(t => t.AnotherReadOnlyProp, f => f.Random.Bool());  // Shouldn't be here

Known Workarounds

No response

Could you help with a pull-request?

No

mpickers avatar Aug 26 '24 07:08 mpickers

Hi @mpickers, thank you for reporting the issue. I think I can write a unit test for the Bogus.Tools.Analayzer to cover this case. I'll try to have a fix by the end of this weekend.

bchavez avatar Aug 27 '24 00:08 bchavez

Hi @mpickers , this issue should be fixed in Bogus.Tools.Analyzer v35.6.0.11 or later:

  • https://www.nuget.org/packages/Bogus.Tools.Analyzer/35.6.0.11

bchavez avatar Sep 02 '24 22:09 bchavez

Awesome, thank you! Works as expected now.

mpickers avatar Sep 03 '24 00:09 mpickers