Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Retrieve attributes only once in SystemTextJsonReflectionService.GenerateProperties #1757

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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;

Expand Down