bchavez / bchavez/Bogus

Bogus.Tools.Analyzer generates 'default' RuleFor() for nullable system types rather than using the randomizer and .OrNull()

Open
#561 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
9.7k
Forks
538
PR merge metrics
No merged PRs in 30d

Description

### 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 a nullable system type is specified, the output RuleFor() is 'default'.

Perhaps this is by design, but it seems one would generally want to have the full spectrum of possible results for a nullable system type.

### LINQPad Example or Reproduction Steps

```
public class NullableProperties
{
public string FirstName { get; set; } = string.Empty;
public decimal? NullableDecimal { get; set; }
public int? NullableInt { get; set; }
public int JustInt { get; set; }
}

static void BogusAnalyzer()
{
var g = new Faker()
.RuleFor(n => n.FirstName, f => f.Lorem.Word())
.RuleFor(n => n.NullableDecimal, f => default) // <-- Would expect this to be f.Random.Decimal().OrNull(f)
.RuleFor(n => n.NullableInt, f => default) // <-- Would expect this to be f.Random.Int().OrNull(f)
.RuleFor(n => n.JustInt, f => f.Random.Int());

var o = g.Generate();
}

```

### Expected Behavior

I'd expect that the generated RuleFor would have the underlying type and use the OrNull() from Bogus, such as:

```
var g = new Faker()
.RuleFor(n => n.FirstName, f => f.Lorem.Word())
.RuleFor(n => n.NullableDecimal, f => f.Random.Decimal().OrNull(f))
.RuleFor(n => n.NullableInt, f => f.Random.Int().OrNull(f))
.RuleFor(n => n.JustInt, f => f.Random.Int());
```

### Actual Behavior

default is output in the RuleFor()

### Known Workarounds

_No response_

### Could you help with a pull-request?

No

Contributor guide

Open the contributing guide

Research direction

Start with the Bogus.Tools.Analyzer behavior shown in the LINQPad reproduction, focusing on nullable system types and the generated RuleFor() expressions. Reproduce the decimal? and int? cases, then verify that generated rules use the underlying randomizer with OrNull() rather than default.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.