Skip to content

Commit b244784

Browse files
authored
fix(service): handle null JSON metadata in pipeline conversion (#1134)
Because - The pipeline conversion service was encountering protobuf syntax errors when trying to unmarshal JSON metadata fields containing the literal value `"null"` - `structpb.Struct` expects a JSON object `{}` but cannot handle JSON `null` values, causing parsing failures with error: `"proto: syntax error (line 1:1): unexpected token null"` - Legacy database records or direct database manipulation could result in NULL metadata values being stored as the JSON literal `"null"` This commit - Adds null value checks before attempting to unmarshal metadata in both `ConvertPipelineToPB` and `ConvertPipelineReleaseToPB` functions - Prevents protobuf parsing errors by skipping unmarshaling when metadata contains the JSON literal `"null"` - Ensures metadata is only set on the protobuf object when valid JSON objects are successfully unmarshaled - Maintains backward compatibility while gracefully handling edge cases with null metadata values The fix ensures robust handling of metadata fields across different scenarios: - NULL database values (handled by skipping null literals) - Empty metadata (handled by existing nil checks) - Valid JSON objects (unmarshaled successfully as before) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Avoid unmarshalling JSON "null" metadata and only set `Metadata` on protobuf when valid, for both pipelines and releases. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit f59431b. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 174f7d6 commit b244784

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

pkg/service/convert.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,17 @@ func (c *converter) ConvertPipelineToPB(ctx context.Context, dbPipelineOrigin *d
641641

642642
if view > pipelinepb.Pipeline_VIEW_BASIC {
643643
if dbPipeline.Metadata != nil {
644-
str := structpb.Struct{}
645-
err := str.UnmarshalJSON(dbPipeline.Metadata)
646-
if err != nil {
647-
logger.Error(err.Error())
644+
645+
// Check if metadata is not null JSON value
646+
if string(dbPipeline.Metadata) != "null" {
647+
str := structpb.Struct{}
648+
err := str.UnmarshalJSON(dbPipeline.Metadata)
649+
if err != nil {
650+
logger.Error(err.Error())
651+
} else {
652+
pbPipeline.Metadata = &str
653+
}
648654
}
649-
pbPipeline.Metadata = &str
650655
}
651656
}
652657

@@ -813,12 +818,16 @@ func (c *converter) ConvertPipelineReleaseToPB(ctx context.Context, dbPipeline *
813818

814819
if view > pipelinepb.Pipeline_VIEW_BASIC {
815820
if dbPipelineRelease.Metadata != nil {
816-
str := structpb.Struct{}
817-
err := str.UnmarshalJSON(dbPipelineRelease.Metadata)
818-
if err != nil {
819-
logger.Error(err.Error())
821+
// Check if metadata is not null JSON value
822+
if string(dbPipelineRelease.Metadata) != "null" {
823+
str := structpb.Struct{}
824+
err := str.UnmarshalJSON(dbPipelineRelease.Metadata)
825+
if err != nil {
826+
logger.Error(err.Error())
827+
} else {
828+
pbPipelineRelease.Metadata = &str
829+
}
820830
}
821-
pbPipelineRelease.Metadata = &str
822831
}
823832
}
824833

pkg/service/pipeline.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,7 @@ func (s *service) CreateNamespacePipelineRelease(ctx context.Context, ns resourc
14521452
if dbPipelineReleaseToCreate.RecipeYAML == "" {
14531453
dbPipelineReleaseToCreate.RecipeYAML = dbPipeline.RecipeYAML
14541454
}
1455+
14551456
if dbPipelineReleaseToCreate.Metadata == nil {
14561457
dbPipelineReleaseToCreate.Metadata = dbPipeline.Metadata
14571458
}

0 commit comments

Comments
 (0)