Skip to content

Commit

Permalink
Add System.Text.Json ExtensionData attribute tests (#1312)
Browse files Browse the repository at this point in the history
* Add System.Text.Json extension attribute tests

* Fix test

Co-authored-by: Rico Suter <mail@rsuter.com>
  • Loading branch information
deceptiveSimplicity and RicoSuter authored Feb 15, 2021
1 parent 3f7f55e commit 39d09be
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#if !NET46 && !NET452

using System.Collections.Generic;
using System.Text.Json.Serialization;
using NJsonSchema.Generation;
using Xunit;

namespace NJsonSchema.Tests.Generation.SystemTextJson
{
public class SystemTextJsonExtensionDataGenerationTests
{
public class ClassWithExtensionData
{
public string Foo { get; set; }

[JsonExtensionData]
public IDictionary<string, object> ExtensionData { get; set; }
}

[Fact]
public void SystemTextJson_When_class_has_property_with_JsonExtensionDataAttribute_on_property_then_AdditionalProperties_schema_is_set()
{
//// Act
var schema = JsonSchema.FromType<ClassWithExtensionData>(new JsonSchemaGeneratorSettings
{
SchemaType = SchemaType.OpenApi3,
SerializerOptions = new System.Text.Json.JsonSerializerOptions()
});

//// Assert
Assert.Equal(1, schema.ActualProperties.Count);
Assert.True(schema.AllowAdditionalProperties);
Assert.True(schema.AdditionalPropertiesSchema.ActualSchema.IsAnyType);
}
}
}

#endif
5 changes: 3 additions & 2 deletions src/NJsonSchema/Generation/SystemTextJsonUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
var attributes = member.GetCustomAttributes(true);

var property = base.CreateProperty(member, memberSerialization);
property.Ignored = attributes
.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonIgnoreAttribute", TypeNameStyle.FullName) != null;
property.Ignored =
attributes.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonIgnoreAttribute", TypeNameStyle.FullName) != null ||
attributes.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) != null;

if (_serializerOptions.PropertyNamingPolicy != null)
{
Expand Down

0 comments on commit 39d09be

Please sign in to comment.