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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spanner): MarshalJSON function caused errors for certain values #9063

Merged
merged 2 commits into from Nov 30, 2023

Conversation

danysousa
Copy link
Contributor

This pull request addresses an issue in the implementation of the Marshaler interface within the spanner package.
The current implementation leads to errors when dealing with certain special characters, such as certain emojis. To resolve this issue, I have adopted an approach similar to that used in the bigquery package, which is not affected by this problem.

The root cause of the problem lies in the use of Sprintf, which may seem appealing but lacks the advantages of checks and escapes provided by the encoding/json package. This pull request aims to rectify the situation by using the json.Marshal.

To validate the proposed solution, a small sample code is provided below that reproduces the error with the current implementation and demonstrates that the proposed changes resolve the issue:

package main

import (
    "encoding/json"
    "cloud.google.com/go/spanner"
)

func main() {
    value := spanner.NullString{
        Valid:     true,
        StringVal: `馃┓`,
    }

    marshalledValue, err := json.Marshal(value)
    if err != nil {
        println("Error:", err.Error())
    } else {
        println("Value:", string(marshalledValue))
    }
}

Output before the fix :

Error: json: error calling MarshalJSON for type spanner.NullString: invalid character 'U' in string escape code

Expected output :

Value: 馃┓

@danysousa danysousa requested review from a team as code owners November 30, 2023 09:21
Copy link

google-cla bot commented Nov 30, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: spanner Issues related to the Spanner API. labels Nov 30, 2023
@rahul2393 rahul2393 added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Nov 30, 2023
@kokoro-team kokoro-team removed kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Nov 30, 2023
@rahul2393 rahul2393 enabled auto-merge (squash) November 30, 2023 10:27
@rahul2393 rahul2393 merged commit afe7c98 into googleapis:main Nov 30, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants