Skip to content

Commit 7c09c48

Browse files
committed
Fix validation of number types
1 parent 901036e commit 7c09c48

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

internal/fields/mappings.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"path/filepath"
1313
"slices"
14+
"strings"
1415

1516
"github.com/Masterminds/semver/v3"
1617
"github.com/google/go-cmp/cmp"
@@ -687,10 +688,13 @@ func (v *MappingValidator) validateObjectMappingAndParameters(previewValue, actu
687688
return errs
688689
}
689690

691+
// Strings from `json.Marshal` include double quotes, so they need to be removed (e.g. "\"float\"")
692+
previewDataString := strings.ReplaceAll(string(previewData), "\"", "")
693+
actualDataString := strings.ReplaceAll(string(actualData), "\"", "")
690694
// exceptions related to numbers
691695
// https://github.com/elastic/elastic-package/blob/8cc126ae5015dd336b22901c365e8c98db4e7c15/internal/fields/validate.go#L1234-L1247
692-
if isNumberTypeField(string(previewData), string(actualData)) {
693-
logger.Debugf("Allowed number fields with different types (preview %s - actual %s)", string(previewData), string(actualData))
696+
if isNumberTypeField(previewDataString, actualDataString) {
697+
logger.Debugf("Allowed number fields with different types (preview %s - actual %s)", previewDataString, actualDataString)
694698
return nil
695699
}
696700

internal/fields/mappings_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,30 @@ func TestComparingMappings(t *testing.T) {
569569
`unexpected value found in mapping for field "foo.type.ignore_above": preview mappings value (1024) different from the actual mappings value (2048)`,
570570
},
571571
},
572+
{
573+
title: "different number types",
574+
preview: map[string]any{
575+
"foo": map[string]any{
576+
"type": "float",
577+
},
578+
},
579+
actual: map[string]any{
580+
"foo": map[string]any{
581+
"type": "long",
582+
},
583+
"bar": map[string]any{
584+
"type": "long",
585+
},
586+
},
587+
schema: []FieldDefinition{
588+
{
589+
Name: "bar",
590+
Type: "float",
591+
External: "ecs",
592+
},
593+
},
594+
expectedErrors: []string{},
595+
},
572596
}
573597

574598
for _, c := range cases {

0 commit comments

Comments
 (0)