Fix #2400: AddObjectStatic throwing on null property values - #2401
Conversation
PR Summary by QodoFix AddObjectStatic null property handling to skip unset optional values
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
|
Code Review by Qodo
1. Getter evaluated twice
|
| (model, parameters) => { | ||
| if (getObject(model) is null) return; | ||
| populate(model, parameters); | ||
| } |
There was a problem hiding this comment.
1. Getter evaluated twice 🐞 Bug ☼ Reliability
Populator.From now calls the property getter once for the null-check and again inside the cached populate delegate, so non-null properties are evaluated twice. This can double side effects/expensive getters and can still throw if the value changes to null between the two reads.
Agent Prompt
### Issue description
`Populator.From(PropertyInfo)` wraps the generated `populate` delegate with a null-check by calling `getObject(model)` and then invoking `populate(model, parameters)`. However, `populate` itself calls `getObject(entity)` again (via `GetPopulate(getObject, property)`), so each non-null property getter is invoked twice.
This changes behavior for stateful/non-idempotent getters and adds avoidable overhead.
### Issue Context
The fix for #2400 is correct (skip null values), but it should not require re-reading the property.
### Fix Focus Areas
- src/RestSharp/Request/PropertyCache.Populator.cs[54-86]
- src/RestSharp/Request/PropertyCache.Populator.cs[122-146]
### Suggested approach
Refactor so the getter is evaluated once per property population:
- Capture `var value = getObject(model);`
- If `value is null`, return.
- Use `value` for the conversion/population path (e.g., introduce a `GetPopulate` variant that accepts the already-fetched `object value`, or build the population logic in `From` based on `property.PropertyType` but operating on the captured `value`).
This preserves the null-skip behavior while avoiding duplicate getter evaluation.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
There was a problem hiding this comment.
We technically can do this and pass the captured value to the populator instead of calculating it again from the model, but I'd like to get the confirmation that this is the way we want to go first @alexeyzimarev.
This change is going to affect a few other methods in this class



Description
Closes #2400
Problem
AddObjectStaticpicks a conversion path from each property's static type but dereferences the runtime value without a null check. A DTO with an unset optional field therefore crashes while the request is being built.Purpose
This pull request is a:
Checklist