autorest icon indicating copy to clipboard operation
autorest copied to clipboard

[C#] IList properties are uninitialized by default

Open bitsforthought opened this issue 10 years ago • 8 comments

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?

bitsforthought avatar Sep 17 '16 17:09 bitsforthought

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

tbombach avatar Sep 20 '16 00:09 tbombach

We'll create a new list by default. no sense over thinking it too much

fearthecowboy avatar Nov 09 '16 00:11 fearthecowboy

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?

olydis avatar Nov 09 '16 02:11 olydis

Yes. Absolutely.

fearthecowboy avatar Nov 09 '16 02:11 fearthecowboy

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 PATCH with [] is inconsistent: Patching [1] is done by merely saying list.Add(1), but patching [] requires some explicit non-sense operation like list.Clear() or an explicit overwrite with new ... after all

olydis avatar Nov 09 '16 21:11 olydis

@olydis Good point. I never ran into this situation because our service doesn't implement any PATCH operations.

bitsforthought avatar Nov 09 '16 23:11 bitsforthought

Given that the PATCH operation is negative affected, we're going to close this, and leave the behavior as-is

fearthecowboy avatar Nov 10 '16 19:11 fearthecowboy

👍

bitsforthought avatar Nov 10 '16 22:11 bitsforthought