-
Notifications
You must be signed in to change notification settings - Fork 495
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
allow changes to file metadata via API #6962 #6968
Conversation
I'm putting this back in code review after fixing a bug @scolapasta found. Now an API user can pass in the existing label or directoryLabel and we still allow edits to go through. |
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 of small comments, general idea is good.
String oldLabel = df.getFileMetadata().getLabel(); | ||
String oldDirectoryLabel = df.getFileMetadata().getDirectoryLabel(); | ||
String oldPathPlusName = oldDirectoryLabel + oldLabel; | ||
String incomingPathPlusName = directoryLabel + label; |
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.
does this do weird things if either one is null? i.e I would expect if you only passed on in, and it was the same, then there should be no labelChange, but I think the code for the equals would be false.
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 figured what matters in terms of if there was a change or not is if the "path + filename" is different, so that's what I'm comparing. No, weird things do not happen if one is null, according to my testing.
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.
Sorry, maybe what I wrote wasn't clear, so let me use an example:
oldDirectory = "directory" and old label = "label"
If I only path in a label as "label" (i.e. no directory), I would expect it to allow it, as I am not changing anything. But the label check would compare "directorylabel" with "nulllabel" and so would run the check when it shouldn't.
Also if (for some reason) for new values you passed directory = "director" and label = "ylabel", the check would find them equal and so not check names, even though you are changing the names.
So the check also needs to have a "/" in there.
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.
Good catch. I fixed both of these.
String path = ""; | ||
if (directoryLabel != null) { | ||
path = directoryLabel + "/"; | ||
} | ||
if (label == null) { | ||
label = df.getFileMetadata().getLabel(); | ||
label = oldLabel; | ||
} | ||
if (IngestUtil.conflictsWithExistingFilenames(label, directoryLabel, fmdList, df)) { |
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.
if directoryLabel is not passed in, do we want path to be ""? or keep the old value (as you do with label)?
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.
path
is only used in the error message...
- "Filename already exists at README.md"
- "Filename already exists at code/README.md"
... and I'm happy with how it works. So yes, I think leaving it as "" (empty string) is fine.
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.
Sorry path confused me here, what I'm getting it is:
Why don't we have have a
if (directoryLabel == null) { directoryLabel = oldDrectorylLabel; }
like with label, otherwise null gets passed into the directory+name check, no? (and if we did this, we could really get rid of path altogether and just use directoryLabel + /. And this way if directorLabel were null, we would get a more accurate message anyway:
"Filename already exists at code/README.md", when "Filename already exists at README.md" isn't accurate?
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 got rid of the extra path variable and made sure the messages are accurate.
@scolapasta I responded to your comments. |
String oldLabel = df.getFileMetadata().getLabel(); | ||
String oldDirectoryLabel = df.getFileMetadata().getDirectoryLabel(); | ||
String oldPathPlusName = oldDirectoryLabel + oldLabel; | ||
String incomingPathPlusName = directoryLabel + label; |
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.
Sorry, maybe what I wrote wasn't clear, so let me use an example:
oldDirectory = "directory" and old label = "label"
If I only path in a label as "label" (i.e. no directory), I would expect it to allow it, as I am not changing anything. But the label check would compare "directorylabel" with "nulllabel" and so would run the check when it shouldn't.
Also if (for some reason) for new values you passed directory = "director" and label = "ylabel", the check would find them equal and so not check names, even though you are changing the names.
So the check also needs to have a "/" in there.
String path = ""; | ||
if (directoryLabel != null) { | ||
path = directoryLabel + "/"; | ||
} | ||
if (label == null) { | ||
label = df.getFileMetadata().getLabel(); | ||
label = oldLabel; | ||
} | ||
if (IngestUtil.conflictsWithExistingFilenames(label, directoryLabel, fmdList, df)) { |
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.
Sorry path confused me here, what I'm getting it is:
Why don't we have have a
if (directoryLabel == null) { directoryLabel = oldDrectorylLabel; }
like with label, otherwise null gets passed into the directory+name check, no? (and if we did this, we could really get rid of path altogether and just use directoryLabel + /. And this way if directorLabel were null, we would get a more accurate message anyway:
"Filename already exists at code/README.md", when "Filename already exists at README.md" isn't accurate?
I responded to code review with a couple commits and comments. |
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.
Ver minor cleanup, but I think simplifies the code for maintainability.
path = directoryLabel + "/"; | ||
// If the user is trying to change the label/directoryLabel or not. | ||
boolean labelChange = true; | ||
if (label == null && directoryLabel == null) { |
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.
Last suggested change - we can remove this check since it's handled by the code below (if both are null, they get populated with their old values). Simplifies the code for minimal "cost".
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.
Good idea. Removed in 491caff.
String incomingPathPlusFileName = path + label; | ||
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("files.api.metadata.update.duplicateFile", Arrays.asList(incomingPathPlusFileName))); | ||
if (labelChange && IngestUtil.conflictsWithExistingFilenames(label, directoryLabel, fmdList, df)) { | ||
String pathPlusFilename = ""; |
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 know this is just for the error message, but why not just use "incomingPathPlusName" 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 if we do (I just tried), we get "null/" as part of the output. Tests fail with messages like this:
Expected: Filename already exists at README.md
Actual: Filename already exists at null/README.md
That is to say, that code (below) is there to make sure there's no "null/" in the output, like above.
String pathPlusFilename = "";
if (directoryLabel != null) {
pathPlusFilename = directoryLabel + "/" + label;
} else {
pathPlusFilename = label;
}
If null, populated by old values anyway.
I removed the code we don't need (good catch) and left a comment about the output. |
Not to self, while old directory is null, pass in "" as the directory. |
@scolapasta I addressed this in a34d7c1 |
* While "labelChange" does end up being true, | ||
* IngestUtil.conflictsWithExistingFilenames returns false, so the change is | ||
* allowed to go through. The description is allowed to be changed and the | ||
* directoryLabel remains null even though the user passed in an empty | ||
* string, which is what we want. |
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.
The issue here is that if it gets to the conflictswithexisting stage and that returns false, I think we have a problem:
if conflictswithexisting filenames returns false, it’s because it’s treating ‘’ and null differently, since the name is the same). So if you did the same thing, but actually changed the name of the file to a different file name that does exist? Wouldn’t conflictswithexisting still return false then and allow you to rename to an already existing file.
The use case is you have two files:
• the existing values are null for directory for both, names are file1 and file2
• call the api to edit file1 with “” as directoryLabel and label as “file2"
• I’m concerned that labelChange would be true and conflictswithexisting would be false and that would allow you to have the same null/file2 for both.
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'll add, one solution could be to not store null for directoryLabel ever. We could either store "" or even "/". This would also help in adding a unique constraint, since unique constraints are problematic with nulls.
I attempted to simplify the code in 2debed2. DuplicatesIT and FilesIT both pass. |
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.
OK, this way looks cleaner and should cover issues with null.
What this PR does / why we need it:
A regression was introduced in pull request #6893 whereby modifying file metadata API failed with "Filename already exists". This bug is in develop, not production.
Which issue(s) this PR closes:
Closes #6962
Special notes for your reviewer:
None.
Suggestions on how to test this:
Does this PR introduce a user interface change? If mockups are available, please link/include them here:
No.
Is there a release notes update needed for this change?:
No.
Additional documentation:
None.