fix(authx): JSON unmarshalling for Dynamic auth type#6268
Conversation
Correcting the `UnmarshalJSON` method to properly unmarshal JSON, particularlyaddressing the population of the embedded `Secret` field. This was achieved by using a type alias to avoid recursive calls and rely on default unmarshalling behavior. Signed-off-by: Dwi Siswanto <git@dw1.io>
|
""" WalkthroughThe Changes
Sequence Diagram(s)sequenceDiagram
participant Test as Test Suite
participant Dynamic as Dynamic.UnmarshalJSON
participant JSON as json.Unmarshal
Test->>Dynamic: Call UnmarshalJSON(data)
Dynamic->>Dynamic: Create type alias (Alias)
Dynamic->>JSON: Unmarshal data into alias pointer
JSON-->>Dynamic: Populate struct fields
Dynamic-->>Test: Return error or nil
Assessment against linked issues
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (3)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
$ go test -v -timeout 30s -run ^TestDynamicUnmarshalJSON$ ./pkg/authprovider/authx
=== RUN TestDynamicUnmarshalJSON
=== RUN TestDynamicUnmarshalJSON/basic-unmarshal
=== RUN TestDynamicUnmarshalJSON/complex-unmarshal
=== RUN TestDynamicUnmarshalJSON/invalid-json
=== RUN TestDynamicUnmarshalJSON/empty-json
--- PASS: TestDynamicUnmarshalJSON (0.00s)
--- PASS: TestDynamicUnmarshalJSON/basic-unmarshal (0.00s)
--- PASS: TestDynamicUnmarshalJSON/complex-unmarshal (0.00s)
--- PASS: TestDynamicUnmarshalJSON/invalid-json (0.00s)
--- PASS: TestDynamicUnmarshalJSON/empty-json (0.00s)
PASS
ok github.com/projectdiscovery/nuclei/v3/pkg/authprovider/authx 0.041s |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (3)
pkg/authprovider/authx/dynamic_test.go (3)
1-8: Run the test (and each sub-test) in parallelEnabling
t.Parallel()improves CI throughput and guards against hidden concurrency issues.func TestDynamicUnmarshalJSON(t *testing.T) { + t.Parallel() t.Run("basic-unmarshal", func(t *testing.T) { + t.Parallel()Repeat
t.Parallel()inside each sub-test.
9-54: Exercise the std-lib unmarshal path as wellThe current test invokes
d.UnmarshalJSONdirectly, bypassing the common
json.Unmarshalpath (json.Unmarshaldetects and calls the method).
Add a second assertion that the alias logic still works when the struct is the
target ofjson.Unmarshal.var d Dynamic - err := d.UnmarshalJSON(data) + var d2 Dynamic + err := d.UnmarshalJSON(data) // direct + err2 := json.Unmarshal(data, &d2) // via encoding/json require.NoError(t, err) + require.NoError(t, err2) + require.Equal(t, d, d2)
112-124: Minor readability tweak – raw string literalUsing a raw string (
\``…`) avoids the{}` escaping noise:- data := []byte(`{invalid json}`) + data := []byte(`{invalid json}`)Consider switching to:
data := []byte(`{invalid json}`)(applies to the other JSON blobs too). Purely cosmetic.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
pkg/authprovider/authx/dynamic.go(1 hunks)pkg/authprovider/authx/dynamic_test.go(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Lint
Signed-off-by: Dwi Siswanto <git@dw1.io>
|
test fails |
All green now. |
Proposed changes
Correcting the
UnmarshalJSONmethod to properlyunmarshal JSON, particularlyaddressing the
population of the embedded
Secretfield. Thiswas achieved by using a type alias to avoid
recursive calls and rely on default unmarshalling
behavior.
Fixes #6267
Checklist
Summary by CodeRabbit
Bug Fixes
Tests