Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null pointer when validating null values in JSON #8

Open
codemental opened this issue Apr 12, 2024 · 0 comments
Open

Null pointer when validating null values in JSON #8

codemental opened this issue Apr 12, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@codemental
Copy link
Member

A null pointer is caused when a JSON payload (expected or actual) contains 'null' as value. This is most probably caused by these lines:

expectedValueType := reflect.TypeOf(expectedValue)
actualValueType := reflect.TypeOf(actualValue)

The documentation states that if the argument of .TypeOf(...) is nil, the result is nil.

Here are example tests that fail:

func TestOKValidationWithNullValue(t *testing.T) {
	expectedValue := []byte("{" +
		"\"active\": null" +
		"}")

	actualValue := []byte("{" +
		"\"active\": null" +
		"}")

	// we ignore the recorder log because the order of the elements is always different
	testComparator(t, expectedValue, actualValue, []string{}, "")
}

func TestErrorValidationWithNullActualValue(t *testing.T) {
	expectedValue := []byte("{" +
		"\"active\": true" +
		"}")

	actualValue := []byte("{" +
		"\"active\": null" +
		"}")

	// we ignore the recorder log because the order of the elements is always different
	testComparator(t, expectedValue, actualValue, []string{}, "")
}

func TestErrorValidationWithNullExpectedValue(t *testing.T) {
	expectedValue := []byte("{" +
		"\"name\": null," +
		"}")

	actualValue := []byte("{" +
		" \"name\": \"Bruce Wayne\"," +
		"}")

	// we ignore the recorder log because the order of the elements is always different
	testComparator(t, expectedValue, actualValue, []string{}, "")
}

func TestNullExpectedValue(t *testing.T) {
	expectedErrors := []string{
		...
	}

	expectedValue := []byte("{" +
		"\"aliases\": [" +
		"null" +
		"]" +
		"}")
	actualValue := []byte("{" +
		"\"aliases\": [" +
		"\"Batman\"" +
		"]" +
		"}")

	expectedRecorderLog := "{\n" +
		"}\n"

	testComparator(t, expectedValue, actualValue, expectedErrors, expectedRecorderLog)
}

func TestNullActualValue(t *testing.T) {
	expectedErrors := []string{
		...
	}

	expectedValue := []byte("{" +
		"\"aliases\": [" +
		"\"Batman\"" +
		"]" +
		"}")
	actualValue := []byte("{" +
		"\"aliases\": [" +
		"null" +
		"]" +
		"}")

	expectedRecorderLog := "{\n" +
		"}\n"

	testComparator(t, expectedValue, actualValue, expectedErrors, expectedRecorderLog)
}
@codemental codemental added the bug Something isn't working label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant