Skip to content

RequestBodyInBytes is declared as non-nullable but can be null, causing compiler warnings #129

@DmytroBevziuk

Description

@DmytroBevziuk

Issue: RequestBodyInBytes is declared as non-nullable but can be null, causing compiler warnings

Description: I’m encountering an issue with the ElasticsearchClientSettings where the RequestBodyInBytes property is declared as a non-nullable byte[], but it can actually be null in certain cases. For example, when calling DeleteAsync, the RequestBodyInBytes is null because there’s no request body.

ElasticsearchClientSettings configuration:

var settings = new ElasticsearchClientSettings(new Uri(elasticsearchOptions.Url))
                .CertificateFingerprint(fingerprint)
                .Authentication(new BasicAuthentication(elasticsearchOptions.Username, elasticsearchOptions.Password))
                .EnableDebugMode(cd =>
                {
                    var requestBody = string.Empty;
                    
                    if (cd.RequestBodyInBytes != null && cd.RequestBodyInBytes.Length > 0)
                    {
                        requestBody = Encoding.Default.GetString(cd.RequestBodyInBytes);
                    }

                    logger.LogDebug("Debug info: {debugInfo}. Request: {request}", cd.DebugInformation, requestBody);
                })
                .PrettyJson();

            return new ElasticsearchClient(settings);

This part:

if (cd.RequestBodyInBytes != null && cd.RequestBodyInBytes.Length > 0)
{
    requestBody = Encoding.Default.GetString(cd.RequestBodyInBytes);
}

returns the warning "Expression is always true according to nullable reference types' annotations".

Is it possible to make this type nullable or don't assign null values?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions