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

Unset default of AllowedFilter after it was set once, which was possible with default(null) before nullable filters where implemented in 5.6.0 #902

Merged
merged 4 commits into from
Jan 8, 2024

Conversation

patrickrobrecht
Copy link
Contributor

@patrickrobrecht patrickrobrecht commented Oct 18, 2023

The changes from #895 aren't backwards compatible.

AllowedFilter::scope('test')->default('myDefaultValue')->default(null) does not unset the default value anymore.

Background: We have a static allowedFilters method in a model, such that we can use the same set of AllowedFilters in multiple controllers:

In TestModel:

public static function allowedFilters(): array
{
        return [
                AllowedFilter::scope('test')->default('myDefaultValue'),
                // ...
        ];
}

We can use the following in multiple controllers:

$testModels = QueryBuilder::for(TestModel::class)
        ->allowedFilters(TestModel::allowedFilters())

Removing one of the default values from the array allowedFilters() when using the filter worked with Laravel Query Builder 5.5.0, but not with 5.6.0:

$allowedFilters = [];
foreach (TestModel::allowedFilters() as $allowedFilter) {
        $allowedFilter->default(null);
        $allowedFilters[] = $allowedFilter;
}
$testModels = QueryBuilder::for(TestModel::class)
        ->allowedFilters($allowedFilters);

This PR introduces a new unsetDefault() method which could be used instead of the default(null) call in the example above. This behavior is not fully backwards compatible as one should be able to expect according to Semantic Versioning used by this project.

@patrickrobrecht
Copy link
Contributor Author

patrickrobrecht commented Dec 11, 2023

I've updated #902 to introduce a new method unsetDefault() which provides the same behavior as default(null) did before merging #895. Of course, there are tests for the new method as well.

@AlexVanderbist @Nielsvanpach Could you have a look on that?

This may be an edge case, but we may want to add some hint to the UPGRADING.md file.

@patrickrobrecht patrickrobrecht changed the title Bugfix: Unset default if the AllowedFilter::default method is called multiple times Unset default of AllowedFilter after it was set once, which was possible with default(null) before nullable filters where implemented in 5.6.0 Dec 17, 2023
@AlexVanderbist
Copy link
Member

Hey, sorry about breaking backwards compatibility. This approach looks good to me, so I'll merge it in and add a disclaimer in the UPGRADING.md. Thanks!

@AlexVanderbist AlexVanderbist merged commit d0e25f6 into spatie:main Jan 8, 2024
14 of 15 checks passed
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 this pull request may close these issues.

2 participants