Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@wanwusangzhi
Copy link
Contributor

type inner struct {
	Duration time.Duration `json:"duration"`
}

func TestUnmarshalerPanic(t *testing.T) {
	content := `
{
	"duration": 1
}
`
	var in inner
	err := conf.LoadFromJsonBytes([]byte(content), &in) // panic here
	fmt.Println(in, err)
}

Panic before repair:

// === RUN TestUnmarshalerPanic
//--- FAIL: TestUnmarshalerPanic (0.00s)
//panic: interface conversion: interface {} is json.Number, not string [recovered]
// panic: interface conversion: interface {} is json.Number, not string

error after repair:

=== RUN TestUnmarshalerPanic
{0s} unexpected type json.Number, expected a string value for field duration
--- PASS: TestUnmarshalerPanic (0.00s)

@kevwan kevwan force-pushed the fix/unmarshler_panic branch from a83d0f5 to c2efb83 Compare June 12, 2025 14:41
@kevwan kevwan requested a review from Copilot June 12, 2025 15:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a panic issue caused by an incorrect type assertion when unmarshalling a time.Duration value and strengthens input error handling.

  • Introduces a type check for non-string values in the duration unmarshalling logic.
  • Adds a new test case (TestUnmarshalDurationUnexpectedError) to verify that an appropriate error is returned for unexpected types.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
core/mapping/unmarshaler.go Adds a type assertion check and improved error handling for duration values.
core/mapping/unmarshaler_test.go Adds a test case to verify that non-string inputs produce the expected error.
Comments suppressed due to low confidence (1)

core/mapping/unmarshaler.go:625

  • The new type assertion prevents a panic by ensuring that mapValue is a string, and the formatted error message clearly indicates a type mismatch. Please verify that this behavior aligns with all expected input types for duration values.
v, ok := mapValue.(string)

@codecov
Copy link

codecov bot commented Jun 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.89%. Comparing base (8690859) to head (c2efb83).
Report is 364 commits behind head on master.

Additional details and impacted files
Files with missing lines Coverage Δ
core/mapping/unmarshaler.go 96.19% <100.00%> (-0.29%) ⬇️

... and 19 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kevwan kevwan added this pull request to the merge queue Jun 12, 2025
Merged via the queue into zeromicro:master with commit 4cacc4d Jun 12, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants