From b86afd517ddace683cc53ff21394e5cd0e7bde9d Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 29 Oct 2020 12:45:32 +0100 Subject: [PATCH] Version increased and fix for refs. --- .../Implementation/Sync/JsonHelper.cs | 44 --------------- .../Sync/Schemas/SchemasSynchronizer.cs | 55 +++++++++++++++++-- .../Commands/Models/SchemaWithRefs.cs | 1 - .../Squidex.CLI/Squidex.CLI.csproj | 2 +- 4 files changed, 52 insertions(+), 50 deletions(-) diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/Sync/JsonHelper.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/Sync/JsonHelper.cs index 1cb98506..e7c7a466 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/Sync/JsonHelper.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/Sync/JsonHelper.cs @@ -6,7 +6,6 @@ // ========================================================================== using System; -using System.Collections.Generic; using System.IO; using System.Linq; using Newtonsoft.Json; @@ -22,7 +21,6 @@ public class JsonHelper private readonly JsonSchemaGeneratorSettings jsonSchemaGeneratorSettings; private readonly JsonSerializerSettings jsonSerializerSettings; private readonly JsonSerializer jsonSerializer; - private readonly JsonSchemaIdConverter schemaIdConverter = new JsonSchemaIdConverter(); internal sealed class CamelCaseExceptDictionaryKeysResolver : CamelCasePropertyNamesContractResolver { @@ -43,7 +41,6 @@ public JsonHelper() ContractResolver = new CamelCaseExceptDictionaryKeysResolver() }; - jsonSerializerSettings.Converters.Add(schemaIdConverter); jsonSerializerSettings.Formatting = Formatting.Indented; jsonSchemaGeneratorSettings = new JsonSchemaGeneratorSettings @@ -59,47 +56,6 @@ public JsonHelper() jsonSerializer = JsonSerializer.Create(jsonSerializerSettings); } - internal sealed class JsonSchemaIdConverter : JsonConverter - { - public Dictionary SchemaMap { get; set; } = new Dictionary(); - - public override string ReadJson(JsonReader reader, Type objectType, string existingValue, bool hasExistingValue, JsonSerializer serializer) - { - if (reader.TokenType != JsonToken.String) - { - throw new JsonException("Expected string."); - } - - var value = (string)reader.Value; - - if (!SchemaMap.TryGetValue(value, out var id)) - { - throw new JsonException($"Schema {value} not found."); - } - - return id; - } - - public override void WriteJson(JsonWriter writer, string value, JsonSerializer serializer) - { - var schemaName = SchemaMap.FirstOrDefault(x => x.Value == value).Key; - - if (string.IsNullOrWhiteSpace(schemaName)) - { - writer.WriteValue(""); - } - else - { - writer.WriteValue(schemaName); - } - } - } - - public void SetSchemaMap(Dictionary schemas) - { - schemaIdConverter.SchemaMap = schemas; - } - public T Read(FileInfo file, ILogger log) { var json = File.ReadAllText(file.FullName); diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/Sync/Schemas/SchemasSynchronizer.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/Sync/Schemas/SchemasSynchronizer.cs index 8722785d..8339c4e9 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/Sync/Schemas/SchemasSynchronizer.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/Implementation/Sync/Schemas/SchemasSynchronizer.cs @@ -31,9 +31,7 @@ public async Task ExportAsync(DirectoryInfo directoryInfo, JsonHelper jsonHelper { var current = await session.Schemas.GetSchemasAsync(session.App); - var schemaMap = current.Items.ToDictionary(x => x.Name, x => x.Id); - - jsonHelper.SetSchemaMap(schemaMap); + var schemaMap = current.Items.ToDictionary(x => x.Id, x => x.Name); foreach (var schema in current.Items.OrderBy(x => x.Name)) { @@ -48,6 +46,8 @@ await log.DoSafeAsync($"Exporting '{schema.Name}'", async () => model.Schema = jsonHelper.Convert(details); + MapReferences(model.Schema, schemaMap); + await jsonHelper.WriteWithSchema(directoryInfo, $"schemas/{schema.Name}.json", model, "../__json/schema"); }); } @@ -104,7 +104,7 @@ await log.DoSafeAsync($"Schema {newSchema.Name} creating", async () => }); } - jsonHelper.SetSchemaMap(schemasByName.ToDictionary(x => x.Key, x => x.Value.Id)); + var schemaMap = schemasByName.ToDictionary(x => x.Key, x => x.Value.Id); var newSchemas = GetSchemaFiles(directoryInfo) @@ -113,6 +113,8 @@ await log.DoSafeAsync($"Schema {newSchema.Name} creating", async () => foreach (var newSchema in newSchemas) { + MapReferences(newSchema.Schema, schemaMap); + var version = schemasByName[newSchema.Name].Version; await log.DoVersionedAsync($"Schema {newSchema.Name} updating", version, async () => @@ -166,5 +168,50 @@ public async Task GenerateSchemaAsync(DirectoryInfo directoryInfo, JsonHelper js await jsonHelper.WriteWithSchema(directoryInfo, "schemas/__schema.json", sample, "../__json/schema"); } + + private void MapReferences(SynchronizeSchemaDto schema, Dictionary map) + { + if (schema.Fields != null) + { + foreach (var field in schema.Fields) + { + MapReferences(field, map); + } + } + } + + private void MapReferences(UpsertSchemaFieldDto field, Dictionary map) + { + MapReferences(field.Properties, map); + + if (field.Nested != null) + { + foreach (var nested in field.Nested) + { + MapReferences(nested.Properties, map); + } + } + } + + private static void MapReferences(FieldPropertiesDto properties, Dictionary map) + { + if (properties is ReferencesFieldPropertiesDto references) + { + if (references.SchemaIds != null && references.SchemaIds.Any()) + { + var names = new List(); + + foreach (var id in references.SchemaIds) + { + if (map.TryGetValue(id, out var target)) + { + names.Add(target); + } + } + + references.SchemaIds = names; + } + } + } } } \ No newline at end of file diff --git a/cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefs.cs b/cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefs.cs index 4aa3699e..b157b12a 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefs.cs +++ b/cli/Squidex.CLI/Squidex.CLI/Commands/Models/SchemaWithRefs.cs @@ -5,7 +5,6 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== -using System; using System.Collections.Generic; using System.IO; using Newtonsoft.Json; diff --git a/cli/Squidex.CLI/Squidex.CLI/Squidex.CLI.csproj b/cli/Squidex.CLI/Squidex.CLI/Squidex.CLI.csproj index 94a498f9..463c4405 100644 --- a/cli/Squidex.CLI/Squidex.CLI/Squidex.CLI.csproj +++ b/cli/Squidex.CLI/Squidex.CLI/Squidex.CLI.csproj @@ -14,7 +14,7 @@ netcoreapp3.1 true sq - 6.0 + 6.1