Closed (duplicate)
Project:
Drupal core
Version:
11.x-dev
Component:
file system
Priority:
Critical
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
1 Jul 2021 at 23:03 UTC
Updated:
7 Jun 2023 at 04:28 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
kim.pepperMoving this to the meta
Comment #3
berdirThe title seems very confusing, shouldn't that say that we should move it to the file *module*. The title sounds like moving it to \Drupal\Core\FileSystem or so, which isn't possible as the file entity is in file module.
rest.module is just as much "core" as file.module, for now anyway ;)
Comment #4
kim.pepperYes, you're right. I've updated the title.
Comment #5
kim.pepperComment #6
kim.pepperInitial patch. I'm a bit confused why the file module has a REST plugin in it, but there you go.
Comment #8
kim.pepperRemoves duplicate code from \Drupal\jsonapi\Controller\TemporaryJsonapiFileFieldUploader
Comment #10
kim.pepperFix error message.
Comment #12
naveen433 commentedi am working on it
Comment #13
BS Pavan commentedfix test cases of #10
Comment #14
BS Pavan commentedsorry @naveen433, i didn't see your comment
Comment #15
naveen433 commentedplease find below attach patch file and interdiff file
Comment #17
kim.pepperThanks @BS Pavan, your changes look good. Marking as needs review for #13.
@naveen433 not sure what went on with your patch. Looks like you deleted the new class.
Comment #18
kim.pepperMarking this critical, as it is blocking #2940383: [META] Unify file upload logic of REST and JSON:API which is critical.
Comment #19
kim.pepperChanging the title to something more appropriate.
Comment #20
BS Pavan commentedAs mentioned in the comment (https://www.drupal.org/project/drupal/issues/3221794#comment-14157504), #13 patch is approved, so to have pass patch as the latest patch in the ticket I am re-uploading the patch.
Thanks
Pavan
Comment #21
kim.pepperThanks @BS Pavan
No need to re-post the patch. :-)
Also
> #13 patch is approved
Have a read of what the different issue statuses mean https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-... Needs review means it still needs review by other reviewers.
Comment #22
berdirWondering if there are any BC aspects to this, subclasses that use this method or so, same for the custom methods that are currently removed. to be extra careful, we might need to keep them around and slap a trigger_error() on them.
Not sure about the translation stuff. Looking at \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator, it doesn't do that, and the messages are already translatable strings. \Drupal\Core\Validation\DrupalTranslator::trans() returns TranslatableMarkup object unchanged.
maybe do this only once outside of the loop?
Wondering what exactly "our" is in this scope. This is copied from the existing method, which in turn is copied 1:1 from _file_save_upload_single().
this doesn't hurt as we're specifically only checking the upload, but I'm still confused about it. In the widget/form elements, it is both validation and documentation/info for the user, so considering it makes sense, but that doesn't really seem to be the case here.
the comment needs to updated.
most of these messages actually don't have a property path, so it looks pretty broken with a leading ":". Is it really worth adding that to the message? Make it dynamic at least, so we only add it if not empty?
Comment #23
kim.pepperThanks for the review @Berdir
I'm also aware that I split this issue off #2940383: [META] Unify file upload logic of REST and JSON:API to make it smaller and easier for committers. However, if we are adding BC layers that we'll need to remove again then it doesn't seem worth it. Thoughts?
Looks like code in \Drupal\file\Plugin\Field\FieldType\FileItem::getUploadValidators() is also duplicate :-\
Comment #24
daffie commentedCan we add
: arrayas a type hint for the return value.Should we not change the 2 parameters, so that we in D10 can easily remove the one that is no longer needed. The same with modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php.
Comment #25
berdir> 2. I assume it was trying to follow the pattern in https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/lib/Drupal/C... ?
Yes, but that's a different thing. That's executing actual validation constraints and it will receive strings. Here we can expect that we receive already translated strings. It's a almost a no-op, but it's also extra code to follow/understand. I'd vote to simplify it when refactoring it into one place.
> 5. Not sure what the alternative is? Remove it? We'd need to add it back in all of the calling code
I don't know. Just thinking out aloud, honestly wondering about the reason/background for adding this explicitly. My understanding is that it can never get that far if file would really be above that limit as PHP would already block the upload or am I missing something?
> Looks like code in \Drupal\file\Plugin\Field\FieldType\FileItem::getUploadValidators() is also duplicate :-\
That is where it was copied from initially. The problem with that was is that's a chicken/egg situation, I've had that before as well. You need an entity with a file item object to get the upload validators out of that. Forms have that, other places like this upload logic do not, as you upload the file first and then pass the ID along when creating the entity on a separate API call. So we had to duplicate it. Now that we do put it on a dedicated service, we might be able to re-use that service in FileItem.
Same story with getUploadLocation(), that was even extracted into a static method to use it from generateSampleValue(), but it was made protected to prevent you from reusing that code ;) Both FileUploadResource and the Json API version duplicate that method too. But I suppose it's not a good match for this new service.
Comment #26
berdirOn 2: Well, arguably FileValidationConstraintValidator is a constraint too and will actually run it through \Drupal\Core\TypedData\Validation\ExecutionContext::addViolation() so that will pass through trans() too. Still feel like it would make sense to remove the call here, as we have the choice to do that, while FileValidationConstraintValidator does not. And we must assume that file_validate() returns TranslatableMarkup() objects, because those validation messages have placeholders and re-translating them would be wrong. It's only not strictly wrong because of the lazy-translation behavior of t() in D8.
Comment #27
kim.pepperRe: #22
1 I couldn't find any contrib code using resourceValidate() http://grep.xnddx.ru/search?text=resourceValidate&filename=
2. Removed DrupalTranslator->trans() calls.
7. Removed property from error message.
Thanks @daffie. Re: #24
Comment #28
kim.pepperPerhaps that can be tackled in #2940383: [META] Unify file upload logic of REST and JSON:API
Comment #30
kim.pepperAdds missing service definition arguments.
Comment #32
kim.pepperI think we are seeing some undeclared dependencies in tests??
Comment #33
kim.pepperAdding a dependency on file module.
Comment #35
daffie commentedThe testbot is returning with the error: The service "jsonapi.file.uploader.field" has a dependency on a non-existent service "file.upload_validator".
Comment #36
kim.pepperYeah, I think that means there is a missing dependency on the file module in those tests.
Comment #37
berdirHm, yeah, the actual dependency was more hidden before, an argument could be made that there are use cases where you want to use jsonapi without files but it already didn't properly handle that before and I guess just relied on not using/calling that service/plugin before. The plugin definition didn't have a module dependency either.
Kernel test fails are very expected, you only enable the specific modules you need to get the test to pass and that now includes file due to the service definition.
Both the plugin and the service could be dynamically added if the file module is enabled, similar to \Drupal\entity_reference_revisions\EntityReferenceRevisionsServiceProvider or so. Not sure if it's worth it :)
Comment #38
kim.pepperAdds file module dependency for Kernel tests.
Comment #39
daffie commentedThe only place where $this->eventDispatcher was used will be removed in this patch. Should we therefor add a deprecation message to the class variable in __construct()?
The same as with the class TemporaryJsonapiFileFieldUploader.
We are removing the use of the trait Drupal\rest\Plugin\rest\resource\EntityResourceValidationTrait. That trait is also used in: Drupal\rest\Plugin\rest\resource\EntityResource and Drupal\user\Plugin\rest\resource\UserRegistrationResource. Should we removed that trait also from those classes? I am not sure, just asking.
Comment #40
kim.pepperI'm also wondering if the validator will get subsumed when we add FileFieldUploader 🤔
The reason I split this off was to try and simplify and reduce the size of the original issue. Seems we are stuck with handling the dependencies.
I might try a combined patch with that issue.
Comment #41
daffie commentedIf #39.3 will get fixed in a followup/postponed issue, then it does not to be resolved in this issue.
Comment #43
stephencamilo commentedComment #44
andypostComment #45
kim.pepperComment #46
ankithashettyHere is the rerolled patch. Retaining status 'Needs work' for changes suggested in #39, thanks!
Comment #50
kim.pepperI believe this is a duplicate of #3221793: Move file upload validation from file.module to constraint validators which covers this and is nearly ready.