Skip to content

Commit

Permalink
fix STJ read only properties handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mereth committed Apr 28, 2024
1 parent 083501d commit f85ca24
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
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

0 comments on commit f85ca24

Please sign in to comment.