RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.37k stars 529 forks source link

Method Resolution Conflict in `SystemTextJsonReflectionService` #1715

Open BlackGad opened 1 month ago

BlackGad commented 1 month ago

Description: In the SystemTextJsonReflectionService class, there are two methods named GetPropertyName with different signatures. The GenerateProperties method calls GetPropertyName, but the compiler resolves this call to the private static method, not the public override method. This makes it impossible to override the GetPropertyName method in a custom ReflectionServiceBase.

Steps to Reproduce:

  1. Create a custom ReflectionServiceBase class.
  2. Override the GetPropertyName method.
  3. Observe that the overridden method is not called in the GenerateProperties method.

Expected Behavior: The call to GetPropertyName in the GenerateProperties method should resolve to the public override method, allowing it to be overridden in a derived class.

Proposed Solution: Rename the static method to GetStaticPropertyName or modify its signature to avoid the conflict.

Code Example:

public override string GetPropertyName(ContextualAccessorInfo accessorInfo, JsonSchemaGeneratorSettings settings);
private static string GetPropertyName(ContextualAccessorInfo accessorInfo, SystemTextJsonSchemaGeneratorSettings settings);

public void GenerateProperties(...)
{
    var propertyTypeDescription = GetDescription(accessorInfo.AccessorType, settings.DefaultReferenceTypeNullHandling, settings);
    var propertyName = GetPropertyName(accessorInfo, settings); // This resolves to the static method
}