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

allow changes to file metadata via API #6962 #6968

Merged
merged 7 commits into from
Jul 1, 2020
Merged

allow changes to file metadata via API #6962 #6968

merged 7 commits into from
Jul 1, 2020

Conversation

pdurbin
Copy link
Member

@pdurbin pdurbin commented Jun 5, 2020

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.

@coveralls
Copy link

coveralls commented Jun 5, 2020

Coverage Status

Coverage increased (+0.008%) to 19.593% when pulling 2debed2 on 6962-bugfix into 97e7a60 on develop.

@pdurbin pdurbin moved this from Code Review 🦁 to IQSS Team - In Progress 💻 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 8, 2020
@pdurbin pdurbin self-assigned this Jun 8, 2020
@pdurbin pdurbin moved this from IQSS Team - In Progress 💻 to Code Review 🦁 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 8, 2020
@pdurbin pdurbin removed their assignment Jun 8, 2020
@pdurbin
Copy link
Member Author

pdurbin commented Jun 8, 2020

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.

Copy link
Contributor

@scolapasta scolapasta left a 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;
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

Comment on lines 407 to 402
String path = "";
if (directoryLabel != null) {
path = directoryLabel + "/";
}
if (label == null) {
label = df.getFileMetadata().getLabel();
label = oldLabel;
}
if (IngestUtil.conflictsWithExistingFilenames(label, directoryLabel, fmdList, df)) {
Copy link
Contributor

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)?

Copy link
Member Author

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.

Copy link
Contributor

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?

Copy link
Member Author

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.

@pdurbin
Copy link
Member Author

pdurbin commented Jun 8, 2020

@scolapasta I responded to your comments.

String oldLabel = df.getFileMetadata().getLabel();
String oldDirectoryLabel = df.getFileMetadata().getDirectoryLabel();
String oldPathPlusName = oldDirectoryLabel + oldLabel;
String incomingPathPlusName = directoryLabel + label;
Copy link
Contributor

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.

Comment on lines 407 to 402
String path = "";
if (directoryLabel != null) {
path = directoryLabel + "/";
}
if (label == null) {
label = df.getFileMetadata().getLabel();
label = oldLabel;
}
if (IngestUtil.conflictsWithExistingFilenames(label, directoryLabel, fmdList, df)) {
Copy link
Contributor

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?

@pdurbin pdurbin moved this from Code Review 🦁 to IQSS Team - In Progress 💻 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 8, 2020
@pdurbin pdurbin self-assigned this Jun 8, 2020
@pdurbin pdurbin moved this from IQSS Team - In Progress 💻 to Code Review 🦁 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 9, 2020
@pdurbin pdurbin assigned pdurbin and unassigned pdurbin Jun 9, 2020
@pdurbin pdurbin moved this from Code Review 🦁 to IQSS Team - In Progress 💻 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 9, 2020
@pdurbin pdurbin moved this from IQSS Team - In Progress 💻 to Code Review 🦁 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 9, 2020
@pdurbin pdurbin removed their assignment Jun 9, 2020
@pdurbin
Copy link
Member Author

pdurbin commented Jun 9, 2020

I responded to code review with a couple commits and comments.

Copy link
Contributor

@scolapasta scolapasta left a 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) {
Copy link
Contributor

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".

Copy link
Member Author

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 = "";
Copy link
Contributor

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?

Copy link
Member Author

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;
}

@pdurbin pdurbin moved this from Code Review 🦁 to IQSS Team - In Progress 💻 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 9, 2020
@pdurbin pdurbin self-assigned this Jun 9, 2020
If null, populated by old values anyway.
@pdurbin pdurbin moved this from IQSS Team - In Progress 💻 to Code Review 🦁 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 9, 2020
@pdurbin pdurbin removed their assignment Jun 9, 2020
@pdurbin
Copy link
Member Author

pdurbin commented Jun 9, 2020

I removed the code we don't need (good catch) and left a comment about the output.

@pdurbin pdurbin moved this from Code Review 🦁 to IQSS Team - In Progress 💻 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 9, 2020
@pdurbin pdurbin self-assigned this Jun 9, 2020
@pdurbin
Copy link
Member Author

pdurbin commented Jun 9, 2020

Not to self, while old directory is null, pass in "" as the directory.

@pdurbin pdurbin moved this from IQSS Team - In Progress 💻 to Code Review 🦁 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 10, 2020
@pdurbin pdurbin removed their assignment Jun 10, 2020
@pdurbin
Copy link
Member Author

pdurbin commented Jun 10, 2020

Not to self, while old directory is null, pass in "" as the directory.

@scolapasta I addressed this in a34d7c1

Comment on lines +432 to +436
* 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.
Copy link
Contributor

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.

Copy link
Contributor

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.

@pdurbin pdurbin moved this from Up Next 🛎 to Code Review 🦁 in IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) Jun 15, 2020
@pdurbin
Copy link
Member Author

pdurbin commented Jun 15, 2020

I attempted to simplify the code in 2debed2. DuplicatesIT and FilesIT both pass.

IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) automation moved this from Code Review 🦁 to QA 🔎✅ Jun 16, 2020
Copy link
Contributor

@scolapasta scolapasta left a 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.

@kcondon kcondon self-assigned this Jun 22, 2020
@kcondon kcondon merged commit bc06fe6 into develop Jul 1, 2020
IQSS/dataverse (TO BE RETIRED / DELETED in favor of project 34) automation moved this from QA 🔎✅ to Done 🚀 Jul 1, 2020
@kcondon kcondon deleted the 6962-bugfix branch July 1, 2020 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Unable to change file metadata via API if not also changing label or directoryLabel
6 participants