Skip to content

AddObjectStatic throws on any null property value (inconsistent with AddObject) #2400

Closed

Description

@Bafyn

Describe the bug

RestRequest.AddObjectStatic(obj) throws when the object has any property whose value is null, instead of skipping that property. The reflection-based AddObject(obj) skips null properties, so the two APIs behave differently.

To Reproduce

using RestSharp;

public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}

// Name is left null
var request = new RestRequest("resource").AddObjectStatic(new Person { Age = 30 });

Building the request throws immediately (before any HTTP call is made).

Expected behavior

null properties are skipped and the request is built from the non-null ones only. The snippet above should produce a single Age=30 parameter and not throw.

Stack trace

System.NullReferenceException: Object reference not set to an instance of an object.
   at RestSharp.PropertyCache`1.Populator.GetStringValue(IConvertible convertible) in RestSharp/Request/PropertyCache.Populator.cs:line 385
   at RestSharp.PropertyCache`1.Populator.Populate(IConvertible convertible, RequestProperty requestProperty, ICollection`1 parameters) in RestSharp/Request/PropertyCache.Populator.cs:line 202
   at RestSharp.PropertyCache`1.Populator.<>c__DisplayClass8_0.<GetPopulate>b__0(T model, ICollection`1 parameters) in RestSharp/Request/PropertyCache.Populator.cs:line 84
   at RestSharp.PropertyCache`1.Populator.Populate(T entity, ICollection`1 parameters) in RestSharp/Request/PropertyCache.Populator.cs:line 47
   at RestSharp.PropertyCache`1.GetParameters(T entity, IEnumerable`1 populators) in RestSharp/Request/PropertyCache.cs:line 70
   at RestSharp.PropertyCache`1.GetParameters(T entity) in RestSharp/Request/PropertyCache.cs:line 61
   at RestSharp.RestRequestExtensions.AddObjectStatic[T](RestRequest request, T obj) in RestSharp/Request/RestRequestExtensions.Object.cs:line 63

Desktop:

  • OS: Windows 11
  • .NET version: .NET 8; affects all target frameworks
  • Version: current dev (114.0.0)

Activity

  1. added a commit that references this issue on Sep 2, 2026
    2370d2e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions