diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/App_Content.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/App_Content.cs index de0ce2d2..01b4fe6f 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/App_Content.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/App_Content.cs @@ -234,7 +234,7 @@ await session.ExportAsync(arguments, log, async entity => { if (arguments.FilePerContent) { - throw new SquidexException("Multiple files are not supported for CSV export."); + throw new CLIException("Multiple files are not supported for CSV export."); } var file = arguments.Output; diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/App_Schemas.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/App_Schemas.cs index d94d5262..6336a66a 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/App_Schemas.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/App_Schemas.cs @@ -98,7 +98,7 @@ public async Task Sync(SyncArguments arguments) } catch (IOException) { - throw new SquidexException("Cannot read schema file."); + throw new CLIException("Cannot read schema file."); } if (string.IsNullOrWhiteSpace(schemaName)) @@ -111,13 +111,13 @@ public async Task Sync(SyncArguments arguments) } catch (JsonException ex) { - throw new SquidexException($"Cannot deserialize schema: {ex.Message}"); + throw new CLIException($"Cannot deserialize schema: {ex.Message}"); } } if (string.IsNullOrWhiteSpace(schemaName)) { - throw new SquidexException("Schema name cannot be empty."); + throw new CLIException("Schema name cannot be empty."); } SchemaDetailsDto targetSchema; diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/Csv2SquidexConverter.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/Csv2SquidexConverter.cs index 6aaf0ec5..f7f00adc 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/Csv2SquidexConverter.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/Csv2SquidexConverter.cs @@ -102,7 +102,7 @@ static object AddElement(object parent, string key, int index, JToken currentVal } } - throw new SquidexException("Invalid json mapping."); + throw new CLIException("Invalid json mapping."); } object container = property; diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/Json2SquidexConverter.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/Json2SquidexConverter.cs index 648ba986..6cc07b84 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/Json2SquidexConverter.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/Json2SquidexConverter.cs @@ -158,7 +158,7 @@ private static object AddElement(object parent, string key, int index, JToken cu } } - throw new SquidexException("Invalid json mapping."); + throw new CLIException("Invalid json mapping."); } } } diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/JsonMapping.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/JsonMapping.cs index d30b7425..560873af 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/JsonMapping.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/ImExport/JsonMapping.cs @@ -44,13 +44,13 @@ static JsonPath GetPath(string value) result.Add((parts[0], GetPath(parts[1]))); break; default: - throw new SquidexException("Field definition not valid."); + throw new CLIException("Field definition not valid."); } } if (result.Count == 0) { - throw new SquidexException("Field definition not valid."); + throw new CLIException("Field definition not valid."); } return result; @@ -87,13 +87,13 @@ static JsonPath GetPath(string value) result.Add((parts[1], GetPath(parts[0]))); break; default: - throw new SquidexException("Field definition not valid."); + throw new CLIException("Field definition not valid."); } } if (result.Count == 0) { - throw new SquidexException("Field definition not valid."); + throw new CLIException("Field definition not valid."); } return result; diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefs.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefs.cs index b157b12a..b231cddc 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefs.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefs.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.IO; using Newtonsoft.Json; +using Squidex.CLI.Commands.Implementation; using Squidex.ClientLibrary; namespace Squidex.CLI.Commands.Models @@ -50,7 +51,7 @@ public static SchemaWithRefs Parse(string json) } catch (IOException ex) { - throw new SquidexException($"Cannot deserialize schema: {ex.Message}"); + throw new CLIException($"Cannot deserialize schema: {ex.Message}"); } } } diff --git a/cli/Squidex.CLI/Squidex.CLI/Configuration/ConfigurationService.cs b/cli/Squidex.CLI/Squidex.CLI/Configuration/ConfigurationService.cs index 5c86511e..54b531a0 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Configuration/ConfigurationService.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Configuration/ConfigurationService.cs @@ -79,7 +79,7 @@ private void Save() } catch (Exception ex) { - throw new SquidexException("Failed to save configuration file.", ex); + throw new CLIException("Failed to save configuration file.", ex); } } @@ -112,7 +112,7 @@ public void UseApp(string entry) { if (!configuration.Apps.ContainsKey(entry)) { - throw new SquidexException("App config with the name does not exist."); + throw new CLIException("App config with the name does not exist."); } configuration.CurrentApp = entry; @@ -124,7 +124,7 @@ public void Remove(string entry) { if (!configuration.Apps.ContainsKey(entry)) { - throw new SquidexException("App config with the name does not exist."); + throw new CLIException("App config with the name does not exist."); } configuration.Apps.Remove(entry); @@ -158,7 +158,7 @@ public ISession StartSession() return new Session(app.Name, new SquidexClientManager(options)); } - throw new SquidexException("Cannot find valid configuration."); + throw new CLIException("Cannot find valid configuration."); } private static SquidexOptions CreateOptions(ConfiguredApp app) diff --git a/cli/Squidex.CLI/Squidex.CLI/Program.cs b/cli/Squidex.CLI/Squidex.CLI/Program.cs index 5c277d40..043897c3 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Program.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Program.cs @@ -58,6 +58,11 @@ public static int Main(string[] args) return appRunner.Run(args); } + catch (CLIException ex) + { + Console.WriteLine("ERROR: {0}", ex.Message); + return -1; + } catch (Exception ex) { Console.WriteLine("ERROR: {0}", ex); diff --git a/cli/Squidex.CLI/Squidex.CLI/Squidex.CLI.csproj b/cli/Squidex.CLI/Squidex.CLI/Squidex.CLI.csproj index 0f69bc16..f2d59f7f 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Squidex.CLI.csproj +++ b/cli/Squidex.CLI/Squidex.CLI/Squidex.CLI.csproj @@ -14,7 +14,7 @@ net5.0 true sq - 7.2 + 7.3 diff --git a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/EnrichedEvents/EnrichedEventsTests.cs b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/EnrichedEvents/EnrichedEventsTests.cs index 006a5799..aa543b0d 100644 --- a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/EnrichedEvents/EnrichedEventsTests.cs +++ b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/EnrichedEvents/EnrichedEventsTests.cs @@ -19,12 +19,20 @@ public class EnrichedEventsTests 'type': 'SchemaUpdated', 'payload': { '$type': 'EnrichedContentEvent', - 'type': 'Updated', 'id': '062b936f-7496-4f87-bd4f-ba7bbb63c751', - 'created': '2021-01-01T00:00:00Z', - 'lastModified': '2021-01-01T00:01:00Z', + 'actor': 'subject:601c2cbafa4e669f214c0438', + 'appId': '3e2df825-86a9-43cb-8eb7-97d5a5bd4eea,testapp', + 'created': '2021-01-01T00:00:00Z', 'createdBy': 'subject:601c2cbafa4e669f214c0438', + 'lastModified': '2021-01-01T00:01:00Z', 'lastModifiedBy': 'subject:601c2cbafa4e669f214c0438', + 'name': 'SchemaUpdated', + 'partition': -792991992, + 'schemaId': '062b936f-7496-4f87-bd4f-ba7bbb63c751,schema', + 'status': 'Published', + 'timestamp': '2021-01-01T00:01:00Z', + 'type': 'Updated', + 'version': 3, 'data': { 'testField': { 'iv': 'test2' @@ -34,15 +42,7 @@ public class EnrichedEventsTests 'testField': { 'iv': 'test' } - }, - 'status': 'Published', - 'partition': -792991992, - 'schemaId': '062b936f-7496-4f87-bd4f-ba7bbb63c751,schema', - 'actor': 'subject:601c2cbafa4e669f214c0438', - 'appId': '3e2df825-86a9-43cb-8eb7-97d5a5bd4eea,testapp', - 'timestamp': '2021-01-01T00:01:00Z', - 'name': 'SchemaUpdated', - 'version': 3 + } }, 'timestamp': '2021-01-01T00:01:00Z' }"; @@ -80,13 +80,13 @@ public void Should_deserialize_EnrichedContentEvent() 'type': 'UserMentioned', 'payload': { '$type': 'EnrichedCommentEvent', - 'text': '@user@test.com testmessage', - 'url': '/app/testapp/content/schema/0e5955e3-cd2a-49f2-92ba-303acf4dd192/comments', - 'partition': -1730311374, 'actor': 'subject:601c2cbafa4e669f214c0438', 'appId': '3e2df825-86a9-43cb-8eb7-97d5a5bd4eea,testapp', - 'timestamp': '2021-01-01T00:00:00Z', 'name': 'UserMentioned', + 'partition': -1730311374, + 'text': '@user@test.com testmessage', + 'timestamp': '2021-01-01T00:00:00Z', + 'url': '/app/testapp/content/schema/0e5955e3-cd2a-49f2-92ba-303acf4dd192/comments', 'version': 0 }, 'timestamp': '2021-01-01T00:00:00Z' @@ -111,23 +111,23 @@ public void Should_deserialize_EnrichedCommentEvent() 'type': 'AssetCreatedFromSnapshot', 'payload': { '$type': 'EnrichedAssetEvent', - 'type': 'Created', 'id': 'c5dc4403-713d-4ebd-9a8f-a17efdba924e', + 'actor': 'subject:6025a698a825d86becf541fe', + 'appId': '3e2df825-86a9-43cb-8eb7-97d5a5bd4eea,testapp', + 'assetType': 'Unknown', 'created': '2021-01-01T00:00:00Z', - 'lastModified': '2021-01-01T00:00:00Z', 'createdBy': 'subject:6025a698a825d86becf541fe', - 'lastModifiedBy': 'subject:6025a698a825d86becf541fe', - 'mimeType': 'application/pdf', 'fileName': 'name.pdf', - 'fileVersion': 0, 'fileSize': 447021, - 'assetType': 'Unknown', + 'fileVersion': 0, 'isImage': false, + 'lastModified': '2021-01-01T00:00:00Z', + 'lastModifiedBy': 'subject:6025a698a825d86becf541fe', + 'mimeType': 'application/pdf', + 'name': 'AssetCreatedFromSnapshot', 'partition': -755061617, - 'actor': 'subject:6025a698a825d86becf541fe', - 'appId': '3e2df825-86a9-43cb-8eb7-97d5a5bd4eea,testapp', 'timestamp': '1970-01-01T00:00:00Z', - 'name': 'AssetCreatedFromSnapshot', + 'type': 'Created', 'version': 1 }, 'timestamp': '1970-01-01T00:00:00Z' diff --git a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SerializationTests.cs b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SerializationTests.cs index 83744269..60ab58d6 100644 --- a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SerializationTests.cs +++ b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary.Tests/SerializationTests.cs @@ -21,6 +21,17 @@ public sealed class MyClass public T Value { get; set; } } + public sealed class MyCamelClass + { + public T Value { get; set; } + } + + [KeepCasing] + public sealed class MyPascalClass + { + public T Value { get; set; } + } + private readonly JsonSerializerSettings settings = new JsonSerializerSettings(); public SerializationTests() @@ -184,5 +195,31 @@ public void Should_deserialize_invariant_empty_value() Assert.Null(res.Value); } + + [Fact] + public void Should_serialize_with_camel_case() + { + var source = new MyCamelClass + { + Value = "hello" + }; + + var serialized = source.ToJson(); + + Assert.Contains("\"value\": \"hello\"", serialized); + } + + [Fact] + public void Should_serialize_with_pascal_case() + { + var source = new MyPascalClass + { + Value = "hello" + }; + + var serialized = source.ToJson(); + + Assert.Contains("\"Value\": \"hello\"", serialized); + } } } diff --git a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/KeepCasingAttribute.cs b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/KeepCasingAttribute.cs index 0d05973d..a3b78d88 100644 --- a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/KeepCasingAttribute.cs +++ b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/KeepCasingAttribute.cs @@ -6,6 +6,8 @@ // ========================================================================== using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; namespace Squidex.ClientLibrary { @@ -14,7 +16,14 @@ namespace Squidex.ClientLibrary /// /// [AttributeUsage(AttributeTargets.Class)] - public sealed class KeepCasingAttribute : Attribute + public sealed class KeepCasingAttribute : JsonContainerAttribute { + /// + /// Initializes a new instance of the class. + /// + public KeepCasingAttribute() + { + NamingStrategyType = typeof(DefaultNamingStrategy); + } } } diff --git a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/Utils/HttpClientExtensions.cs b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/Utils/HttpClientExtensions.cs index ea9b2772..629dff30 100644 --- a/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/Utils/HttpClientExtensions.cs +++ b/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/Utils/HttpClientExtensions.cs @@ -8,7 +8,6 @@ using System.IO; using System.Net.Http; using System.Net.Http.Headers; -using System.Reflection; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; @@ -21,15 +20,13 @@ namespace Squidex.ClientLibrary.Utils { public static class HttpClientExtensions { - private static readonly JsonSerializerSettings PascalCasing = CreateSerializer(new DefaultContractResolver()); + private static readonly JsonSerializerSettings SerializerSettings = CreateSerializer(); - private static readonly JsonSerializerSettings CamelCasing = CreateSerializer(new CamelCasePropertyNamesContractResolver()); - - private static JsonSerializerSettings CreateSerializer(IContractResolver contractResolver) + private static JsonSerializerSettings CreateSerializer() { var result = new JsonSerializerSettings { - ContractResolver = contractResolver + ContractResolver = new CamelCasePropertyNamesContractResolver() }; result.Converters.Add(new StringEnumConverter()); @@ -63,12 +60,7 @@ public static HttpContent ToContent(this Stream stream, string name, string mime public static string ToJson(this T value) { - var serializerSettings = - value.GetType().GetCustomAttribute() != null ? - PascalCasing : - CamelCasing; - - var json = JsonConvert.SerializeObject(value, Formatting.Indented, serializerSettings); + var json = JsonConvert.SerializeObject(value, Formatting.Indented, SerializerSettings); return json; }