googlemaps / google-maps-services-go

Go client library for Google Maps API Web Services
https://pkg.go.dev/googlemaps.github.io/maps
Apache License 2.0
737 stars 214 forks source link

Directions API: Unmarshal error in detecting Route.Legs as type string #256

Open samkim130 opened 3 years ago

samkim130 commented 3 years ago

I kept getting the error where I couldn't unmarshal a saved sample data because for some reason it would think Route.Legs is a type of string. I modified the code to test it directly right after getting a response, but still the same issue.

Environment details

  1. Specify the API at the beginning of the title (for example, "Places: ...")
  2. OS type and version
  3. Library version and other environment information
    • googlemaps.github.io/maps v1.3.2 (the latest)
    • go version go1.16.5 darwin/amd64

Steps to reproduce

  1. Get []map.Route from valid client request through the Directions API
  2. Marshal the object
  3. Unmarshal the same object

Code example

        routes := getSampleRoutes()
        jsonObj, err := json.MarshalIndent(routes, "", "   ")
    if err != nil {
              log.Fatal(err)
        }
        // At this point, I was able to check that the proper data came through and that the object was marshalled properly
    var newRoutes []maps.Route
    err = json.Unmarshal(jsonObj, &newRoutes)
    if err != nil {
              log.Fatal(err) // hits error here
        }

Stack trace

fatal error: json: cannot unmarshal object into Go struct field Route.legs of type string
AlphaWong commented 2 years ago

Can u copy this test case https://github.com/googlemaps/google-maps-services-go/blob/621d70731566a6c7c11fc09a2bd4bbad4989fc88/directions_test.go#L69 and paste the JSON string into it

and paste the result here?

As I think it might be related to ur response.