From faceddb8a4f334df723c864eb8c8fc5388a7a084 Mon Sep 17 00:00:00 2001 From: Bart Koelman <104792814+bart-vmware@users.noreply.github.com> Date: Tue, 15 Oct 2024 12:25:53 +0200 Subject: [PATCH] Fix broken tests when running Config Schema Generator on .NET 9 --- src/Components/Directory.Build.targets | 2 +- .../ConfigSchemaEmitter.cs | 42 ++++++++++++++----- .../ConfigurationSchemaGenerator.csproj | 2 +- .../ConfigurationSchemaGenerator.Tests.csproj | 13 +++++- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/Components/Directory.Build.targets b/src/Components/Directory.Build.targets index df3a4b6720..c453a8bfd2 100644 --- a/src/Components/Directory.Build.targets +++ b/src/Components/Directory.Build.targets @@ -78,7 +78,7 @@ + Properties="$(Properties);TargetFramework=$(DefaultTargetFramework)"> diff --git a/src/Tools/ConfigurationSchemaGenerator/ConfigSchemaEmitter.cs b/src/Tools/ConfigurationSchemaGenerator/ConfigSchemaEmitter.cs index e052c12f1a..8e7fd09fbb 100644 --- a/src/Tools/ConfigurationSchemaGenerator/ConfigSchemaEmitter.cs +++ b/src/Tools/ConfigurationSchemaGenerator/ConfigSchemaEmitter.cs @@ -61,7 +61,7 @@ private void GenerateLogCategories(JsonObject parent) { var categoryNode = new JsonObject(); categoryNode["$ref"] = "#/definitions/logLevelThreshold"; - propertiesNode[categories[i]] = categoryNode; + ReplaceNodeWithKeyCasingChange(propertiesNode, categories[i], categoryNode); } parent["definitions"] = new JsonObject @@ -138,7 +138,7 @@ private bool GeneratePathSegment(JsonObject currentNode, TypeSpec type, Queue? exclusionPaths) return result; } + private static void ReplaceNodeWithKeyCasingChange(JsonObject jsonObject, string key, JsonNode value) + { +#if NET9_0_OR_GREATER + // In .NET 9, the key casing is never adapted. See https://github.com/dotnet/runtime/issues/108790. + var index = jsonObject.IndexOf(key); + if (index != -1) + { + jsonObject.RemoveAt(index); + jsonObject.Insert(index, key, value); + } + else + { + jsonObject[key] = value; + } +#else + // In .NET 8, the key casing is adapted, except when the value doesn't change. + if (ReferenceEquals(jsonObject[key], value)) + { + // There's no API to preserve property order. + jsonObject.Remove(key); + } + + jsonObject[key] = value; +#endif + } + private sealed class SchemaOrderJsonNodeConverter : JsonConverter { public static SchemaOrderJsonNodeConverter Instance { get; } = new SchemaOrderJsonNodeConverter(); diff --git a/src/Tools/ConfigurationSchemaGenerator/ConfigurationSchemaGenerator.csproj b/src/Tools/ConfigurationSchemaGenerator/ConfigurationSchemaGenerator.csproj index eaafbbd25a..90a576274a 100644 --- a/src/Tools/ConfigurationSchemaGenerator/ConfigurationSchemaGenerator.csproj +++ b/src/Tools/ConfigurationSchemaGenerator/ConfigurationSchemaGenerator.csproj @@ -1,7 +1,7 @@ - $(DefaultTargetFramework) + $(AllTargetFrameworks) Exe enable annotations diff --git a/tests/ConfigurationSchemaGenerator.Tests/ConfigurationSchemaGenerator.Tests.csproj b/tests/ConfigurationSchemaGenerator.Tests/ConfigurationSchemaGenerator.Tests.csproj index df160f6479..3fba47b5f9 100644 --- a/tests/ConfigurationSchemaGenerator.Tests/ConfigurationSchemaGenerator.Tests.csproj +++ b/tests/ConfigurationSchemaGenerator.Tests/ConfigurationSchemaGenerator.Tests.csproj @@ -1,7 +1,7 @@ - $(DefaultTargetFramework) + $(AllTargetFrameworks) true true @@ -18,10 +18,19 @@ - + + + + + + + + + +