From 0c1e58a55c5a6ea7db0df3e1e08806d34f70886c Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Tue, 26 Dec 2023 12:47:52 +0800 Subject: [PATCH] Fix schema generation of sealed type with extension data (#332) --- .../Issues/Issue0331Tests.cs | 45 +++++++++++++++++++ .../Generation/JSchemaGeneratorInternal.cs | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 Src/Newtonsoft.Json.Schema.Tests/Issues/Issue0331Tests.cs diff --git a/Src/Newtonsoft.Json.Schema.Tests/Issues/Issue0331Tests.cs b/Src/Newtonsoft.Json.Schema.Tests/Issues/Issue0331Tests.cs new file mode 100644 index 00000000..ac3d0f44 --- /dev/null +++ b/Src/Newtonsoft.Json.Schema.Tests/Issues/Issue0331Tests.cs @@ -0,0 +1,45 @@ +#region License +// Copyright (c) Newtonsoft. All Rights Reserved. +// License: https://raw.github.com/JamesNK/Newtonsoft.Json.Schema/master/LICENSE.md +#endregion + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Schema.Generation; +using Newtonsoft.Json.Serialization; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Schema.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif + +namespace Newtonsoft.Json.Schema.Tests.Issues +{ + [TestFixture] + public class Issue0331Tests : TestFixtureBase + { + [Test] + public void Test() + { + JSchemaGenerator generator = new JSchemaGenerator(); + JSchema schema = generator.Generate(typeof(MyDto)); + + Assert.IsFalse(schema.AllowAdditionalPropertiesSpecified); + } + + public sealed class MyDto + { + [JsonProperty("foo")] + public string Foo { get; set; } + + [JsonExtensionData] + public IDictionary ExtensionData { get; set; } + } + } +} diff --git a/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs b/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs index f0ca2efa..4a38d924 100644 --- a/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs +++ b/Src/Newtonsoft.Json.Schema/Generation/JSchemaGeneratorInternal.cs @@ -642,7 +642,7 @@ private void GenerateObjectSchema(JSchema schema, Type type, JsonObjectContract } } - if (type.IsSealed()) + if (type.IsSealed() && contract.ExtensionDataGetter == null) { schema.AllowAdditionalProperties = false; }