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

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

Open
samkim130 opened this issue Jul 14, 2021 · 2 comments
Open
Assignees
Labels
triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@samkim130
Copy link

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
@samkim130 samkim130 added triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Jul 14, 2021
@samkim130 samkim130 changed the title Directions API: Directions API: Unmarshal error in detecting Route.Legs as type string Jul 14, 2021
@AlphaWong
Copy link
Contributor

AlphaWong commented Feb 2, 2022

Can u copy this test case

func TestDirectionsTransit(t *testing.T) {
and paste the JSON string into it

and paste the result here?

As I think it might be related to ur response.

@sondregj
Copy link

sondregj commented Feb 6, 2025

It seems to happen because of the *url.URL-fields in TransitLine, and if so only applies to transit directions. When these fields are set to null in the JSON and unmarshaling again it works.

An example of what the marshaled URLs might look like for those fields:

{
  "Scheme": "",
  "Opaque": "",
  "User": null,
  "Host": "",
  "Path": "",
  "RawPath": "",
  "OmitHost": false,
  "ForceQuery": false,
  "RawQuery": "",
  "Fragment": "",
  "RawFragment": ""
}

You can reproduce it by getting directions from "Bergen, Norway" to "Vossevangen, Norway" by transit.

I have simply added this before marshaling to mitigate it as I don't care about those fields for my use case:

for _, route := range routes {
	for _, leg := range route.Legs {
		for _, step := range leg.Steps {
			if step.TransitDetails != nil {
				step.TransitDetails.Line.URL = nil
				step.TransitDetails.Line.Icon = nil
				step.TransitDetails.Line.Vehicle.Icon = nil
				// NOTE: There is also an URL in step.TransitDetails.Line.Agencies
			}
		}
	}
}

Perhaps this is an issue with the unmarshaling code in encoding.go not handling empty/malformed URLs well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

4 participants