Skip to content
Merged
Show file tree
Hide file tree
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
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