bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.66k stars 495 forks source link

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

Closed mpickers closed 1 week ago

mpickers commented 2 weeks ago

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

bchavez commented 2 weeks ago

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 commented 1 week ago

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

mpickers commented 1 week ago

Awesome, thank you! Works as expected now.