Skip to content

PropertyNamingPolicy from JsonSerializerOptions of the DefaultSourceSerializer is not applied for mappings #8357

Closed
@stevejgordon

Description

@stevejgordon

Elastic.Clients.Elasticsearch version: Elastic.Clients.Elasticsearch.Serverless 1.1.10, but will also affect Elastic.Clients.Elasticsearch

Elasticsearch version: Serverless

.NET runtime version: 9.0.0-rc.1

Operating system version: Win 10

Description of the problem including expected versus actual behavior:
When customising the DefaultSourceSerializer with JsonSerializerOptions that includes the PropertyNamingPolicy, that option is not honoured when manually creating an index with property mappings.

Steps to reproduce:
Create an ElasticsearchClient:

var options = sp.GetRequiredService<IOptions<ElasticsearchOptions>>().Value;

static void ConfigureOptions(JsonSerializerOptions o) =>
    o.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;

var nodePool = new CloudNodePool(options.CloudId, new ApiKey(options.ApiKey));
var settings = new ElasticsearchClientSettings(
    nodePool,
    sourceSerializer: (defaultSerializer, settings) =>
        new DefaultSourceSerializer(settings, ConfigureOptions));

var es = ElasticsearchClient(settings);

Create an index with mappings:

var indexResponse = await es.Indices.CreateAsync<User>(i => i
    .Index("users.v1")
    .Mappings(m => m.Properties(p => p
        .Keyword(f => f.Id)
        .Keyword(f => f.Email)
        .Date(f => f.CreatedUtc))), ct);

Actual Mapping:

{
  "users.v1": {
    "mappings": {
      "properties": {
        "createdUtc": {
          "type": "date"
        },
        "email": {
          "type": "keyword"
        },
        "id": {
          "type": "keyword"
        }
      }
    }
  }
}

Expected Mapping:

{
  "users.v1": {
    "mappings": {
      "properties": {
        "created_utc": {
          "type": "date"
        },
        "email": {
          "type": "keyword"
        },
        "id": {
          "type": "keyword"
        }
      }
    }
  }
}

NOTE: The casing of createdUtc which should be created_utc.

Expected behavior
The inferred field names in the mapping should reflect the configured PropertyNamingPolicy from System.Text.Json.

The workaround is to use the JsonPropertyName attribute on the POCO properties to manually provide a snake-cased name.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions