Skip to content

Commit

Permalink
Retrieve attributes only once in JsonSchemaGenerator.Apply.DataAnnota…
Browse files Browse the repository at this point in the history
…tions (#1756)
  • Loading branch information
lahma authored Nov 24, 2024
1 parent 953ab69 commit 1a7c0ec
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public virtual void Generate<TSchemaType>(TSchemaType schema, ContextualType con
{
var typeDescription = Settings.ReflectionService.GetDescription(contextualType, Settings);

JsonSchemaGenerator.ApplyTypeExtensionDataAttributes(schema, contextualType);
ApplyTypeExtensionDataAttributes(schema, contextualType);

if (TryHandleSpecialTypes(schema, typeDescription.ContextualType, schemaResolver))
{
Expand Down Expand Up @@ -336,7 +336,8 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription
{
var contextualType = typeDescription.ContextualType;

dynamic? displayAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.DisplayAttribute");
var attributes = contextualType.GetContextAttributes(true).ToArray();
dynamic? displayAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.DisplayAttribute");
if (displayAttribute != null)
{
// GetName returns null if the Name property on the attribute is not specified.
Expand All @@ -347,7 +348,7 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription
}
}

dynamic? defaultValueAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("System.ComponentModel.DefaultValueAttribute");
dynamic? defaultValueAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DefaultValueAttribute");
if (defaultValueAttribute != null)
{
if (typeDescription.IsEnum &&
Expand All @@ -361,7 +362,7 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription
}
}

dynamic? regexAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RegularExpressionAttribute");
dynamic? regexAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RegularExpressionAttribute");
if (regexAttribute != null)
{
if (typeDescription.IsDictionary)
Expand All @@ -378,7 +379,7 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription

if (typeDescription.Type is JsonObjectType.Number or JsonObjectType.Integer)
{
JsonSchemaGenerator.ApplyRangeAttribute(schema, contextualType.GetContextAttributes(true));
ApplyRangeAttribute(schema, attributes);

var multipleOfAttribute = contextualType.GetContextAttribute<MultipleOfAttribute>(true);
if (multipleOfAttribute != null)
Expand All @@ -387,7 +388,7 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription
}
}

dynamic? minLengthAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.MinLengthAttribute");
dynamic? minLengthAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.MinLengthAttribute");
if (minLengthAttribute?.Length != null)
{
if (typeDescription.Type == JsonObjectType.String)
Expand All @@ -400,7 +401,7 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription
}
}

dynamic? maxLengthAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.MaxLengthAttribute");
dynamic? maxLengthAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.MaxLengthAttribute");
if (maxLengthAttribute?.Length != null)
{
if (typeDescription.Type == JsonObjectType.String)
Expand All @@ -413,7 +414,7 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription
}
}

dynamic? stringLengthAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.StringLengthAttribute");
dynamic? stringLengthAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.StringLengthAttribute");
if (stringLengthAttribute != null)
{
if (typeDescription.Type == JsonObjectType.String)
Expand All @@ -423,13 +424,13 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription
}
}

dynamic? dataTypeAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.DataTypeAttribute");
dynamic? dataTypeAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.DataTypeAttribute");
if (dataTypeAttribute != null)
{
var dataType = dataTypeAttribute.DataType.ToString();
if (DataTypeFormats.ContainsKey(dataType))
if (DataTypeFormats.TryGetValue(dataType, out string format))
{
schema.Format = DataTypeFormats[dataType];
schema.Format = format;
}
}
}
Expand Down Expand Up @@ -618,13 +619,14 @@ protected virtual void GenerateArray<TSchemaType>(
schema.Item = JsonSchema.CreateAnySchema();
}

dynamic? minLengthAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("MinLengthAttribute", TypeNameStyle.Name);
var attributes = contextualType.GetContextAttributes(true).ToArray();
dynamic? minLengthAttribute = attributes.FirstAssignableToTypeNameOrDefault("MinLengthAttribute", TypeNameStyle.Name);
if (minLengthAttribute != null && ObjectExtensions.HasProperty(minLengthAttribute, "Length"))
{
schema.MinItems = minLengthAttribute?.Length;
}

dynamic? maxLengthAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("MaxLengthAttribute", TypeNameStyle.Name);
dynamic? maxLengthAttribute = attributes.FirstAssignableToTypeNameOrDefault("MaxLengthAttribute", TypeNameStyle.Name);
if (maxLengthAttribute != null && ObjectExtensions.HasProperty(maxLengthAttribute, "Length"))
{
schema.MaxItems = maxLengthAttribute?.Length;
Expand Down Expand Up @@ -653,7 +655,9 @@ protected virtual void GenerateDictionary<TSchemaType>(TSchemaType schema, JsonT

var valueType = genericTypeArguments.Length == 2 ? genericTypeArguments[1] : typeof(object).ToContextualType();

var patternPropertiesAttributes = contextualType.GetContextAttributes(true).OfType<JsonSchemaPatternPropertiesAttribute>();
var attributes = contextualType.GetContextAttributes(true).ToArray();

var patternPropertiesAttributes = attributes.OfType<JsonSchemaPatternPropertiesAttribute>();
if (patternPropertiesAttributes.Any())
{
schema.AllowAdditionalProperties = false;
Expand All @@ -670,13 +674,13 @@ protected virtual void GenerateDictionary<TSchemaType>(TSchemaType schema, JsonT
schema.AllowAdditionalProperties = true;
}

dynamic? minLengthAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("MinLengthAttribute", TypeNameStyle.Name);
dynamic? minLengthAttribute = attributes.FirstAssignableToTypeNameOrDefault("MinLengthAttribute", TypeNameStyle.Name);
if (minLengthAttribute != null && ObjectExtensions.HasProperty(minLengthAttribute, "Length"))
{
schema.MinProperties = minLengthAttribute?.Length;
}

dynamic? maxLengthAttribute = contextualType.GetContextAttributes(true).FirstAssignableToTypeNameOrDefault("MaxLengthAttribute", TypeNameStyle.Name);
dynamic? maxLengthAttribute = attributes.FirstAssignableToTypeNameOrDefault("MaxLengthAttribute", TypeNameStyle.Name);
if (maxLengthAttribute != null && ObjectExtensions.HasProperty(maxLengthAttribute, "Length"))
{
schema.MaxProperties = maxLengthAttribute?.Length;
Expand Down Expand Up @@ -1210,7 +1214,7 @@ public void AddProperty(
propertySchema.Default = ConvertDefaultValue(property.AccessorType, defaultValue);

ApplyDataAnnotations(propertySchema, propertyTypeDescription);
JsonSchemaGenerator.ApplyPropertyExtensionDataAttributes(propertySchema, property);
ApplyPropertyExtensionDataAttributes(propertySchema, property);
};

var referencingProperty = GenerateWithReferenceAndNullability(
Expand Down

0 comments on commit 1a7c0ec

Please sign in to comment.