Skip to content

Commit

Permalink
fix: ApplicationSource.Equal return false negative
Browse files Browse the repository at this point in the history
when comparing string with escaped and unescaped representation.

Signed-off-by: Nattawit Chaiworawitsakul <c.nattawit@gmail.com>
  • Loading branch information
nattawitc committed Jul 7, 2024
1 parent b909993 commit a8feae1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1alpha1

import (
"bytes"
"encoding/json"
"fmt"
"math"
Expand Down Expand Up @@ -2821,9 +2822,23 @@ func (source *ApplicationSource) Equals(other *ApplicationSource) bool {
otherCopy := other.DeepCopy()
sourceCopy.Plugin = nil
otherCopy.Plugin = nil
sourceCopy.htmlEscape()
otherCopy.htmlEscape()
return reflect.DeepEqual(sourceCopy, otherCopy)
}

func (source *ApplicationSource) htmlEscape() {
if source.Helm != nil {
if source.Helm.ValuesObject != nil {
if source.Helm.ValuesObject.Raw != nil {
var buf bytes.Buffer
json.HTMLEscape(&buf, source.Helm.ValuesObject.Raw)
source.Helm.ValuesObject.Raw = buf.Bytes()
}
}
}
}

// ExplicitType returns the type (e.g. Helm, Kustomize, etc) of the application. If either none or multiple types are defined, returns an error.
func (source *ApplicationSource) ExplicitType() (*ApplicationSourceType, error) {
var appTypes []ApplicationSourceType
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/application/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,27 @@ func TestApplicationSource_IsZero(t *testing.T) {
}
}

func TestApplicationSource_Equals(t *testing.T) {
t.Run("Escaped Characters", func(t *testing.T) {
src1 := ApplicationSource{
Helm: &ApplicationSourceHelm{
ValuesObject: &runtime.RawExtension{
Raw: []byte(`{"Test":"test&<>

"}`),
},
},
}
src2 := ApplicationSource{
Helm: &ApplicationSourceHelm{
ValuesObject: &runtime.RawExtension{
Raw: []byte(`{"Test":"test\u0026\u003c\u003e\u2028\u2029"}`),
},
},
}

assert.True(t, src1.Equals(&src2))
})
}

func TestApplicationSourceHelm_AddParameter(t *testing.T) {
src := ApplicationSourceHelm{}
t.Run("Add", func(t *testing.T) {
Expand Down

0 comments on commit a8feae1

Please sign in to comment.