Skip to content

Commit

Permalink
Code Analysis - CA2227
Browse files Browse the repository at this point in the history
Collection properties should be read only
https://docs.microsoft.com/en-gb/visualstudio/code-quality/ca2227

Suppress FxCopAnalyzers warning CA2227 as System.Text.Json.JsonSerializer.Deserialize in .NET Core 3.1 cannot deserialise to read-only properties.

There are two related issues that will allow System.Text.Json.JsonSerializer.Deserialize to deserialise to read-only properties in .NET Core 5.0:
1) Pull Request that includes support for non-public accessors: dotnet/runtime#34675
2) Issue regarding, among other things, adding to collections during deserialisation if the collection property has no setter: dotnet/runtime#30258
  • Loading branch information
watfordjc committed Jul 8, 2020
1 parent 5a1c2e1 commit fb6057d
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/Events/SceneItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class SourceOrderChanged : EventBase
[JsonPropertyName("scene-name")]
public string SceneName { get; set; }
[JsonPropertyName("scene-items")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.ObsEventSceneItem> SceneItems { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class SceneItemAdded : EventBase
Expand Down
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/Events/Scenes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class SwitchScenes : EventBase
[JsonPropertyName("scene-name")]
public string SceneName { get; set; }
[JsonPropertyName("sources")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.SceneItem> Sources { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class ScenesChanged : EventBase
Expand Down
4 changes: 4 additions & 0 deletions OBSWebSocketLibrary/Models/Events/Sources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public class SourceAudioMixersChanged : EventBase
[JsonPropertyName("sourceName")]
public string SourceName { get; set; }
[JsonPropertyName("mixers")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.ObsMixer> Mixers { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
[JsonPropertyName("hexMixersValue")]
public string HexMixersValue { get; set; }
}
Expand Down Expand Up @@ -110,6 +112,8 @@ public class SourceFiltersReordered : EventBase
[JsonPropertyName("sourceName")]
public string SourceName { get; set; }
[JsonPropertyName("filters")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.ObsFilter> Filters { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}
}
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/Events/StudioMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class PreviewSceneChanged : EventBase
[JsonPropertyName("scene-name")]
public string SceneName { get; set; }
[JsonPropertyName("GetCurrentScene")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.SceneItem> GetCurrentScene { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class StudioModeSwitched : EventBase
Expand Down
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/RequestReplies/Outputs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace OBSWebSocketLibrary.Models.RequestReplies
public class ListOutputs : RequestReplyBase
{
[JsonPropertyName("outputs")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.Output> Outputs { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class GetOutputInfo : RequestReplyBase
Expand Down
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/RequestReplies/Profiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class GetCurrentProfile : RequestReplyBase
public class ListProfiles : RequestReplyBase
{
[JsonPropertyName("profiles")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<OBSWebSocketLibrary.Models.TypeDefs.ObsProfile> Profiles { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}
}
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/RequestReplies/SceneCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class GetCurrentSceneCollection : RequestReplyBase
public class ListSceneCollections : RequestReplyBase
{
[JsonPropertyName("scene-collections")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<string> SceneCollections { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}
}
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/RequestReplies/SceneItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public class GetSceneItemProperties : RequestReplyBase
[JsonPropertyName("parentGroupName")]
public string ParentGroupName { get; set; }
[JsonPropertyName("groupChildren")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.SceneItemTransform> GroupChildren { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class SetSceneItemProperties : RequestReplyBase
Expand Down
5 changes: 5 additions & 0 deletions OBSWebSocketLibrary/Models/RequestReplies/Scenes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Text.Json.Serialization;

Expand All @@ -14,15 +15,19 @@ public class GetCurrentScene : RequestReplyBase
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("sources")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.SceneItem> Sources { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class GetSceneList : RequestReplyBase
{
[JsonPropertyName("current-scene")]
public string CurrentScene { get; set; }
[JsonPropertyName("scenes")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.Scene> Scenes { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class ReorderSceneItems : RequestReplyBase
Expand Down
6 changes: 6 additions & 0 deletions OBSWebSocketLibrary/Models/RequestReplies/Sources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ namespace OBSWebSocketLibrary.Models.RequestReplies
public class GetSourcesList : RequestReplyBase
{
[JsonPropertyName("sources")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.ObsReplySource> Sources { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class GetSourceTypesList : RequestReplyBase
{
[JsonPropertyName("types")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.ObsReplyType> Types { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class GetVolume : RequestReplyBase
Expand Down Expand Up @@ -217,7 +221,9 @@ public class GetSpecialSources : RequestReplyBase
public class GetSourceFilters : RequestReplyBase
{
[JsonPropertyName("filters")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.ObsReplyFilter> Filters { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class GetSourceFilterInfo : RequestReplyBase
Expand Down
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/RequestReplies/StudioMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class GetPreviewScene : RequestReplyBase
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("sources")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.SceneItem> Sources { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class SetPreviewScene : RequestReplyBase
Expand Down
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/RequestReplies/Transitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class GetTransitionList : RequestReplyBase
[JsonPropertyName("current-transition")]
public string CurrentTransition { get; set; }
[JsonPropertyName("transitions")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.ObsTransitionName> Transitions { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class GetCurrentTransition : RequestReplyBase
Expand Down
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/Requests/Scenes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public class ReorderSceneItems : RequestBase
[JsonPropertyName("scene")]
public string Scene { get; set; }
[JsonPropertyName("items")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<TypeDefs.ItemObject> Items { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class SetSceneTransitionOverride : RequestBase
Expand Down
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/TypeDefs/Scene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class Scene
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("sources")]
#pragma warning disable CA2227 // Collection properties should be read only
public ObservableCollection<SceneItem> Sources { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}
}
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/TypeDefs/SceneItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public double Volume
[JsonPropertyName("parentGroupName")]
public string ParentGroupName { get; set; }
[JsonPropertyName("groupChildren")]
#pragma warning disable CA2227 // Collection properties should be read only
public ObservableCollection<SceneItem> GroupChildren { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
[JsonIgnore]
public SourceTypes.BaseType Source
{
Expand Down
2 changes: 2 additions & 0 deletions OBSWebSocketLibrary/Models/TypeDefs/SceneItemTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public class SceneItemTransform
[JsonPropertyName("parentGroupName")]
public string ParentGroupName { get; set; }
[JsonPropertyName("groupChildren")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<SceneItemTransform> GroupChildren { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only

public static implicit operator SceneItemTransform(RequestReplies.GetSceneItemProperties v)
{
Expand Down
4 changes: 4 additions & 0 deletions OBSWebSocketLibrary/Models/TypeDefs/SourceTypes/BaseType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public string MonitorType
}
}
[JsonIgnore]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<ObsMixer> Mixers
#pragma warning restore CA2227 // Collection properties should be read only
{
get { return mixers; }
set
Expand All @@ -98,7 +100,9 @@ public string HexMixersValue
}
}
[JsonIgnore]
#pragma warning disable CA2227 // Collection properties should be read only
public ObservableCollection<FilterTypes.BaseFilter> Filters { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only

[JsonIgnore]
public TypeDefs.ObsReplyType Type { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions OBSWebSocketLibrary/Models/TypeDefs/SourceTypes/Composite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class Scene : BaseType
[JsonPropertyName("id_counter")]
public int IdCounter { get; set; }
[JsonPropertyName("items")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<ObsSceneItem> Items { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}

public class Group : BaseType
Expand All @@ -27,6 +29,8 @@ public class Group : BaseType
[JsonPropertyName("id_counter")]
public int IdCounter { get; set; }
[JsonPropertyName("items")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<ObsSceneItem> Items { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public bool HasVideoInterface
get { return !String.IsNullOrEmpty(VideoDeviceId); }
}

#pragma warning disable CA2227 // Collection properties should be read only
public IList<string> FilePaths
#pragma warning restore CA2227 // Collection properties should be read only
{
get { return filePaths; }
set
Expand All @@ -103,7 +105,9 @@ public bool HasFiles
get { return FilePaths != null && FilePaths.Count != 0; }
}

#pragma warning disable CA2227 // Collection properties should be read only
public IList<string> Uris
#pragma warning restore CA2227 // Collection properties should be read only
{
get { return uris; }
set
Expand Down
4 changes: 4 additions & 0 deletions OBSWebSocketLibrary/Models/TypeDefs/SourceTypes/Video.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public class ColorSourceV2 : BaseType
public class Slideshow : BaseType
{
[JsonPropertyName("files")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<ObsFile> Files { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
[JsonPropertyName("playback_behavior")]
public string PlaybackBehavior { get; set; }
[JsonPropertyName("slide_time")]
Expand Down Expand Up @@ -172,7 +174,9 @@ public class VlcSource : BaseType
[JsonPropertyName("playback_behavior")]
public string PlaybackBehavior { get; set; }
[JsonPropertyName("playlist")]
#pragma warning disable CA2227 // Collection properties should be read only
public IList<VlcPlaylistItem> Playlist { get; set; }
#pragma warning restore CA2227 // Collection properties should be read only
[JsonPropertyName("shuffle")]
public bool Shuffle { get; set; }
[JsonPropertyName("subtitle")]
Expand Down

0 comments on commit fb6057d

Please sign in to comment.