Skip to content

Commit

Permalink
Change IJsonSchemaExtensionDataAttribute to allow multiple extendion …
Browse files Browse the repository at this point in the history
…data entries, closes #1578
  • Loading branch information
Rico Suter committed Sep 26, 2023
1 parent e64b2f3 commit 8e65d2d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
7 changes: 6 additions & 1 deletion src/NJsonSchema.Tests/Serialization/ExtensionDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using NJsonSchema.Annotations;
using NJsonSchema.NewtonsoftJson.Generation;
using NJsonSchema.Tests.Generation;
using Xunit;

namespace NJsonSchema.Tests.Serialization
Expand Down Expand Up @@ -123,8 +122,14 @@ public MyCustomExtensionAttribute()
}

public string Key { get; }

public object Value { get; }

public IReadOnlyDictionary<string, object> ExtensionData => new Dictionary<string, object>
{
{ Key, Value }
};

public override bool IsValid(object value)
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace NJsonSchema.Annotations
{
/// <summary>Interface to add an extension data property to a class or property, implementation needs to inherit from System.Attribute.</summary>
public interface IJsonSchemaExtensionDataAttribute
{
/// <summary>Gets the property name.</summary>
string Key { get; }

/// <summary>Gets the value.</summary>
object Value { get; }
/// <summary>Gets the extension data.</summary>
IReadOnlyDictionary<string, object> ExtensionData { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace NJsonSchema.Annotations
{
Expand All @@ -31,5 +32,11 @@ public JsonSchemaExtensionDataAttribute(string key, object value)

/// <summary>Gets the value.</summary>
public object Value { get; }

/// <inheritdocs />
public IReadOnlyDictionary<string, object> ExtensionData => new Dictionary<string, object>
{
{ Key, Value }
};
}
}
46 changes: 25 additions & 21 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ public void AddProperty(
propertySchema.Default = ConvertDefaultValue(property.AccessorType, defaultValue);
ApplyDataAnnotations(propertySchema, propertyTypeDescription);
ApplyPropertyExtensionDataAttributes(property, propertySchema);
ApplyPropertyExtensionDataAttributes(propertySchema, property);
};

var referencingProperty = GenerateWithReferenceAndNullability(
Expand Down Expand Up @@ -1201,7 +1201,7 @@ public virtual bool IsPropertyIgnored(ContextualAccessorInfo accessorInfo, Type
/// <returns></returns>
public bool IsPropertyIgnoredBySettings(ContextualAccessorInfo accessorInfo)
{
if (Settings.IgnoreObsoleteProperties &&
if (Settings.IgnoreObsoleteProperties &&
accessorInfo.GetContextAttribute<ObsoleteAttribute>() != null)
{
return true;
Expand Down Expand Up @@ -1284,34 +1284,38 @@ private void ApplyRangeAttribute(JsonSchema schema, IEnumerable<Attribute> paren
}
}

private void ApplyTypeExtensionDataAttributes<TSchemaType>(TSchemaType schema, ContextualType contextualType) where TSchemaType : JsonSchema, new()
private void ApplyTypeExtensionDataAttributes(JsonSchema schema, ContextualType contextualType)
{
var extensionAttributes = contextualType.OriginalType.GetTypeInfo().GetCustomAttributes()
.Where(attribute => attribute is IJsonSchemaExtensionDataAttribute).ToArray();

if (extensionAttributes.Any())
{
var extensionData = new Dictionary<string, object>();

foreach (var attribute in extensionAttributes)
{
var extensionAttribute = (IJsonSchemaExtensionDataAttribute)attribute;
extensionData.Add(extensionAttribute.Key, extensionAttribute.Value);
}
var extensionAttributes = contextualType.OriginalType
.GetTypeInfo()
.GetCustomAttributes()
.OfType<IJsonSchemaExtensionDataAttribute>()
.ToArray();

schema.ExtensionData = extensionData;
}
ApplyTypeExtensionDataAttributes(schema, extensionAttributes);
}

private void ApplyPropertyExtensionDataAttributes(ContextualAccessorInfo accessorInfo, JsonSchemaProperty propertySchema)
private void ApplyPropertyExtensionDataAttributes(JsonSchemaProperty propertySchema, ContextualAccessorInfo accessorInfo)
{
var extensionDataAttributes = accessorInfo
var extensionAttributes = accessorInfo
.GetContextAttributes<IJsonSchemaExtensionDataAttribute>()
.ToArray();

if (extensionDataAttributes.Any())
ApplyTypeExtensionDataAttributes(propertySchema, extensionAttributes);
}

private static void ApplyTypeExtensionDataAttributes(JsonSchema schema, IJsonSchemaExtensionDataAttribute[] extensionAttributes)
{
if (extensionAttributes.Any())
{
propertySchema.ExtensionData = extensionDataAttributes.ToDictionary(a => a.Key, a => a.Value);
var extensionData = new Dictionary<string, object>();
foreach (var kvp in extensionAttributes
.SelectMany(attribute => attribute.ExtensionData))
{
extensionData[kvp.Key] = kvp.Value;
}

schema.ExtensionData = extensionData;
}
}
}
Expand Down

0 comments on commit 8e65d2d

Please sign in to comment.