Skip to content

Commit bc2b857

Browse files
authored
Resolves erroneous consideration of nested content item templates as JSON (#56)
* Used new extension method to reliably check strings as JSON. * Updated JSON parsing and bumped version to 4.7.0-rc002.
1 parent bbca34c commit bc2b857

File tree

7 files changed

+46
-45
lines changed

7 files changed

+46
-45
lines changed

build/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Usage: on line 2 put the release version, on line 3 put the version comment (example: beta)
22
4.2.0
3-
rc001
3+
rc002

src/Umbraco.Deploy.Contrib.Connectors/GridCellValueConnectors/DocTypeGridEditorCellValueConnector.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Umbraco.Deploy.Connectors.GridCellValueConnectors;
1212
using Umbraco.Deploy.Connectors.ValueConnectors.Services;
1313
using Umbraco.Deploy.Core;
14+
using Umbraco.Deploy.Extensions;
1415

1516
namespace Umbraco.Deploy.Contrib.Connectors.GridCellValueConnectors
1617
{
@@ -87,7 +88,7 @@ public sealed override string GetValue(GridValue.GridControl gridControl, IColle
8788
_logger.Debug<DocTypeGridEditorCellValueConnector>($"GetValue - propertyTypeValue - {value}");
8889

8990
//properties like MUP / Nested Content are JSON, we need to convert to string for the conversion to artifact
90-
string parsedValue = propValueConnector.ToArtifact(IsJson(value) ? value.ToString() : value, propertyType, dependencies, contextCache);
91+
string parsedValue = propValueConnector.ToArtifact(value != null && value.ToString().TryParseJson(out JToken _) ? value.ToString() : value, propertyType, dependencies, contextCache);
9192

9293
_logger.Debug<DocTypeGridEditorCellValueConnector>($"GetValue - ParsedValue - {parsedValue}");
9394

@@ -149,15 +150,9 @@ public sealed override void SetValue(GridValue.GridControl gridControl, IContext
149150
var propValueConnector = ValueConnectors.Get(propertyType);
150151
var convertedValue = propValueConnector.FromArtifact(value.ToString(), propertyType, string.Empty, contextCache);
151152

152-
JToken jtokenValue = null;
153-
if (IsJson(convertedValue))
154-
{
155-
// test if the value is a json object (thus could be a nested complex editor)
156-
// if that's the case we'll need to add it as a json object instead of string to avoid it being escaped
157-
jtokenValue = GetJTokenValue(convertedValue);
158-
}
159-
160-
if (jtokenValue != null)
153+
// test if the value is a json object (thus could be a nested complex editor)
154+
// if that's the case we'll need to add it as a json object instead of string to avoid it being escaped
155+
if (convertedValue != null && convertedValue.ToString().TryParseJson(out JToken jtokenValue))
161156
{
162157
_logger.Debug<DocTypeGridEditorCellValueConnector>($"SetValue - jtokenValue - {jtokenValue}");
163158
docTypeGridEditorContent.Value[propertyType.Alias] = jtokenValue;
@@ -174,10 +169,6 @@ public sealed override void SetValue(GridValue.GridControl gridControl, IContext
174169
gridControl.Value = jtokenObj;
175170
}
176171

177-
private JToken GetJTokenValue(object value) => value != null && IsJson(value) ? JToken.Parse(value.ToString()) : null;
178-
179-
private bool IsJson(object value) => value != null && value.ToString().DetectIsJson();
180-
181172
private bool AddUdiDependency(ICollection<ArtifactDependency> dependencies, object value)
182173
{
183174
if (Udi.TryParse(value.ToString(), out var udi))

src/Umbraco.Deploy.Contrib.Connectors/Properties/VersionInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// </auto-generated>
99
//------------------------------------------------------------------------------
1010

11-
[assembly: System.Reflection.AssemblyInformationalVersion("4.2.0-rc001")]
11+
[assembly: System.Reflection.AssemblyInformationalVersion("4.2.0-rc002")]
1212
[assembly: System.Reflection.AssemblyVersion("4.2.0")]
1313

1414

src/Umbraco.Deploy.Contrib.Connectors/Umbraco.Deploy.Contrib.Connectors.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88
<ItemGroup>
99
<PackageReference Include="UmbracoDeploy.Core">
10-
<Version>4.7.0-rc001</Version>
10+
<Version>4.7.0-rc002</Version>
1111
</PackageReference>
1212
</ItemGroup>
1313
</Project>

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Umbraco.Deploy.Connectors.ValueConnectors;
1313
using Umbraco.Deploy.Connectors.ValueConnectors.Services;
1414
using Umbraco.Deploy.Core;
15+
using Umbraco.Deploy.Extensions;
1516

1617
namespace Umbraco.Deploy.Contrib.Connectors.ValueConnectors
1718
{
@@ -47,29 +48,27 @@ public BlockEditorValueConnector(IContentTypeService contentTypeService, Lazy<Va
4748
public override string ToArtifact(object value, PropertyType propertyType, ICollection<ArtifactDependency> dependencies, IContextCache contextCache)
4849
{
4950
_logger.Debug<BlockEditorValueConnector>("Converting {PropertyType} to artifact.", propertyType.Alias);
50-
var svalue = value as string;
51+
var valueAsString = value as string;
5152

5253
// nested values will arrive here as JObject - convert to string to enable reuse of same code as when non-nested.
5354
if (value is JObject)
5455
{
5556
_logger.Debug<BlockListValueConnector>("Value is a JObject - converting to string.");
56-
svalue = value.ToString();
57+
valueAsString = value.ToString();
5758
}
5859

59-
if (string.IsNullOrWhiteSpace(svalue))
60+
if (string.IsNullOrWhiteSpace(valueAsString))
6061
{
6162
_logger.Debug<BlockEditorValueConnector>($"Value is null or whitespace. Skipping conversion to artifact.");
6263
return null;
6364
}
6465

65-
if (svalue.DetectIsJson() == false)
66+
if (!valueAsString.TryParseJson(out BlockEditorValue blockEditorValue))
6667
{
67-
_logger.Warn<BlockListValueConnector>("Value '{Value}' is not a json string. Skipping conversion to artifact.", svalue);
68+
_logger.Warn<BlockListValueConnector>("Value '{Value}' is not a JSON string. Skipping conversion to artifact.", valueAsString);
6869
return null;
6970
}
7071

71-
var blockEditorValue = JsonConvert.DeserializeObject<BlockEditorValue>(svalue);
72-
7372
if (blockEditorValue == null)
7473
{
7574
_logger.Warn<BlockEditorValueConnector>("Deserialized value is null. Skipping conversion to artifact.");
@@ -148,13 +147,15 @@ public override object FromArtifact(string value, PropertyType propertyType, obj
148147
return value;
149148
}
150149

151-
if (value.DetectIsJson() == false)
150+
if (!value.TryParseJson(out BlockEditorValue blockEditorValue))
151+
{
152152
return value;
153-
154-
var blockEditorValue = JsonConvert.DeserializeObject<BlockEditorValue>(value);
153+
}
155154

156155
if (blockEditorValue == null)
156+
{
157157
return value;
158+
}
158159

159160
var allBlocks = blockEditorValue.Content.Concat(blockEditorValue.Settings ?? Enumerable.Empty<Block>()).ToList();
160161

src/Umbraco.Deploy.Contrib.Connectors/ValueConnectors/NestedContentValueConnector.cs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Umbraco.Deploy.Connectors.ValueConnectors;
1313
using Umbraco.Deploy.Connectors.ValueConnectors.Services;
1414
using Umbraco.Deploy.Core;
15+
using Umbraco.Deploy.Extensions;
1516

1617
namespace Umbraco.Deploy.Contrib.Connectors.ValueConnectors
1718
{
@@ -51,28 +52,38 @@ public NestedContentValueConnector(IContentTypeService contentTypeService, Lazy<
5152
public sealed override string ToArtifact(object value, PropertyType propertyType, ICollection<ArtifactDependency> dependencies, IContextCache contextCache)
5253
{
5354
_logger.Debug<NestedContentValueConnector>("Converting {PropertyType} to artifact.", propertyType.Alias);
54-
var svalue = value as string;
55+
var valueAsString = value as string;
5556

56-
if (string.IsNullOrWhiteSpace(svalue))
57+
if (string.IsNullOrWhiteSpace(valueAsString))
5758
{
5859
_logger.Debug<NestedContentValueConnector>($"Value is null or whitespace. Skipping conversion to artifact.");
5960
return null;
6061
}
6162

62-
if (svalue.DetectIsJson() == false)
63-
{
64-
_logger.Warn<NestedContentValueConnector>("Value '{Value}' is not a json string. Skipping conversion to artifact.", svalue);
65-
return null;
66-
}
67-
6863
var nestedContent = new List<NestedContentValue>();
69-
if (svalue.Trim().StartsWith("{"))
64+
if (valueAsString.Trim().StartsWith("{"))
7065
{
71-
nestedContent.Add(JsonConvert.DeserializeObject<NestedContentValue>(svalue));
66+
if (valueAsString.TryParseJson(out NestedContentValue nestedContentObjectValue))
67+
{
68+
nestedContent.Add(nestedContentObjectValue);
69+
}
70+
else
71+
{
72+
_logger.Warn<NestedContentValueConnector>("Value '{Value}' is not a JSON string. Skipping conversion to artifact.", valueAsString);
73+
return null;
74+
}
7275
}
7376
else
7477
{
75-
nestedContent.AddRange(JsonConvert.DeserializeObject<NestedContentValue[]>(svalue));
78+
if (valueAsString.TryParseJson(out NestedContentValue[] nestedContentCollectionValue))
79+
{
80+
nestedContent.AddRange(nestedContentCollectionValue);
81+
}
82+
else
83+
{
84+
_logger.Warn<NestedContentValueConnector>("Value '{Value}' is not a JSON string. Skipping conversion to artifact.", valueAsString);
85+
return null;
86+
}
7687
}
7788

7889
if (nestedContent.All(x => x == null))
@@ -153,14 +164,12 @@ public sealed override object FromArtifact(string value, PropertyType propertyTy
153164
return value;
154165
}
155166

156-
if (value.DetectIsJson() == false)
167+
if (!value.TryParseJson(out NestedContentValue[] nestedContent))
157168
{
158169
_logger.Warn<NestedContentValueConnector>("Value '{Value}' is not a json string. Skipping conversion from artifact.", value);
159170
return value;
160171
}
161172

162-
var nestedContent = JsonConvert.DeserializeObject<NestedContentValue[]>(value);
163-
164173
if (nestedContent == null || nestedContent.All(x => x == null))
165174
{
166175
_logger.Warn<NestedContentValueConnector>("Value contained no elements. Skipping conversion from artifact.");
@@ -187,7 +196,7 @@ public sealed override object FromArtifact(string value, PropertyType propertyTy
187196
// see note in NestedContentValue - leave it unchanged
188197
if (key == "key")
189198
continue;
190-
199+
191200
var innerPropertyType = contentType.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == key);
192201

193202
if (innerPropertyType == null)
@@ -217,9 +226,9 @@ public sealed override object FromArtifact(string value, PropertyType propertyTy
217226
row.PropertyValues[key] = convertedValue.ToString();
218227
}
219228
// json strings need to be converted into JTokens
220-
else if (convertedValue is string convertedStringValue && convertedStringValue.DetectIsJson())
229+
else if (convertedValue is string convertedStringValue && convertedStringValue.TryParseJson(out JToken valueAsJToken))
221230
{
222-
row.PropertyValues[key] = JToken.Parse(convertedStringValue);
231+
row.PropertyValues[key] = valueAsJToken;
223232
}
224233
else
225234
{

src/Umbraco.Deploy.Contrib.Tests/Umbraco.Deploy.Contrib.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="UmbracoDeploy.Core">
11-
<Version>4.7.0-rc001</Version>
11+
<Version>4.7.0-rc002</Version>
1212
</PackageReference>
1313
</ItemGroup>
1414

0 commit comments

Comments
 (0)