From f77fe3e5bba2845fe8ee953023e27f26023633fb Mon Sep 17 00:00:00 2001 From: Daniel Stout Date: Sat, 7 Oct 2023 09:58:03 -0300 Subject: [PATCH] Add ability to customize which JsonProperties get ignored --- .../Generation/NewtonsoftJsonReflectionService.cs | 2 +- .../Generation/NewtonsoftJsonSchemaGeneratorSettings.cs | 3 +++ src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonReflectionService.cs b/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonReflectionService.cs index ed880c9f4..0b28f24f5 100644 --- a/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonReflectionService.cs +++ b/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonReflectionService.cs @@ -158,7 +158,7 @@ public override string GetPropertyName(ContextualAccessorInfo accessorInfo, Json private void LoadPropertyOrField(JsonProperty jsonProperty, ContextualAccessorInfo accessorInfo, Type parentType, JsonSchema parentSchema, NewtonsoftJsonSchemaGeneratorSettings settings, JsonSchemaGenerator schemaGenerator, JsonSchemaResolver schemaResolver) { var propertyTypeDescription = ((IReflectionService)this).GetDescription(accessorInfo.AccessorType, settings); - if (jsonProperty.Ignored == false && schemaGenerator.IsPropertyIgnoredBySettings(accessorInfo) == false) + if (!settings.IgnoreProperty(jsonProperty) && !schemaGenerator.IsPropertyIgnoredBySettings(accessorInfo)) { var propertyName = GetPropertyName(jsonProperty, accessorInfo, settings); var propertyAlreadyExists = parentSchema.Properties.ContainsKey(propertyName); diff --git a/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonSchemaGeneratorSettings.cs b/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonSchemaGeneratorSettings.cs index d7cc9630c..3aa9171b4 100644 --- a/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonSchemaGeneratorSettings.cs +++ b/src/NJsonSchema.NewtonsoftJson/Generation/NewtonsoftJsonSchemaGeneratorSettings.cs @@ -29,6 +29,9 @@ public NewtonsoftJsonSchemaGeneratorSettings() SerializerSettings = new JsonSerializerSettings(); } + /// Returns whether to ignore a . By default this is simply the "Ignored" property of JsonProperty + public Func IgnoreProperty = prop => prop.Ignored; + /// Gets or sets the Newtonsoft JSON serializer settings. [JsonIgnore] public JsonSerializerSettings SerializerSettings diff --git a/src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs b/src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs index 9bdce778b..eb1406ea8 100644 --- a/src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs +++ b/src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs @@ -18,6 +18,15 @@ public class Mno public string IgnoreMe; } + [Fact] + public async Task When_IgnoreJsonIgnore_is_specified_marked_fields_are_included() + { + var json = NewtonsoftJsonSchemaGenerator.FromType(new NewtonsoftJsonSchemaGeneratorSettings + { + IgnoreProperty = prop => false + }).ToJson(); + Assert.Contains("IgnoreMe", json); + } [Fact] public async Task When_field_has_JsonIgnoreAttribute_then_it_is_ignored()