Skip to content

[GenerateOneOf] Allows Null Values Which Leads to NullReferenceExceptions #200

@JCKortlang

Description

@JCKortlang

Issue

I am encountering NullReferenceExceptions when using the .Match(...) method of [GeneratedOneOf] reference classes. It appears to be an issue with the manner that the generated class handles value assignment. See below.

Discusison

  • I think that attempting to assign null values should not be possible. Consider configuring the nullability via GenerateOneOf
  • If this behavior is expected, a compiler warning / error would be ideal.

Repro

public class SomeClass1()
{

};

public class SomeClass2()
{

};

[GenerateOneOf]
public partial class GeneratedOneOf : OneOfBase<SomeClass1, SomeClass2>;

[TestFixture]
public class FooTest
{

    [Test]
    public void NullTest()
    {
        //As expected
        GeneratedOneOf? generated = this.GetNullGeneratedOneOf();
        Assert.That(generated, Is.Null);

        //I expect the returned null to result in the GeneratedOneOf reference to be null rather than setting the value to null.
        GeneratedOneOf? someClass1 = this.GetNullSomeClass1();

        //This fails. `.Match` is then invoked with a null value.
        Assert.That(someClass1, Is.Null); 

        GeneratedOneOf? someClass2 = this.GetNullSomeClass2();
        Assert.That(someClass2, Is.Null);
    }

    private GeneratedOneOf? GetNullGeneratedOneOf() => null;

    private SomeClass1? GetNullSomeClass1() => null;

    private SomeClass2? GetNullSomeClass2() => null;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions