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

Fix SystemTextJson read only properties handling #1697

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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public class HealthCheckResult
public string Name { get; }

public string Description { get; }

private string PrivateReadOnlyProperty1 { get; }

private string PrivateReadOnlyProperty2 => "TEST";

public static string PublicReadOnlyStaticProperty { get; }

private static string PrivateReadOnlyStaticProperty { get; }
}

[Fact]
Expand All @@ -26,5 +34,31 @@ public async Task When_property_is_readonly_then_its_in_the_schema()
Assert.Contains(@"Name", data);
Assert.Contains(@"Description", data);
}

[Fact]
public async Task When_property_is_private_and_readonly_then_its_not_in_the_schema()
{
//// Act
var schema = JsonSchema.FromType<HealthCheckResult>();
var data = schema.ToJson();

//// Assert
Assert.NotNull(data);
Assert.False(data.Contains("PrivateReadOnlyProperty1"), data);
Assert.False(data.Contains("PrivateReadOnlyProperty2"), data);
}

[Fact]
public async Task When_property_is_static_readonly_then_its_not_in_the_schema()
{
//// Act
var schema = JsonSchema.FromType<HealthCheckResult>();
var data = schema.ToJson();

//// Assert
Assert.NotNull(data);
Assert.False(data.Contains("PublicReadOnlyStaticProperty"), data);
Assert.False(data.Contains("PrivateReadOnlyStaticProperty"), data);
}
}
}
4 changes: 2 additions & 2 deletions src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex
}

if (accessorInfo.MemberInfo is PropertyInfo propertyInfo &&
(propertyInfo.GetMethod?.IsPrivate == true || propertyInfo.GetMethod?.IsStatic == true) &&
(propertyInfo.SetMethod?.IsPrivate == true || propertyInfo.SetMethod?.IsStatic == true) &&
(propertyInfo.GetMethod == null || propertyInfo.GetMethod.IsPrivate == true || propertyInfo.GetMethod.IsStatic == true) &&
(propertyInfo.SetMethod == null || propertyInfo.SetMethod.IsPrivate == true || propertyInfo.SetMethod.IsStatic == true) &&
!propertyInfo.IsDefined(typeof(DataMemberAttribute)))
{
continue;
Expand Down