Skip to content

fix(typedapi): unmarshal DateDecayFunction #966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dakimura
Copy link

@dakimura dakimura commented Mar 13, 2025

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)
	}
}

Copy link

cla-checker-service bot commented Mar 13, 2025

💚 CLA has been signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant