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

params.previousData is undefined in update call with hydraDataProvider #520

Open
floriandammeyer opened this issue Dec 18, 2023 · 0 comments

Comments

@floriandammeyer
Copy link

API Platform version(s) affected: 3.4.4

Description
We need to use PATCH instead of PUT to update existing resources, so I drew inspiration from this comment #370 (comment) and overwrote the update method of our data provider, which is an instance of hydraDataProvider.

The new update method should create a diff between the current resource data (given in params.data) and the previous data (given in params.previousData) and then execute a PATCH request.

However, params.previousData is undefined when a user edits a resource and clicks the save button.

I figured it may be caused by some previous error on the page where this behavior occurs, but there are no errors in the JavaScript console on that page. The first error that occurs is that params.previousData is undefined when our diff function is called.

We haven't added much custom logic to our admin implementation, most things are done by the default implementations and automations of API Platform Admin.

What could cause this issue? Is params.previousData supposed to be empty?

How to reproduce

// We use the built-in fetchHydra() and parseHydraDocumentation() functions, 
// but wrapped them to handle authorization as shown in the API Platform Admin docs and examples
const baseDataProvider = hydraDataProvider({
    entrypoint: ENTRYPOINT,
    httpClient: fetchHydraWithAuthentication, 
    apiDocumentationParser: apiDocumentationParser
});

baseDataProvider.update = function(resource, params) {
    return this.httpClient(`${this.entrypoint}/${resource}/${params.id}`, {
        method: 'PATCH',
        body: JSON.stringify(diff(params.data, params.previousData)), 
    }).then(({json}) => ({data: json}));
};

// We use lodash helpers to implement this diff function
const diff = (object, base) => {
    return transform(object, (result, value, key) => {
        if (!isEqual(value, base[key])) {
            result[key] = isObject(value) && isObject(base[key]) ? diff(value, base[key]) : value;
        }
    });
};
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

No branches or pull requests

1 participant