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

Warnings about deprecated fields due to JSON.stringify usage #308

Closed
AndreyTretyak opened this issue Jan 29, 2024 · 6 comments · Fixed by #312
Closed

Warnings about deprecated fields due to JSON.stringify usage #308

AndreyTretyak opened this issue Jan 29, 2024 · 6 comments · Fixed by #312

Comments

@AndreyTretyak
Copy link
Contributor

AndreyTretyak commented Jan 29, 2024

I'm getting errors in browser console about using depricated fields like utc_offset and open_now that I'm not actually using.
I think they are caused by JSON.stringify of PlaceDetails objects in the

It would be very helful to have an option to configure some exclusion list to skip fields during serialization. Usually Google Maps API allows to select what fields you want in order to avoid those problems, but it's not the case for SearchBox for example.
I've also created issue for the SearchBox to allow selecting fields, but looks like the won't act on it https://issuetracker.google.com/issues/322623560

Problem could be reproduced using following code:

var jsObjectRef = await JsObjectRef.CreateAsync(jsRuntime, "google.maps.places.SearchBox", inputField, opts);
await jsObjectRef.InvokeWithReturnedObjectRefAsync(
     "addListener", 
     "places_changed", handler);
     async () => await {
           // next line would cause warnings about depricated fields in console
           var places = await jsObjectRef.InvokeAsync<PlaceResult[]>("getPlaces")
     }));

So far the best workaround I was able to come up with is following hack, but I would really line to have better option to just exlude fields in the GoogleMapsComponents/wwwroot/js/objectManager.js instead of replacing JSON.stringify for everybody. (udpated snipnet after some more testing)

const removeDepricatedFieldsFromObject = function (obj) {
    if ('place_id' in obj) {
        delete obj.utc_offset
    }
    if ('opening_hours' in obj) {
        const hours = obj.opening_hours
        delete hours.open_now
    }
}

const removeDepricatedFields = function (obj) {
    if (Array.isArray(obj)) {
        for (const element of obj) {
            removeDepricatedFields(element)
        }
    } if (typeof obj === 'object' && obj !== null) {
        removeDepricatedFieldsFromObject(obj)
    }
}

const stringifyCore = JSON.stringify;
JSON.stringify = function (obj, replacer, space) {
    removeDepricatedFields(obj);
    return stringifyCore(obj, replacer, space);
};
@valentasm1
Copy link
Collaborator

Very nice catch.
Maybe we could check google maps version and remove fields globaly on method?
I have just basic understanding of js, so not sure about right solution.

@AndreyTretyak
Copy link
Contributor Author

AndreyTretyak commented Jan 29, 2024

I'm also don't know a lot about js. I think excluding fileds by default might be a bit risky since in the future Google Maps Api could add fields with the same names, or some customers might actually need those fields there.

I think a better approach would be to have an option of controling serialization behaivour that would allow avoiding such cases if needed.

For example define wrapper over JSON.stringify with override option and use it in the GoogleMapsComponents/wwwroot/js/objectManager.js:

const extendepbleStringify = function (obj, replacer, space) {
    if (window.blazorGoogleMapsBeforeStringify) {
          obj = window.blazorGoogleMapsBeforeStringify(obj);
    }
    return JSON.stringify(obj, replacer, space);
};

Then customers would be able to define their funtion to exclude fields that they have problem with, like this:

const removeDepricatedFieldsFromObject = function (obj) {
    if ('place_id' in obj) {
        delete obj.utc_offset
    }
    if ('opening_hours' in obj) {
        const hours = obj.opening_hours
        delete hours.open_now
    }
}

const removeDepricatedFields = function (obj) {
    if (Array.isArray(obj)) {
        for (const element of obj) {
            removeDepricatedFields(element)
        }
    } if (typeof obj === 'object' && obj !== null) {
        removeDepricatedFieldsFromObject(obj)
    }
}

window.blazorGoogleMapsBeforeStringify = function (obj) {
   removeDepricatedFields(obj);
   return obj;
};

This would also alow flexibility to adapt to future API changes and should not degrade performance too much for people who are not interested in excluding fields since warnings do not apear in most of the scenarios.

I'm not sure how common this approach are in js or is there are a nicer way, but if that's something you thin acceptable I would be happy to contribute this change. I'm also open for any other suggestions.

@AndreyTretyak
Copy link
Contributor Author

My initial issue for google API was closed as "by design", so I've created another one with alternative suggestion https://issuetracker.google.com/issues/323230578

@valentasm1
Copy link
Collaborator

Sorry but i am on workation, so will take a look till 18 for sure. Especially it is not critical.

@AndreyTretyak
Copy link
Contributor Author

No problem at all. Thank you for letting me know. I hope you'll have a great workation!

@valentasm1
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants