Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add ability to customize which JsonProperties get ignored #1635

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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);
Expand Down
Expand Up @@ -29,6 +29,9 @@ public NewtonsoftJsonSchemaGeneratorSettings()
SerializerSettings = new JsonSerializerSettings();
}

/// <summary> Returns whether to ignore a <see cref="JsonProperty"/>. By default this is simply the "Ignored" property of JsonProperty</summary>
public Func<JsonProperty, bool> IgnoreProperty = prop => prop.Ignored;

/// <summary>Gets or sets the Newtonsoft JSON serializer settings.</summary>
[JsonIgnore]
public JsonSerializerSettings SerializerSettings
Expand Down
9 changes: 9 additions & 0 deletions src/NJsonSchema.Tests/Generation/IgnoredPropertyTests.cs
Expand Up @@ -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<Mno>(new NewtonsoftJsonSchemaGeneratorSettings
{
IgnoreProperty = prop => false
}).ToJson();
Assert.Contains("IgnoreMe", json);
}

[Fact]
public async Task When_field_has_JsonIgnoreAttribute_then_it_is_ignored()
Expand Down