[C#] IList properties are uninitialized by default
There are two common patterns for initializing IList properties:
scoringProfile.Functions = new[] { new TagScoringFunction(), ... };
and
scoringProfile.Functions.Add(new TagScoringFunction());
...
With AutoRest generated model classes, the latter pattern breaks with NullReferenceException if you forget to initialize the IList property. This catches some customers by surprise.
Leaving IList properties uninitialized optimizes for the first pattern, since there's no extra collection being allocated and then discarded. However, there are ways around this (older Azure SDKs based on Hyak used a special LazyList type for this purpose).
Any thoughts on changing this behavior so AutoRest-generated C# code follows the principle of least surprise?
I agree with this - it's definitely best practice to make sure the list is initialized by the time the model constructor is finished (otherwise the model isn't really ready to use). Whether we port over LazyList or just initialize with a new List, we should definitely address it
We'll create a new list by default. no sense over thinking it too much
In terms of being consistent:
Shouldn't we then also initialize IDictionary properties and generally any complex properties?
Take the Swagger
swagger: '2.0'
info:
version: 1.0.0
title: Simple API
definitions:
init:
properties:
prop1:
type: array
items:
type: string
prop2:
type: object
additionalProperties:
type: integer
prop3:
type: object
properties:
dummy:
type: integer
additionalProperties:
type: integer
which generates (modulo comments)
public partial class Init
{
public Init() { }
public Init(System.Collections.Generic.IList<string> prop1 = default(System.Collections.Generic.IList<string>), System.Collections.Generic.IDictionary<string, int?> prop2 = default(System.Collections.Generic.IDictionary<string, int?>), InitProp3 prop3 = default(InitProp3))
{
Prop1 = prop1;
Prop2 = prop2;
Prop3 = prop3;
}
public System.Collections.Generic.IList<string> Prop1 { get; set; }
public System.Collections.Generic.IDictionary<string, int?> Prop2 { get; set; }
public InitProp3 Prop3 { get; set; }
}
Should we initialize every property that has a mutable (i.e. not string) reference type or is there something special about IList?
Yes. Absolutely.
New perspective: Maybe we should not initialize anything, as this would cause PATCH operations to always overwrite lists with empty-lists, etc.! In other words null should stay the default if nothing else is set, otherwise we ruin PATCH semantics.
Solutions:
- go lazy, i.e. first access to member initializes it (but that is odd, see below)
- do nothing, i.e. demand being explicit
Note that the lazy behavior is odd for PATCH writers for a number of reasons:
- Reading the property would magically let an empty list pop into existence - strange semantics ("don't read before you PATCH")
- experience when you want to
PATCHwith[]is inconsistent: Patching[1]is done by merely sayinglist.Add(1), but patching[]requires some explicit non-sense operation likelist.Clear()or an explicit overwrite withnew ...after all
@olydis Good point. I never ran into this situation because our service doesn't implement any PATCH operations.
Given that the PATCH operation is negative affected, we're going to close this, and leave the behavior as-is
👍