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
Add Form Request and Tests for Update Asset API Method #14458
base: develop
Are you sure you want to change the base?
Conversation
…sion of SubstituteBindings
This pull request has been linked to Shortcut Story #24884: ErrorException: Object of class Illuminate\Database\Eloquent\Collection could not be converted to int.... |
PR Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple things to address but it looks good 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good to me 😄
@@ -115,7 +115,7 @@ public function declinedCheckout(User $declinedBy, $signature) | |||
]; | |||
|
|||
|
|||
/** | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can not figure out how to get rid of this, sorry 🙄
Conflicts are resolved here @snipe |
Ack, need to get marcus' changed test in from the other day |
Cool, tests passing and |
working on fixing latest conflict and getting this all correct again, will ping again when it's actually good to go |
alright, this is good to go again |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me 😄
use Illuminate\Support\Facades\Gate; | ||
use Illuminate\Validation\Rule; | ||
|
||
class UpdateAssetRequest extends ImageUploadRequest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we extending ImageUploadRequest here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we still use some methods from ImageUploadRequest in the controller, this makes it so they're still available via the request - we do the same thing in StoreAssetRequest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! I love the route-model binding thing, and I like the FormRequest changes, and I love the new tests. Unfortunately, due to a bug, we needed to merge the other PR first to fix the couple of customers who were having some problems. But I think if you can fix the conflicts here - and make sure that both your tests and the new tests I wrote all pass - I think we'll be in some pretty good shape. I had some more specific comments on some of the rule duplication which made me a little bit concerned, which you can view in-line.
Another thing you might want to consider (but is not necessary) would be to squash a lot of these changes into a new set of changes - once you've fixed the conflicts, of course. But that's your call; either way is fine.
I think if we can figure out a better way to handle the rule duplication problem and address some of the minor nits I raised, I'd be ready for us to take this.
It's some excellent work, and I'm really excited to see where this stuff is going! Thank you!
public function rules() | ||
{ | ||
$rules = array_merge([ | ||
'model_id' => ['integer', 'exists:models,id,deleted_at,NULL', 'not_array'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get these three rules - where we are removing the 'required' attribute. But a lot of the rest of the rules look exactly the same as the rules on the model. Can we do a kind-of 3-way array_merge()
here, where we grab the Asset's $rules
(or maybe rules()
?), override these couple of ones where we need to pull the required
attribute, then grab the parent::rules()
and merge those in too? I feel like that would cut down on the duplication. Or, another way you could go - grab the Asset::rules
, and grep-out the 'required' attributes, and merge in the parent::rules()?
I just hate repeating rules in multiple places and fear that if we change some constraint somewhere - let's say, we decide to make 'name' have a max of 1023 characters for example - then having to go and update that in 4 or more places. That feels like a foot-gun waiting to happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I've put a couple of different options for resolving this in the rules()
method - $rules
& $rules2
One quick thing, the conversion of the rule strings to arrays in the model is not necessary if we go with option 1 ($rules
) - converting them to arrays just makes it a little easier to manipulate.
Description
This does a couple things:
Adds a new Form Request for Asset Updates to continue our rollout of that feature.
Unfortunately, the only way it makes sense to do this on an update request was to duplicate the validation set. There were a couple reasons for this:
prepareForValidation()
method, but it got a little confusing because of:unique_undeleted
rule does not play nice in a form request because of the trait on the model that makes it work.I still believe that the form request itself is still worth it, it makes the application safer by making sure that our data is validated before we operate on it. And after all, an update request is still it's own request with it's own logic, so it stands to reason rules could be slightly different.
Adds the SubstituteBindings middleware to API routes to allow us to use Route Model Binding.
https://laravel.com/docs/8.x/routing#route-model-binding
Really just a starting step to allowing us to get rid of a lot of lines like
SubstituteBindings is default middleware in Laravel now, so I'm not sure why it wasn't there and I couldn't find any PRs or Issues about it being removed on purpose, I'm assuming it was a symptom of an upgrade and it didn't get added because we already had custom stuff going on in our Kernel - but that's a guess, please let me know if there's a reason it isn't there, and I can look into other options.
The model-not-found exception is already inline with what we had written manually, so the error bubbles up the same if a model isn't found.
This only effects areas in which we're using type-hinting with a method & model (like
public function update(Asset $asset)
), so we shouldn't see any adverse effects from this from what I can tell.Fixes # [sc-24884]
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Tests written
Test Configuration: