RicoSuter/NSwag · error · ArgumentNullException
key
Error message
key
What it means
ObservableDictionary.Remove throws ArgumentNullException when the key argument is null. Null is never a valid dictionary key here, so the method fails fast before attempting the underlying Dictionary.Remove and collection-change notification.
Solutions
- Ensure the key variable is non-null before calling Remove
- Check ContainsKey with a non-null key first; skip removal if null
- Use an early return: if (key == null) return false;
Example fix
// before
dict.Remove(key);
// after
if (key != null && dict.ContainsKey(key))
{
dict.Remove(key);
} Defensive patterns
Strategy: validation
Validate before calling
if (key != null) dict.Remove(key);
Type guard
bool canRemove = key is not null;
Prevention
- Never store null as a candidate key; use string.Empty or a sentinel instead
- Check ContainsKey before removal to avoid no-op or null keys
When it happens
Trigger: Calling Remove(null) on an ObservableDictionary, e.g. removing a schema or parameter keyed by a variable that was never assigned.
Common situations: Removing an OpenAPI component by a name that came from optional config or a failed lookup that returned null.
Related errors
- items
- An item with the same key has already been added.
- document
- globalScopeNames
- This UI does not support multiple documents per UI: Do not…
AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14).
Data as JSON: /api/errors/a544e0f84f5aff68.
Report an issue: GitHub.
Appendix: source
Thrown at src/NSwag.Core/Collections/ObservableDictionary.cs:217
public ICollection<TKey> Keys => _dictionary.Keys;
internal Dictionary<TKey, TValue>.KeyCollection KeyCollection => _dictionary.Keys;
ICollection IDictionary.Values => _dictionary.Values;
ICollection IDictionary.Keys => _dictionary.Keys;
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => Values;
/// <summary>Removes the specified key.</summary>
/// <param name="key">The key.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">key</exception>
public virtual bool Remove(TKey key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
var removed = _dictionary.Remove(key);
if (removed)
{
OnCollectionChanged();
}
//OnCollectionChanged(NotifyCollectionChangedAction.Remove, new KeyValuePair<TKey, TValue>(key, value));
return removed;
}
/// <summary>Tries the get value.</summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public bool TryGetValue(TKey key, out TValue value)
{
return _dictionary.TryGetValue(key, out value);View on GitHub (pinned to 63daf8fcc3)