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

Private expression-bodied read-only properties should be ignored #1694

Open
mereth opened this issue Apr 20, 2024 · 1 comment · May be fixed by #1697
Open

Private expression-bodied read-only properties should be ignored #1694

mereth opened this issue Apr 20, 2024 · 1 comment · May be fixed by #1697

Comments

@mereth
Copy link

mereth commented Apr 20, 2024

Hi,

I'm currently encountering an issue with a generated OpenAPI schema that seems to be related on how properties are filtered by the SystemTextJsonReflectionService implementations.

public class TestClass
{
    private string Prop1 { get; set; }

    private string Prop2 { get; } = "Hello";

    private string Prop3 => "Hello";
}

Both Prop2 and Prop3 are not filtered because they don't have a setter.

The excluding test, extracted from SystemTextJsonReflectionService, doesn't seem handle setter or getter nullability properly :

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

Unlike the including test, from NewtonsoftJsonReflectionService

contextualType
.Properties
.Where(p => p.PropertyInfo.DeclaringType == contextualType.Type &&
            (p.PropertyInfo.GetMethod?.IsPrivate != true && p.PropertyInfo.GetMethod?.IsStatic == false ||
             p.PropertyInfo.SetMethod?.IsPrivate != true && p.PropertyInfo.SetMethod?.IsStatic == false ||
             p.PropertyInfo.IsDefined(typeof(DataMemberAttribute))))

I think the SystemTextJsonReflectionService test should changed for:

if (accessorInfo.MemberInfo is PropertyInfo propertyInfo &&
    (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;
}

Regards

@altso
Copy link

altso commented Apr 24, 2024

I believe it's what causing RicoSuter/NSwag#4681.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants