Skip to content

Custom Json serializer options not respected in ImportDocuments #290

@cala-br

Description

@cala-br

Hi, we have a collection that has the fields written in snake case.

When registering the client, we provide a custom json serializer.

builder.Services.AddTypesenseClient(x =>
{
    x.ApiKey = typesenseOptions.ApiKey;
    x.Nodes =
    [
        new Node(
            host: typesenseOptions.Url.Host,
            port: typesenseOptions.Url.Port.ToString(),
            protocol: typesenseOptions.Url.Scheme
        )
    ];

    x.JsonSerializerOptions ??= new JsonSerializerOptions();
    x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
    x.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
});

While the methods that are used for reading (such as SearchInternal) use _jsonNameCaseInsensitiveTrue, and thus respect the custom json serializer options, the ImportDocuments method uses the _jsonOptionsCamelCaseIgnoreWritingNull.

This field, when set, overrides the custom options with -- as the name implies -- the CamelCase naming policy, thus making the import fail.

Line 57

if (config.Value.JsonSerializerOptions is not null)
{
_jsonNameCaseInsensitiveTrue = new JsonSerializerOptions(config.Value.JsonSerializerOptions)
{
PropertyNameCaseInsensitive = true
};
_jsonOptionsCamelCaseIgnoreWritingNull = new JsonSerializerOptions(config.Value.JsonSerializerOptions)
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
}

Is this behavior intended?

As of now, we've managed to circumvent this problem by serializing the documents manually, and then passing them to the import method, but if feels a bit "dirty".

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