Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json.Serialization.JsonExtensionDataAttribute allows object & JsonElement values #1367

Merged
merged 4 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<string, object> ExtensionData { get; set; }
}

public class ClassWithJsonElementExtensionData
{
public string Foo { get; set; }

[JsonExtensionData]
public IDictionary<string, JsonElement> 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<ClassWithObjectExtensionData>(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<ClassWithExtensionData>(new JsonSchemaGeneratorSettings
var schema = JsonSchema.FromType<ClassWithJsonElementExtensionData>(new JsonSchemaGeneratorSettings
{
SchemaType = SchemaType.OpenApi3,
SerializerOptions = new System.Text.Json.JsonSerializerOptions()
SerializerOptions = new JsonSerializerOptions()
});

//// Assert
Expand Down
2 changes: 2 additions & 0 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ private void ApplyAdditionalProperties<TSchemaType>(TSchemaType schema, Type typ
{
var genericTypeArguments = extensionDataProperty.GenericArguments;
var extensionDataPropertyType = genericTypeArguments.Length == 2 ? genericTypeArguments[1] : typeof(object).ToContextualType();
if (extensionDataPropertyType.TypeInfo.FullName.Equals("System.Text.Json.JsonElement"))
extensionDataPropertyType = typeof(object).ToContextualType();
RicoSuter marked this conversation as resolved.
Show resolved Hide resolved

schema.AdditionalPropertiesSchema = GenerateWithReferenceAndNullability<JsonSchema>(
extensionDataPropertyType, schemaResolver);
Expand Down