Skip to content

Commit

Permalink
Update Namotion.Reflection to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Aug 4, 2021
1 parent 9d9cd16 commit edbbe8d
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/NJsonSchema/Generation/DefaultSchemaNameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public virtual string Generate(Type type)
{
var cachedType = type.ToCachedType();

var jsonSchemaAttribute = cachedType.GetTypeAttribute<JsonSchemaAttribute>();
var jsonSchemaAttribute = cachedType.GetInheritedAttribute<JsonSchemaAttribute>();
if (!string.IsNullOrEmpty(jsonSchemaAttribute?.Name))
{
return jsonSchemaAttribute.Name;
Expand Down
146 changes: 85 additions & 61 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions src/NJsonSchema/Infrastructure/DescriptionAttributeType.cs

This file was deleted.

118 changes: 63 additions & 55 deletions src/NJsonSchema/Infrastructure/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using Namotion.Reflection;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

Expand All @@ -20,85 +21,79 @@ public static class TypeExtensions

/// <summary>Gets the name of the property for JSON serialization.</summary>
/// <returns>The name.</returns>
internal static string GetName(this ContextualMemberInfo member)
internal static string GetName(this ContextualAccessorInfo accessorInfo)
{
if (!_names.ContainsKey(member))
if (!_names.ContainsKey(accessorInfo))
{
lock (_names)
{
if (!_names.ContainsKey(member))
if (!_names.ContainsKey(accessorInfo))
{
_names[member] = GetNameWithoutCache(member);
_names[accessorInfo] = GetNameWithoutCache(accessorInfo);
}
}
}
return _names[member];
return _names[accessorInfo];
}

private static string GetNameWithoutCache(ContextualMemberInfo member)
private static string GetNameWithoutCache(ContextualAccessorInfo accessorInfo)
{
var jsonPropertyAttribute = member.GetContextAttribute<JsonPropertyAttribute>();
var jsonPropertyAttribute = accessorInfo.AccessorType.GetContextAttribute<JsonPropertyAttribute>();
if (jsonPropertyAttribute != null && !string.IsNullOrEmpty(jsonPropertyAttribute.PropertyName))
{
return jsonPropertyAttribute.PropertyName;
}

var dataMemberAttribute = member.GetContextAttribute<DataMemberAttribute>();
var dataMemberAttribute = accessorInfo.AccessorType.GetContextAttribute<DataMemberAttribute>();
if (dataMemberAttribute != null && !string.IsNullOrEmpty(dataMemberAttribute.Name))
{
var dataContractAttribute = member.MemberInfo.DeclaringType.ToCachedType().GetTypeAttribute<DataContractAttribute>();
var dataContractAttribute = accessorInfo.MemberInfo.DeclaringType.ToCachedType().GetInheritedAttribute<DataContractAttribute>();
if (dataContractAttribute != null)
{
return dataMemberAttribute.Name;
}
}

return member.Name;
return accessorInfo.Name;
}

/// <summary>Gets the description of the given member (based on the DescriptionAttribute, DisplayAttribute or XML Documentation).</summary>
/// <param name="type">The member info</param>
/// <param name="attributeType">The attribute type to check.</param>
/// <returns>The description or null if no description is available.</returns>
public static string GetDescription(this CachedType type, DescriptionAttributeType attributeType = DescriptionAttributeType.Context)
public static string GetDescription(this CachedType type)
{
var attributes = type is ContextualType contextualType && attributeType == DescriptionAttributeType.Context ?
contextualType.ContextAttributes : type.TypeAttributes;
var attributes = type is ContextualType contextualType ? contextualType.ContextAttributes : type.InheritedAttributes;

dynamic descriptionAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DescriptionAttribute");
if (descriptionAttribute != null && !string.IsNullOrEmpty(descriptionAttribute.Description))
var description = GetDescription(attributes);
if (description != null)
{
return descriptionAttribute.Description;
return description;
}
else

var summary = type.GetXmlDocsSummary();
if (summary != string.Empty)
{
dynamic displayAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.DisplayAttribute");
if (displayAttribute != null)
{
// GetDescription returns null if the Description property on the attribute is not specified.
var description = displayAttribute.GetDescription();
if (description != null)
{
return description;
}
}
return summary;
}

if (type is ContextualMemberInfo contextualMember)
{
var summary = contextualMember.GetXmlDocsSummary();
if (summary != string.Empty)
{
return summary;
}
}
else if (type != null)
{
var summary = type.GetXmlDocsSummary();
if (summary != string.Empty)
{
return summary;
}
}
return null;
}

/// <summary>Gets the description of the given member (based on the DescriptionAttribute, DisplayAttribute or XML Documentation).</summary>
/// <param name="accessorInfo">The accessor info.</param>
/// <returns>The description or null if no description is available.</returns>
public static string GetDescription(this ContextualAccessorInfo accessorInfo)
{
var description = GetDescription(accessorInfo.AccessorType.Attributes);
if (description != null)
{
return description;
}

var summary = accessorInfo.MemberInfo.GetXmlDocsSummary();
if (summary != string.Empty)
{
return summary;
}

return null;
Expand All @@ -109,25 +104,38 @@ public static string GetDescription(this CachedType type, DescriptionAttributeTy
/// <returns>The description or null if no description is available.</returns>
public static string GetDescription(this ContextualParameterInfo parameter)
{
dynamic descriptionAttribute = parameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DescriptionAttribute");
var description = GetDescription(parameter.ContextAttributes);
if (description != null)
{
return description;
}

var summary = parameter.GetXmlDocs();
if (summary != string.Empty)
{
return summary;
}

return null;
}

private static string GetDescription(IEnumerable<Attribute> attributes)
{
dynamic descriptionAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DescriptionAttribute");
if (descriptionAttribute != null && !string.IsNullOrEmpty(descriptionAttribute.Description))
{
return descriptionAttribute.Description;
}
else
{
dynamic displayAttribute = parameter.ContextAttributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.DisplayAttribute");
if (displayAttribute != null && !string.IsNullOrEmpty(displayAttribute.Description))
{
return displayAttribute.Description;
}

if (parameter != null)
dynamic displayAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.DisplayAttribute");
if (displayAttribute != null)
{
var summary = parameter.GetXmlDocs();
if (summary != string.Empty)
// GetDescription returns null if the Description property on the attribute is not specified.
var description = displayAttribute.GetDescription();
if (description != null)
{
return summary;
return description;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/NJsonSchema/Infrastructure/XmlObjectExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class XmlObjectExtension
/// <param name="type">The type of the JSON Schema.</param>
public static void GenerateXmlObjectForType(this JsonSchema schema, Type type)
{
var attributes = type.ToCachedType().TypeAttributes;
var attributes = type.ToCachedType().InheritedAttributes;
if (attributes.Any())
{
dynamic xmlTypeAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlTypeAttribute");
Expand All @@ -49,7 +49,7 @@ public static void GenerateXmlObjectForArrayType(this JsonSchema schema)
public static void GenerateXmlObjectForItemType(this JsonSchema schema, CachedType type)
{
// Is done all the time for XML to be able to get type name as the element name if not there was an attribute defined since earlier
var attributes = type.TypeAttributes;
var attributes = type.InheritedAttributes;
dynamic xmlTypeAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlTypeAttribute");

var itemName = GetXmlItemName(type.OriginalType);
Expand Down Expand Up @@ -117,7 +117,7 @@ public static void GenerateXmlObjectForProperty(this JsonSchemaProperty property
// We need to ensure that the property name is preserved
if (string.IsNullOrEmpty(xmlName) && propertySchema.Type == JsonObjectType.None)
{
dynamic xmlReferenceTypeAttribute = type.TypeAttributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlTypeAttribute");
dynamic xmlReferenceTypeAttribute = type.InheritedAttributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlTypeAttribute");
if (xmlReferenceTypeAttribute != null)
{
xmlName = propertyName;
Expand Down
12 changes: 7 additions & 5 deletions src/NJsonSchema/JsonReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void AddDocumentReference(string documentPath, IJsonReference schema)
/// <returns>The JSON Schema or <c>null</c> when the object could not be found.</returns>
/// <exception cref="InvalidOperationException">Could not resolve the JSON path.</exception>
/// <exception cref="NotSupportedException">Could not resolve the JSON path.</exception>
public async Task<IJsonReference> ResolveReferenceAsync(object rootObject, string jsonPath, Type targetType,
public async Task<IJsonReference> ResolveReferenceAsync(object rootObject, string jsonPath, Type targetType,
IContractResolver contractResolver, CancellationToken cancellationToken = default)
{
return await ResolveReferenceAsync(rootObject, jsonPath, targetType, contractResolver, true, cancellationToken).ConfigureAwait(false);
Expand All @@ -77,7 +77,7 @@ public async Task<IJsonReference> ResolveReferenceAsync(object rootObject, strin
/// <returns>The JSON Schema or <c>null</c> when the object could not be found.</returns>
/// <exception cref="InvalidOperationException">Could not resolve the JSON path.</exception>
/// <exception cref="NotSupportedException">Could not resolve the JSON path.</exception>
public async Task<IJsonReference> ResolveReferenceWithoutAppendAsync(object rootObject, string jsonPath, Type targetType,
public async Task<IJsonReference> ResolveReferenceWithoutAppendAsync(object rootObject, string jsonPath, Type targetType,
IContractResolver contractResolver, CancellationToken cancellationToken = default)
{
return await ResolveReferenceAsync(rootObject, jsonPath, targetType, contractResolver, false, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -150,7 +150,7 @@ private async Task<IJsonReference> ResolveReferenceAsync(object rootObject, stri
if (documentPath.StartsWith("http://") || documentPath.StartsWith("https://"))
{
var url = new Uri(new Uri(documentPath), jsonPath).ToString();
return await ResolveUrlReferenceWithAlreadyResolvedCheckAsync(url, jsonPath, targetType, contractResolver, append, cancellationToken ).ConfigureAwait(false);
return await ResolveUrlReferenceWithAlreadyResolvedCheckAsync(url, jsonPath, targetType, contractResolver, append, cancellationToken).ConfigureAwait(false);
}
else
{
Expand Down Expand Up @@ -305,8 +305,10 @@ private IJsonReference ResolveDocumentReferenceWithoutDereferencing(object obj,
return ResolveDocumentReference(extensionObj.ExtensionData[firstSegment], segments.Skip(1).ToList(), targetType, contractResolver, checkedObjects);
}

foreach (var member in obj.GetType().GetContextualPropertiesAndFields()
.Where(p => p.GetTypeAttribute<JsonIgnoreAttribute>() == null))
foreach (var member in obj
.GetType()
.GetContextualAccessors()
.Where(p => p.AccessorType.GetInheritedAttribute<JsonIgnoreAttribute>() == null))
{
var pathSegment = member.GetName();
if (pathSegment == firstSegment)
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/NJsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<WarningsAsErrors />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Namotion.Reflection" Version="1.0.23" />
<PackageReference Include="Namotion.Reflection" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
Expand Down
6 changes: 3 additions & 3 deletions src/NJsonSchema/Visitors/AsyncJsonReferenceVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ await VisitAsync(dictionary[key], path + "/" + key, key.ToString(), checkedObjec

// Custom dictionary type with additional properties (OpenApiPathItem)
var contextualType = obj.GetType().ToContextualType();
if (contextualType.TypeAttributes.OfType<JsonConverterAttribute>().Any())
if (contextualType.InheritedAttributes.OfType<JsonConverterAttribute>().Any())
{
foreach (var property in contextualType.Type.GetContextualProperties()
.Where(p => p.MemberInfo.DeclaringType == contextualType.Type &&
!p.ContextAttributes.OfType<JsonIgnoreAttribute>().Any()))
.Where(p => p.MemberInfo.DeclaringType == contextualType.Type &&
!p.GetContextAttributes<JsonIgnoreAttribute>().Any()))
{
var value = property.GetValue(obj);
if (value != null)
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema/Visitors/JsonReferenceVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ protected virtual void Visit(object obj, string path, string typeNameHint, ISet<

// Custom dictionary type with additional properties (OpenApiPathItem)
var contextualType = obj.GetType().ToContextualType();
if (contextualType.TypeAttributes.OfType<JsonConverterAttribute>().Any())
if (contextualType.GetInheritedAttributes<JsonConverterAttribute>().Any())
{
foreach (var property in contextualType.Type.GetContextualProperties()
.Where(p => p.MemberInfo.DeclaringType == contextualType.Type &&
!p.ContextAttributes.OfType<JsonIgnoreAttribute>().Any()))
!p.GetContextAttributes<JsonIgnoreAttribute>().Any()))
{
var value = property.GetValue(obj);
if (value != null)
Expand Down

0 comments on commit edbbe8d

Please sign in to comment.