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

jsonpb: Attempting to unmarshal escaped JSON into a protobuf throws an error #605

Closed
lukeic opened this issue May 15, 2018 · 5 comments · Fixed by #612
Closed

jsonpb: Attempting to unmarshal escaped JSON into a protobuf throws an error #605

lukeic opened this issue May 15, 2018 · 5 comments · Fixed by #612
Assignees

Comments

@lukeic
Copy link

lukeic commented May 15, 2018

In a project of mine, I am fetching JSON from a third-party API and then attempting to unmarshal the response into a protobuf struct, defined with the type google.protobuf.Struct. The API response has escaped values in it and is resulting in the following error: bad value in StructValue for key "image": unrecognized type for Value "\"https:\\/\\/ei.isnooth.com\\/multimedia\\/0\\/2\\/8\\/image_787698_square.jpeg\"".

As a workaround I have had to use the regular encoding/json library to unmarshal and then marshal the response again, before passing the data on to jsonpb.Unmarshal with a protobuf.

Further information and relevant code can be found on my posts here:

@dsnet
Copy link
Member

dsnet commented May 17, 2018

This is a bug in jsonpb here:

} else if v, err := strconv.Unquote(ivStr); err == nil {

strconv.Unquote is the wrong function to use as it unquotes a string according to string literals in the Go language specification, and not according to string literals for JSON strings, which have a different grammar.

@cybrcodr
Copy link
Contributor

@lukeic , mind posting the sample JSON string that produces this bug. I just want to make sure I got a correct test case. Thanks.

@cybrcodr
Copy link
Contributor

Just sent out a PR. I think I found a reproducible testcase which I've also added to the tests.

I'd still like to see a sample JSON to make sure I'm not missing out on other cases.

cybrcodr added a commit that referenced this issue May 18, 2018
…type (#612)

Fix unmarshaling JSON object with escaped string into Struct type.

Fixes #605
@lukeic
Copy link
Author

lukeic commented May 19, 2018

Hey @cybrcodr, awesome to see that you've fixed this. Here's a sample of the JSON I'm using:

    {
    "meta": {
        "results": 1489442,
        "returned": 5,
        "errmsg": "",
        "status": 1
    },
    "wines": [
        {
            "name": "Conway Deep Sea Chardonnay la Costa Wine Co",
            "code": "conway-deep-sea-chardonnay-la-costa-wine-co-2008-1",
            "region": "USA > California > Central Coast",
            "winery": "Conway Family Wines",
            "winery_id": "conway-family-wines",
            "varietal": "Chardonnay",
            "price": "21.99",
            "vintage": "2008",
            "type": "White Wine",
            "link": "http:\/\/www.snooth.com\/wine\/conway-deep-sea-chardonnay-la-costa-wine-co-2008-1\/",
            "tags": "",
            "image": "https:\/\/ei.isnooth.com\/multimedia\/0\/2\/8\/image_787698_square.jpeg",
            "snoothrank": 3,
            "available": 0,
            "num_merchants": 0,
            "num_reviews": 10
        },
        {
            "name": "Olmaia Cabernet di Toscana",
            "code": "olmaia-cabernet-di-toscana",
            "region": "Italy > Tuscany > Toscana Igt",
            "winery": "Col D Orcia",
            "winery_id": "col-d-orcia",
            "varietal": "Cabernet Sauvignon",
            "price": "0.00",
            "vintage": "",
            "type": "Red Wine",
            "link": "http:\/\/www.snooth.com\/wine\/olmaia-cabernet-di-toscana\/",
            "tags": "",
            "image": "https:\/\/ei.isnooth.com\/multimedia\/d\/e\/e\/image_790198_square.jpeg",
            "snoothrank": 3.5,
            "available": 0,
            "num_merchants": 0,
            "num_reviews": 25
        },
        {
            "name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
            "code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006",
            "region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
            "winery": "Bischöfliches Priesterseminar Trier",
            "winery_id": "bischofliches-priesterseminar-trier",
            "varietal": "Prieto Picudo",
            "price": "15.89",
            "vintage": "2006",
            "type": "Red Wine",
            "link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006\/",
            "tags": "",
            "image": "https:\/\/ei.isnooth.com\/multimedia\/d\/0\/4\/image_336595_square.jpeg",
            "snoothrank": "n\/a",
            "available": 0,
            "num_merchants": 0,
            "num_reviews": 1
        },
        {
            "name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
            "code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005",
            "region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
            "winery": "Bischöfliches Priesterseminar Trier",
            "winery_id": "bischofliches-priesterseminar-trier",
            "varietal": "Prieto Picudo",
            "price": "38.99",
            "vintage": "2005",
            "type": "Red Wine",
            "link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005\/",
            "tags": "",
            "image": "https:\/\/ei.isnooth.com\/multimedia\/1\/d\/a\/image_336596_square.jpeg",
            "snoothrank": "n\/a",
            "available": 0,
            "num_merchants": 0,
            "num_reviews": 1
        },
        {
            "name": "The Little Penguin Chardonnay Premier",
            "code": "the-little-penguin-chardonnay-premier-2010",
            "region": "South East Australia",
            "winery": "The Little Penguin",
            "winery_id": "the-little-penguin",
            "varietal": "Chardonnay",
            "price": "11.99",
            "vintage": "2010",
            "type": "White Wine",
            "link": "http:\/\/www.snooth.com\/wine\/the-little-penguin-chardonnay-premier-2010\/",
            "tags": "",
            "image": "https:\/\/ei.isnooth.com\/multimedia\/2\/c\/4\/image_826282_square.jpeg",
            "snoothrank": "n\/a",
            "available": 0,
            "num_merchants": 0,
            "num_reviews": 7
        }
    ]
}

@cybrcodr
Copy link
Contributor

Thanks. That confirms what I've noticed. Somehow the bug was only exposed for StringValue inside a google.protobuf.Struct. A StringValue field by itself didn't had the same issue.

@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants