Skip to content

Commit

Permalink
Fix BytesWithPreservedJson and add more tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Nov 13, 2023
1 parent e6732e1 commit 08e098f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
20 changes: 14 additions & 6 deletions core/services/job/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,21 @@ func (r JSONConfig) Bytes() []byte {
return b
}

// BytesWithPreservedJson returns raw bytes and properly marshals any potential json structure strings.
func (r JSONConfig) BytesWithPreservedJson() []byte {
var retCopy = make(JSONConfig, 0)
for key, value := range r {
copiedVal := value
// If the value is a string, unmarshal it first to preserve potential JSON structure
// If the value is a json structure string, unmarshal it to preserve JSON structure
// e.g. instead of this {"key":"{\"nestedKey\":{\"nestedValue\":123}}"}
// we want this {"key":{"nestedKey":{"nestedValue":123}}},
if strValue, ok := copiedVal.(string); ok {
var parsedValue interface{}
err := json.Unmarshal([]byte(strValue), &parsedValue)
if err == nil {
copiedVal = parsedValue
if isValidJSONStruct(strValue) {
var parsedValue interface{}
if err := json.Unmarshal([]byte(strValue), &parsedValue); err == nil {
copiedVal = parsedValue
}
}

}
retCopy[key] = copiedVal
}
Expand All @@ -299,6 +302,11 @@ func (r JSONConfig) BytesWithPreservedJson() []byte {
return b
}

func isValidJSONStruct(s string) bool {
var js map[string]interface{}
return json.Unmarshal([]byte(s), &js) == nil
}

// Value returns this instance serialized for database storage.
func (r JSONConfig) Value() (driver.Value, error) {
return json.Marshal(r)
Expand Down
8 changes: 8 additions & 0 deletions core/services/job/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestJSONConfig_BytesWithPreservedJson(t *testing.T) {
Input: JSONConfig{
"key": "{\"nestedKey\": {\"nestedValue\":123}}",
},
// regular Bytes marshals to this: {"key":"{\"nestedKey\": {\"nestedValue\":123}}"}
Expected: []byte(`{"key":{"nestedKey":{"nestedValue":123}}}`),
},
{
Expand All @@ -107,6 +108,13 @@ func TestJSONConfig_BytesWithPreservedJson(t *testing.T) {
},
Expected: []byte(`{"key":"abc"}`),
},
{
name: "string number stays string number",
Input: JSONConfig{
"key1": "1",
},
Expected: []byte(`{"key1":"1"}`),
},
{
name: "all together",
Input: JSONConfig{
Expand Down
2 changes: 1 addition & 1 deletion core/services/ocr2/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ func (d *Delegate) newServicesGenericPlugin(
JobID: spec.ID,
ContractID: spec.ContractID,
New: d.isNewlyCreatedJob,
RelayConfig: spec.RelayConfig.Bytes(),
RelayConfig: spec.RelayConfig.BytesWithPreservedJson(),
ProviderType: cconf.ProviderType,
}, types.PluginArgs{
TransmitterID: spec.TransmitterID.String,
Expand Down

0 comments on commit 08e098f

Please sign in to comment.