domaindrivendev/Swashbuckle.AspNetCore

Please annotate SchemaRepository.TryLookupByType

Aperta

#3659 aperta il 15 nov 2025

 (3 commenti) (2 reazioni) (0 assegnatari)C# (1341 fork)batch import
good first issuehelp-wanted

Metriche repository

Star
 (5495 stelle)
Metriche merge PR
 (Merge medio 3g 14h) (45 PR mergiate in 30 g)

Descrizione

I was reviewing some code and noticed a hidden bug: null could be returned, but there was no compiler warning. This is because TryLookupByType lacks the [NotNullWhen(true)] annotation, which is used, for example, in IPAddress.TryParse.

It would be helpful if SchemaRepository.TryLookupByType were annotated for nullability, similar to my workaround below:

#nullable enable
public static bool TryLookupByTypeSafe(this SchemaRepository schemaRepository,
    Type type, [NotNullWhen(true)] out OpenApiSchemaReference? referenceSchema)
{
    bool result = schemaRepository.TryLookupByType(type,
        out OpenApiSchemaReference? obliviousReferenceSchema);
    referenceSchema = result ? obliviousReferenceSchema : null;
    return result;
}
#nullable restore

And then the hidden bug becomes a compiler warning:

Guida contributor