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

fix(typedapi): unmarshal DateDecayFunction

Open dakimura opened this issue 8 months ago • 1 comments
trafficstars

WHAT

Add a custom UnmarshalJSON function to DateDecayFunction. Let me know if there's anything I can do to help with the automatic generation of this code!

It looks like these codes are automatically generated from the API specifications, but I wasn't sure how to generate them automatically and manually created this pull request for now....

WHY

There is an issue where deserialization does not work properly for DateDecayFunction, similar to elastic/go-elasticsearch#830. It seems necessary to implement the UnmarshalJSON function, just like in other parts of the code.

TEST

Verified that the values remain unchanged after performing Marshal & Unmarshal using the following test code. This test fails without this pull request.

package typedapi

import (
	"encoding/json"
	"testing"

	"github.com/google/go-cmp/cmp"

	"github.com/elastic/go-elasticsearch/v8/typedapi/types"
	"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/multivaluemode"
)

func TestUnmarshalDateDecayFunction(t *testing.T) {
	a := types.Float64(1)
	s := "2025-03-12"

	before := types.DateDecayFunction{
		DecayFunctionBaseDateMathDuration: map[string]types.DecayPlacementDateMathDuration{
			"example_field": {
				Decay:  &a,
				Origin: &s,
				Scale:  "1d",
			},
		},
		MultiValueMode: &multivaluemode.Min,
	}

	// marshal and unmarshal the DateDecayFunction object, compare diff
	v, err := json.Marshal(before)
	if err != nil {
		t.Fatal(err)
	}

	var after types.DateDecayFunction
	err = json.Unmarshal(v, &after)
	if err != nil {
		t.Fatal(err)
	}

	if d := cmp.Diff(before, after); d != "" {
		t.Errorf(d)
	}
}

dakimura avatar Mar 13 '25 01:03 dakimura

💚 CLA has been signed