Skip to content

Scripted fields not mapped correctly during reading #3022

Closed
@llosimura

Description

@llosimura

I just noticed that if we set a custom name for a scripted field, it won't be mapped correctly to the entity in question while reading it.

Example:

@Document("my-test")
class ScriptFieldProperty {
     ...
     @ScriptedField(name = "scripted-field-property")
     String scriptedFieldProperty;
}

I did some investigation and noticed that when we moved the populateScriptFields method from using ReflectionUtils to PersitenceEntity on 9d57410, this condition:

 if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) {

will never be true for custom names,since property#getNames is unaware of the ScriptedField annotation. In the previous example, it will always return scriptedFieldProperty instead of the expected scripted-field-property

I can see two ways of solving the issue.

  1. Remove the second part of the conditional and do the check internally. The name should come first from the annotation and only fall back to the property if it is not set. (Note this is already done before setting the value)
  2. Make SimpleElasticsearchPersistentProperty aware of the ScriptedField annotation. By adding a condition to check it in SimpleElasticsearchPersistentProperty#getAnnotatedFieldName, we can also ensure that the property has the correct name when populating the scripted fields.

I'm unsure of any other valid solution and would love some input. I have both solutions implemented locally and can create a PR if needed

Update: created a PR with the changes here #3023

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions