Skip to content

Commit 63e37be

Browse files
Fix value connector nullability warnings
1 parent 053730c commit 63e37be

File tree

3 files changed

+277
-276
lines changed

3 files changed

+277
-276
lines changed

src/Umbraco.Deploy.Contrib/ValueConnectors/BlockEditorValueConnector.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public BlockEditorValueConnector(IContentTypeService contentTypeService, Lazy<Va
3939
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
4040
}
4141

42-
public override string ToArtifact(object value, IPropertyType propertyType, ICollection<ArtifactDependency> dependencies, IContextCache contextCache)
42+
public override string? ToArtifact(object? value, IPropertyType propertyType, ICollection<ArtifactDependency> dependencies, IContextCache contextCache)
4343
{
4444
_logger.LogDebug("Converting {PropertyType} to artifact.", propertyType.Alias);
4545
var valueAsString = value as string;
@@ -57,7 +57,7 @@ public override string ToArtifact(object value, IPropertyType propertyType, ICol
5757
return null;
5858
}
5959

60-
if (!valueAsString.TryParseJson(out BlockEditorValue blockEditorValue))
60+
if (!valueAsString.TryParseJson(out BlockEditorValue? blockEditorValue))
6161
{
6262
_logger.LogWarning("Value '{Value}' is not a JSON string. Skipping conversion to artifact.", valueAsString);
6363
return null;
@@ -84,22 +84,23 @@ public override string ToArtifact(object value, IPropertyType propertyType, ICol
8484
return contextCache.GetContentTypeByKey(_contentTypeService, keyAsGuid);
8585
});
8686

87-
//Ensure all of these content types are found
87+
// Ensure all of these content types are found
8888
if (allContentTypes.Values.Any(contentType => contentType == null))
8989
{
9090
throw new InvalidOperationException($"Could not resolve these content types for the Block Editor property: {string.Join(",", allContentTypes.Where(x => x.Value == null).Select(x => x.Key))}");
9191
}
9292

93-
//Ensure that these content types have dependencies added
94-
foreach (var contentType in allContentTypes.Values)
93+
// Ensure that these content types have dependencies added
94+
foreach (var contentType in allContentTypes.Values.WhereNotNull())
9595
{
9696
_logger.LogDebug("Adding dependency for content type {ContentType}.", contentType.Alias);
9797
dependencies.Add(new ArtifactDependency(contentType.GetUdi(), false, ArtifactDependencyMode.Match));
9898
}
9999

100100
foreach (var block in allBlocks)
101101
{
102-
var contentType = allContentTypes[block.ContentTypeKey];
102+
var contentType = allContentTypes[block.ContentTypeKey]
103+
?? throw new InvalidOperationException($"Could not find content type with alias '{block.ContentTypeKey}'.");
103104

104105
if (block.PropertyValues != null)
105106
{
@@ -119,7 +120,7 @@ public override string ToArtifact(object value, IPropertyType propertyType, ICol
119120

120121
// pass the value, property type and the dependencies collection to the connector to get a "artifact" value
121122
var innerValue = block.PropertyValues[key];
122-
object parsedValue = propertyValueConnector.ToArtifact(innerValue, innerPropertyType, dependencies, contextCache);
123+
object? parsedValue = propertyValueConnector.ToArtifact(innerValue, innerPropertyType, dependencies, contextCache);
123124

124125
_logger.LogDebug("Mapped {Key} value '{PropertyValue}' to '{ParsedValue}' using {PropertyValueConnectorType} for {PropertyType}.", key, block.PropertyValues[key], parsedValue, propertyValueConnector.GetType(), innerPropertyType.Alias);
125126

@@ -135,15 +136,15 @@ public override string ToArtifact(object value, IPropertyType propertyType, ICol
135136
return (string)value;
136137
}
137138

138-
public override object FromArtifact(string value, IPropertyType propertyType, object currentValue, IContextCache contextCache)
139+
public override object? FromArtifact(string? value, IPropertyType propertyType, object? currentValue, IContextCache contextCache)
139140
{
140141
_logger.LogDebug("Converting {PropertyType} from artifact.", propertyType.Alias);
141142
if (string.IsNullOrWhiteSpace(value))
142143
{
143144
return value;
144145
}
145146

146-
if (!value.TryParseJson(out BlockEditorValue blockEditorValue))
147+
if (!value.TryParseJson(out BlockEditorValue? blockEditorValue))
147148
{
148149
return value;
149150
}
@@ -167,15 +168,16 @@ public override object FromArtifact(string value, IPropertyType propertyType, ob
167168
return contextCache.GetContentTypeByKey(_contentTypeService, keyAsGuid);
168169
});
169170

170-
//Ensure all of these content types are found
171+
// Ensure all of these content types are found
171172
if (allContentTypes.Values.Any(contentType => contentType == null))
172173
{
173174
throw new InvalidOperationException($"Could not resolve these content types for the Block Editor property: {string.Join(",", allContentTypes.Where(x => x.Value == null).Select(x => x.Key))}");
174175
}
175176

176177
foreach (var block in allBlocks)
177178
{
178-
var contentType = allContentTypes[block.ContentTypeKey];
179+
var contentType = allContentTypes[block.ContentTypeKey]
180+
?? throw new InvalidOperationException($"Could not find content type with alias '{block.ContentTypeKey}'.");
179181

180182
if (block.PropertyValues != null)
181183
{
@@ -194,7 +196,6 @@ public override object FromArtifact(string value, IPropertyType propertyType, ob
194196
var propertyValueConnector = ValueConnectors.Get(innerPropertyType);
195197

196198
var innerValue = block.PropertyValues[key];
197-
198199
if (innerValue != null)
199200
{
200201
// pass the artifact value and property type to the connector to get a real value from the artifact
@@ -208,11 +209,13 @@ public override object FromArtifact(string value, IPropertyType propertyType, ob
208209
{
209210
block.PropertyValues[key] = convertedValue;
210211
}
212+
211213
_logger.LogDebug("Mapped {Key} value '{PropertyValue}' to '{ConvertedValue}' using {PropertyValueConnectorType} for {PropertyType}.", key, innerValue, convertedValue, propertyValueConnector.GetType(), innerPropertyType.Alias);
212214
}
213215
else
214216
{
215217
block.PropertyValues[key] = innerValue;
218+
216219
_logger.LogDebug("{Key} value was null. Setting value as null without conversion.", key);
217220
}
218221
}
@@ -271,35 +274,35 @@ public class BlockEditorValue
271274
/// JObject is fine for transferring this over.
272275
/// </summary>
273276
[JsonProperty("layout")]
274-
public JObject Layout { get; set; }
277+
public JObject? Layout { get; set; }
275278

276279
/// <summary>
277280
/// This contains all the blocks created in the block editor.
278281
/// </summary>
279282
[JsonProperty("contentData")]
280-
public IEnumerable<Block> Content { get; set; }
283+
public IEnumerable<Block> Content { get; set; } = Array.Empty<Block>();
281284

282285
/// <summary>
283286
/// This contains the settings associated with the block editor.
284287
/// </summary>
285288
[JsonProperty("settingsData")]
286-
public IEnumerable<Block> Settings { get; set; }
289+
public IEnumerable<Block> Settings { get; set; } = Array.Empty<Block>();
287290
}
288291

289292
public class Block
290293
{
291294
[JsonProperty("contentTypeKey")]
292-
public string ContentTypeKey { get; set; }
295+
public string ContentTypeKey { get; set; } = string.Empty;
293296

294297
[JsonProperty("udi")]
295-
public string Udi { get; set; }
298+
public string Udi { get; set; } = string.Empty;
296299

297300
/// <summary>
298301
/// This is the property values defined on the block.
299302
/// These can be anything so we have to use a dictionary to represent them and JsonExtensionData attribute ensures all otherwise unmapped properties are stored here.
300303
/// </summary>
301304
[JsonExtensionData]
302-
public IDictionary<string, object> PropertyValues { get; set; }
305+
public IDictionary<string, object?> PropertyValues { get; set; } = new Dictionary<string, object?>();
303306
}
304307
}
305308
}

0 commit comments

Comments
 (0)