elasticsearch-net icon indicating copy to clipboard operation
elasticsearch-net copied to clipboard

PutMappingAsync fails with Bad Request when PutMappingRequest.DynamicTemplates contains just one element

Open yansklyarenko opened this issue 8 months ago • 4 comments

Elastic.Clients.Elasticsearch version: 8.10.0

Elasticsearch version: 8.2.2

.NET runtime version: net6.0, net7.0

Operating system version: Windows 10

Description of the problem including expected versus actual behavior: Consider the following code sample:

var myTemplate = new DynamicTemplate
{
    PathMatch = "testPathMatch",
    Mapping = new KeywordProperty()
};

var putMappingRequest = new PutMappingRequest("my-index")
{
    DynamicTemplates = new[]
    {
        new Dictionary<string, DynamicTemplate>
        {
            { "testTemplateName", myTemplate }
        }
    }
};

var response = await client.Indices.PutMappingAsync(putMappingRequest).ConfigureAwait(false);

Actual behavior The response is invalid, containing 400 bad request:

Invalid Elasticsearch response built from a unsuccessful (400) low level call on PUT: /repository-integration-tests/_mapping?pretty=true&error_trace=true
 Exception: Request failed to execute. Call: Status code 400 from: PUT /repository-integration-tests/_mapping?pretty=true&error_trace=true. ServerError: Type: mapper_parsing_exception Reason: "Failed to parse mapping: Dynamic template syntax error. An array of named objects is expected." CausedBy: "Type: mapper_parsing_exception Reason: "Dynamic template syntax error. An array of named objects is expected.""

Expected behavior The appropriate mapping is added to the index.

Some details which might be useful As soon as I add another element to the DynamicTemplates array, the request passes just fine and the mapping is created. This is because the DynamicTemplates property is decorated with [SingleOrManyCollectionConverter(typeof(IDictionary<string, DynamicTemplate>))], which serializes the array with a single element as an object, not as an array.

Based on this comment from another issue, I make a conclusion that it was done on purpose. So, it is not clear whether it is the 'put mapping' endpoint problem that it doesn't understand the new body format, or the serialization logic.

Please, let me know if you need some more details.

yansklyarenko avatar Oct 11 '23 14:10 yansklyarenko