From fa484cfa1e886801cc9c88c101848c7582bbd499 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Sun, 24 Nov 2024 19:48:47 +0200 Subject: [PATCH] Retrieve attributes only once in SystemTextJsonReflectionService.GenerateProperties --- .../Generation/SystemTextJsonReflectionService.cs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs index 17e4ff93c..4ee6339eb 100644 --- a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs +++ b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs @@ -50,9 +50,8 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex } var propertyIgnored = false; - var jsonIgnoreAttribute = accessorInfo - .GetAttributes(true) - .FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonIgnoreAttribute", TypeNameStyle.FullName); + var attributes = accessorInfo.GetAttributes(true).ToArray(); + var jsonIgnoreAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonIgnoreAttribute", TypeNameStyle.FullName); if (jsonIgnoreAttribute != null) { @@ -65,9 +64,8 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex var ignored = propertyIgnored || schemaGenerator.IsPropertyIgnoredBySettings(accessorInfo) - || accessorInfo.GetAttributes(true) - .FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) != null - || settings.ExcludedTypeNames.Contains(accessorInfo.AccessorType.Type.FullName); + || attributes.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) != null + || Array.IndexOf(settings.ExcludedTypeNames, accessorInfo.AccessorType.Type.FullName) != -1; if (!ignored) { @@ -87,9 +85,7 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex } } - var requiredAttribute = accessorInfo - .GetAttributes(true) - .FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RequiredAttribute"); + var requiredAttribute = attributes.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RequiredAttribute"); var isDataContractMemberRequired = schemaGenerator.GetDataMemberAttribute(accessorInfo, contextualType.Type)?.IsRequired == true;