Skip to content

Commit e07629d

Browse files
committed
fix(sdjwtvc): skip missing JSON paths in ExtractClaimsByJSONPath
ExtractClaimsByJSONPath previously returned a hard error when any VCTM JSON path did not match the document data. This caused OIDC RP callback failures when the credential template (VCTM) defines more claims than the user's credential_mappings actually provide. Changed to silently skip unmatched paths — callers already handle partial results gracefully with 'if !ok { continue }' guards. Updated tests to reflect the new skip-missing-paths behavior.
1 parent 64228d4 commit e07629d

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

pkg/sdjwtvc/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func Base64Decode(s string) (string, error) {
167167
// ExtractClaimsByJSONPath extracts specific claim values from document data using JSONPath queries.
168168
// Takes a map of label->JSONPath expressions and returns a map of label->extracted values.
169169
// Example: {"given-name": "$.name.given"} extracts the value at path $.name.given and maps it to "given-name".
170-
// Returns an error if any path fails to extract.
170+
// Paths that do not match any value in the document data are silently skipped.
171171
func ExtractClaimsByJSONPath(documentData map[string]any, jsonPathMap map[string]string) (map[string]any, error) {
172172
v := any(nil)
173173

@@ -185,7 +185,8 @@ func ExtractClaimsByJSONPath(documentData map[string]any, jsonPathMap map[string
185185
for key, path := range jsonPathMap {
186186
result, err := jsonpath.Get(path, v)
187187
if err != nil {
188-
return nil, fmt.Errorf("failed to get path %s: %w", path, err)
188+
// Path not found in document data — skip it.
189+
continue
189190
}
190191

191192
reply[key] = result

pkg/sdjwtvc/utils_jsonpath_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ func TestExtractClaimsByJSONPath(t *testing.T) {
5959
}
6060

6161
result, err := ExtractClaimsByJSONPath(documentData, jsonPathMap)
62-
// Should return error when path doesn't exist
63-
assert.Error(t, err)
64-
assert.Nil(t, result)
65-
assert.Contains(t, err.Error(), "failed to get path")
62+
// Missing paths are silently skipped — only matched paths are returned
63+
require.NoError(t, err)
64+
assert.Equal(t, "John", result["existing"])
65+
assert.NotContains(t, result, "nonexistent")
6666
})
6767

6868
t.Run("empty_document", func(t *testing.T) {
@@ -73,9 +73,9 @@ func TestExtractClaimsByJSONPath(t *testing.T) {
7373
}
7474

7575
result, err := ExtractClaimsByJSONPath(documentData, jsonPathMap)
76-
// Should return error when path doesn't exist in empty document
77-
assert.Error(t, err)
78-
assert.Nil(t, result)
76+
// No paths match in empty document — returns empty result, no error
77+
require.NoError(t, err)
78+
assert.Empty(t, result)
7979
})
8080

8181
t.Run("empty_json_path_map", func(t *testing.T) {

0 commit comments

Comments
 (0)