diff --git a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs index 797bb59e2..a22a2ce3b 100644 --- a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs +++ b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonExtensionDataGenerationTests.cs @@ -1,6 +1,7 @@ #if !NET46 && !NET452 using System.Collections.Generic; +using System.Text.Json; using System.Text.Json.Serialization; using NJsonSchema.Generation; using Xunit; @@ -9,22 +10,46 @@ namespace NJsonSchema.Tests.Generation.SystemTextJson { public class SystemTextJsonExtensionDataGenerationTests { - public class ClassWithExtensionData + public class ClassWithObjectExtensionData { public string Foo { get; set; } [JsonExtensionData] public IDictionary ExtensionData { get; set; } } + + public class ClassWithJsonElementExtensionData + { + public string Foo { get; set; } + + [JsonExtensionData] + public IDictionary ExtensionData { get; set; } + } [Fact] - public void SystemTextJson_When_class_has_property_with_JsonExtensionDataAttribute_on_property_then_AdditionalProperties_schema_is_set() + public void SystemTextJson_When_class_has_object_Dictionary_with_JsonExtensionDataAttribute_on_property_then_AdditionalProperties_schema_is_set() + { + //// Act + var schema = JsonSchema.FromType(new JsonSchemaGeneratorSettings + { + SchemaType = SchemaType.OpenApi3, + SerializerOptions = new JsonSerializerOptions() + }); + + //// Assert + Assert.Equal(1, schema.ActualProperties.Count); + Assert.True(schema.AllowAdditionalProperties); + Assert.True(schema.AdditionalPropertiesSchema.ActualSchema.IsAnyType); + } + + [Fact] + public void SystemTextJson_When_class_has_JsonElement_Dictionary_with_JsonExtensionDataAttribute_on_property_then_AdditionalProperties_schema_is_set() { //// Act - var schema = JsonSchema.FromType(new JsonSchemaGeneratorSettings + var schema = JsonSchema.FromType(new JsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3, - SerializerOptions = new System.Text.Json.JsonSerializerOptions() + SerializerOptions = new JsonSerializerOptions() }); //// Assert diff --git a/src/NJsonSchema/Generation/DefaultReflectionService.cs b/src/NJsonSchema/Generation/DefaultReflectionService.cs index 92aac1bd9..df26ec598 100644 --- a/src/NJsonSchema/Generation/DefaultReflectionService.cs +++ b/src/NJsonSchema/Generation/DefaultReflectionService.cs @@ -176,6 +176,7 @@ public virtual JsonTypeDescription GetDescription(ContextualType contextualType, if (type.IsAssignableToTypeName(nameof(JToken), TypeNameStyle.Name) || type.FullName == "System.Dynamic.ExpandoObject" || + type.FullName == "System.Text.Json.JsonElement" || type == typeof(object)) { return JsonTypeDescription.Create(contextualType, JsonObjectType.None, isNullable, null);