Skip to content

Commit

Permalink
Update Namotion.Reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Suter committed Oct 31, 2023
1 parent 164520b commit cce0e36
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Namotion.Reflection" Version="3.0.1" />
<PackageVersion Include="Namotion.Reflection" Version="3.1.0" />
<PackageVersion Include="NBench" Version="2.0.1" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="NodaTime" Version="3.1.9" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class NewtonsoftJsonReflectionService : ReflectionServiceBase<NewtonsoftJ
/// <inheritdoc />
public override bool IsNullable(ContextualType contextualType, ReferenceTypeNullHandling defaultReferenceTypeNullHandling)
{
var jsonPropertyAttribute = contextualType.GetContextAttribute<JsonPropertyAttribute>();
var jsonPropertyAttribute = contextualType.GetContextAttribute<JsonPropertyAttribute>(true);
if (jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.DisallowNull)
{
return false;
Expand Down Expand Up @@ -117,7 +117,7 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex
// TODO: Remove this hacky code (used to support serialization of exceptions and restore the old behavior [pre 9.x])
foreach (var accessorInfo in contextualAccessors.Where(m => allowedProperties == null || allowedProperties.Contains(m.Name)))
{
var attribute = accessorInfo.GetContextAttribute<JsonPropertyAttribute>();
var attribute = accessorInfo.GetAttribute<JsonPropertyAttribute>(true);
var memberType = (accessorInfo as ContextualPropertyInfo)?.PropertyInfo.PropertyType ??
(accessorInfo as ContextualFieldInfo)?.FieldInfo.FieldType;

Expand Down Expand Up @@ -173,7 +173,9 @@ private void LoadPropertyOrField(JsonProperty jsonProperty, ContextualAccessorIn
}
}

var requiredAttribute = accessorInfo.ContextAttributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RequiredAttribute");
var requiredAttribute = accessorInfo
.GetAttributes(true)
.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RequiredAttribute");

var hasJsonNetAttributeRequired = jsonProperty.Required == Required.Always || jsonProperty.Required == Required.AllowNull;
var isDataContractMemberRequired = schemaGenerator.GetDataMemberAttribute(accessorInfo, parentType)?.IsRequired == true;
Expand Down
11 changes: 6 additions & 5 deletions src/NJsonSchema/Generation/DefaultSchemaNameGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public virtual string Generate(Type type)
{
var cachedType = type.ToCachedType();

var jsonSchemaAttribute = cachedType.GetInheritedAttribute<JsonSchemaAttribute>();
var jsonSchemaAttribute = cachedType.GetAttribute<JsonSchemaAttribute>(true);

if (!string.IsNullOrEmpty(jsonSchemaAttribute?.Name))
{
return jsonSchemaAttribute!.Name!;
Expand All @@ -44,10 +45,10 @@ public virtual string Generate(Type type)
private static string GetName(CachedType cType)
{
return
cType.TypeName == "Int16" ? GetNullableDisplayName(cType, "Short") :
cType.TypeName == "Int32" ? GetNullableDisplayName(cType, "Integer") :
cType.TypeName == "Int64" ? GetNullableDisplayName(cType, "Long") :
GetNullableDisplayName(cType, cType.TypeName);
cType.Name == "Int16" ? GetNullableDisplayName(cType, "Short") :
cType.Name == "Int32" ? GetNullableDisplayName(cType, "Integer") :
cType.Name == "Int64" ? GetNullableDisplayName(cType, "Long") :
GetNullableDisplayName(cType, cType.Name);
}

private static string GetNullableDisplayName(CachedType type, string actual)
Expand Down
81 changes: 40 additions & 41 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions src/NJsonSchema/Generation/ReflectionServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public JsonTypeDescription GetDescription(ContextualType contextualType, Referen
var type = contextualType.OriginalType;
var isNullable = IsNullable(contextualType, defaultReferenceTypeNullHandling);

var jsonSchemaTypeAttribute = contextualType.GetAttribute<JsonSchemaTypeAttribute>();
var jsonSchemaTypeAttribute = contextualType.GetContextOrTypeAttribute<JsonSchemaTypeAttribute>(true);
if (jsonSchemaTypeAttribute != null)
{
type = jsonSchemaTypeAttribute.Type;
Expand All @@ -50,7 +50,7 @@ public JsonTypeDescription GetDescription(ContextualType contextualType, Referen
}
}

var jsonSchemaAttribute = contextualType.GetAttribute<JsonSchemaAttribute>();
var jsonSchemaAttribute = contextualType.GetContextOrTypeAttribute<JsonSchemaAttribute>(true); ;
if (jsonSchemaAttribute != null)
{
var classType = jsonSchemaAttribute.Type != JsonObjectType.None ? jsonSchemaAttribute.Type : JsonObjectType.Object;
Expand Down Expand Up @@ -235,12 +235,12 @@ protected virtual JsonTypeDescription GetDescription(ContextualType contextualTy
/// <returns>true if the type can be null.</returns>
public virtual bool IsNullable(ContextualType contextualType, ReferenceTypeNullHandling defaultReferenceTypeNullHandling)
{
if (contextualType.ContextAttributes.FirstAssignableToTypeNameOrDefault("NotNullAttribute", TypeNameStyle.Name) != null)
if (contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("NotNullAttribute", TypeNameStyle.Name) != null)
{
return false;
}

if (contextualType.ContextAttributes.FirstAssignableToTypeNameOrDefault("CanBeNullAttribute", TypeNameStyle.Name) != null)
if (contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("CanBeNullAttribute", TypeNameStyle.Name) != null)
{
return true;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ protected virtual bool IsBinary(ContextualType contextualType)
{
// TODO: Move all file handling to NSwag. How?

var parameterTypeName = contextualType.TypeName;
var parameterTypeName = contextualType.Name;
return parameterTypeName == "IFormFile" ||
contextualType.IsAssignableToTypeName("HttpPostedFile", TypeNameStyle.Name) ||
contextualType.IsAssignableToTypeName("HttpPostedFileBase", TypeNameStyle.Name) ||
Expand All @@ -298,7 +298,7 @@ protected virtual bool IsBinary(ContextualType contextualType)
/// <returns>true or false.</returns>
private bool IsIAsyncEnumerableType(ContextualType contextualType)
{
return contextualType.TypeName == "IAsyncEnumerable`1";
return contextualType.Name == "IAsyncEnumerable`1";
}

/// <summary>Checks whether the given type is an array type.</summary>
Expand All @@ -311,7 +311,7 @@ protected virtual bool IsArrayType(ContextualType contextualType)
return false;
}

if (contextualType.TypeName == "ObservableCollection`1")
if (contextualType.Name == "ObservableCollection`1")
{
return true;
}
Expand All @@ -327,7 +327,7 @@ protected virtual bool IsArrayType(ContextualType contextualType)
/// <returns>true or false.</returns>
protected virtual bool IsDictionaryType(ContextualType contextualType)
{
if (contextualType.TypeName == "IDictionary`2" || contextualType.TypeName == "IReadOnlyDictionary`2")
if (contextualType.Name == "IDictionary`2" || contextualType.Name == "IReadOnlyDictionary`2")
{
return true;
}
Expand All @@ -339,7 +339,10 @@ protected virtual bool IsDictionaryType(ContextualType contextualType)

private bool HasStringEnumConverter(ContextualType contextualType)
{
dynamic? jsonConverterAttribute = contextualType.Attributes?.FirstOrDefault(a => a.GetType().Name == "JsonConverterAttribute");
dynamic? jsonConverterAttribute = contextualType
.GetContextOrTypeAttributes(true)?
.FirstOrDefault(a => a.GetType().Name == "JsonConverterAttribute");

if (jsonConverterAttribute != null && ObjectExtensions.HasProperty(jsonConverterAttribute, "ConverterType"))
{
var converterType = jsonConverterAttribute?.ConverterType as Type;
Expand Down
14 changes: 8 additions & 6 deletions src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex
}

if (accessorInfo.Name == "EqualityContract" &&
accessorInfo.ContextAttributes.OfType<CompilerGeneratedAttribute>().Any())
accessorInfo.IsAttributeDefined<CompilerGeneratedAttribute>(true))
{
continue;
}

var propertyIgnored = false;
var jsonIgnoreAttribute = accessorInfo
.ContextAttributes
.GetAttributes(true)
.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonIgnoreAttribute", TypeNameStyle.FullName);

if (jsonIgnoreAttribute != null)
Expand All @@ -61,8 +61,8 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex

var ignored = propertyIgnored
|| schemaGenerator.IsPropertyIgnoredBySettings(accessorInfo)
|| accessorInfo.ContextAttributes
.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) != null;
|| accessorInfo.GetAttributes(true)
.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) != null;

if (!ignored)
{
Expand All @@ -82,7 +82,9 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex
}
}

var requiredAttribute = accessorInfo.ContextAttributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RequiredAttribute");
var requiredAttribute = accessorInfo
.GetAttributes(true)
.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RequiredAttribute");

var isDataContractMemberRequired = schemaGenerator.GetDataMemberAttribute(accessorInfo, contextualType.Type)?.IsRequired == true;

Expand Down Expand Up @@ -132,7 +134,7 @@ public override string GetPropertyName(ContextualAccessorInfo accessorInfo, Json

private static string GetPropertyName(ContextualAccessorInfo accessorInfo, SystemTextJsonSchemaGeneratorSettings settings)
{
dynamic? jsonPropertyNameAttribute = accessorInfo.ContextAttributes
dynamic? jsonPropertyNameAttribute = accessorInfo.GetAttributes(true)
.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonPropertyNameAttribute", TypeNameStyle.FullName);

if (!string.IsNullOrEmpty(jsonPropertyNameAttribute?.Name))
Expand Down
14 changes: 8 additions & 6 deletions src/NJsonSchema/Infrastructure/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ public static string GetName(this ContextualAccessorInfo accessorInfo)

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

var dataMemberAttribute = accessorInfo.AccessorType.GetContextAttribute<DataMemberAttribute>();
var dataMemberAttribute = accessorInfo.GetAttribute<DataMemberAttribute>(true);
if (dataMemberAttribute != null && !string.IsNullOrEmpty(dataMemberAttribute.Name))
{
var dataContractAttribute = accessorInfo
.MemberInfo
.DeclaringType?
.ToCachedType()
.GetInheritedAttribute<DataContractAttribute>();
.GetAttribute<DataContractAttribute>(true);

if (dataContractAttribute != null)
{
Expand All @@ -89,7 +89,9 @@ private static string GetNameWithoutCache(ContextualAccessorInfo accessorInfo)
/// <returns>The description or null if no description is available.</returns>
public static string? GetDescription(this CachedType type, IXmlDocsSettings xmlDocsSettings)
{
var attributes = type is ContextualType contextualType ? contextualType.ContextAttributes : type.InheritedAttributes;
var attributes = type is ContextualType contextualType ?
contextualType.GetContextOrTypeAttributes<Attribute>(true) :
type.GetAttributes(true);

var description = GetDescription(attributes);
if (description != null)
Expand All @@ -115,7 +117,7 @@ private static string GetNameWithoutCache(ContextualAccessorInfo accessorInfo)
/// <returns>The description or null if no description is available.</returns>
public static string? GetDescription(this ContextualAccessorInfo accessorInfo, IXmlDocsSettings xmlDocsSettings)
{
var description = GetDescription(accessorInfo.AccessorType.Attributes);
var description = GetDescription(accessorInfo.GetAttributes(true));
if (description != null)
{
return description;
Expand All @@ -139,7 +141,7 @@ private static string GetNameWithoutCache(ContextualAccessorInfo accessorInfo)
/// <returns>The description or null if no description is available.</returns>
public static string? GetDescription(this ContextualParameterInfo parameter, IXmlDocsSettings xmlDocsSettings)
{
var description = GetDescription(parameter.ContextAttributes);
var description = GetDescription(parameter.GetAttributes(true));
if (description != null)
{
return description;
Expand Down
14 changes: 7 additions & 7 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().InheritedAttributes;
var attributes = type.ToCachedType().GetAttributes(true);
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.InheritedAttributes;
var attributes = type.GetAttributes(true);
dynamic? xmlTypeAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlTypeAttribute");

var itemName = GetXmlItemName(type.OriginalType);
Expand All @@ -73,14 +73,14 @@ public static void GenerateXmlObjectForProperty(this JsonSchemaProperty property

if (propertySchema.IsArray)
{
dynamic? xmlArrayAttribute = type.Attributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlArrayAttribute");
dynamic? xmlArrayAttribute = type.GetContextOrTypeAttributes<Attribute>(true).FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlArrayAttribute");
if (xmlArrayAttribute != null)
{
xmlName = xmlArrayAttribute.ElementName;
xmlNamespace = xmlArrayAttribute.Namespace;
}

dynamic? xmlArrayItemsAttribute = type.Attributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlArrayItemAttribute");
dynamic? xmlArrayItemsAttribute = type.GetContextOrTypeAttributes<Attribute>(true).FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlArrayItemAttribute");
if (xmlArrayItemsAttribute != null)
{
var xmlItemName = xmlArrayItemsAttribute.ElementName;
Expand All @@ -92,14 +92,14 @@ public static void GenerateXmlObjectForProperty(this JsonSchemaProperty property
xmlWrapped = true;
}

dynamic? xmlElementAttribute = type.Attributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlElementAttribute");
dynamic? xmlElementAttribute = type.GetContextOrTypeAttributes<Attribute>(true).FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlElementAttribute");
if (xmlElementAttribute != null)
{
xmlName = xmlElementAttribute.ElementName;
xmlNamespace = xmlElementAttribute.Namespace;
}

dynamic? xmlAttribute = type.Attributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlAttributeAttribute");
dynamic? xmlAttribute = type.GetContextOrTypeAttributes<Attribute>(true).FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlAttributeAttribute");
if (xmlAttribute != null)
{
if (!string.IsNullOrEmpty(xmlAttribute.AttributeName))
Expand All @@ -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.InheritedAttributes.FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlTypeAttribute");
dynamic? xmlReferenceTypeAttribute = type.GetAttributes(true).FirstAssignableToTypeNameOrDefault("System.Xml.Serialization.XmlTypeAttribute");
if (xmlReferenceTypeAttribute != null)
{
xmlName = propertyName;
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/JsonReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private async Task<IJsonReference> ResolveUrlReferenceWithAlreadyResolvedCheckAs
foreach (var member in obj
.GetType()
.GetContextualAccessors()
.Where(p => p.AccessorType.GetInheritedAttribute<JsonIgnoreAttribute>() == null))
.Where(p => p.IsAttributeDefined<JsonIgnoreAttribute>(true) == false))
{
var pathSegment = member.GetName();
if (pathSegment == firstSegment)
Expand Down

0 comments on commit cce0e36

Please sign in to comment.