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

Use try-with-resources to guarantee closing of JsonReader in .../api/DataSets.java #10033

Closed
rtreacy opened this issue Oct 23, 2023 · 3 comments · Fixed by #9980
Closed

Use try-with-resources to guarantee closing of JsonReader in .../api/DataSets.java #10033

rtreacy opened this issue Oct 23, 2023 · 3 comments · Fixed by #9980
Labels
D: SonarQubeCleanup Issues related to cleanup based on Sonar Cube tool hacktoberfest It's Hacktoberfest! https://groups.google.com/g/dataverse-community/c/n_Nn_T2yA-w/m/BcoXO4tEAQAJ Size: 3 A percentage of a sprint. 2.1 hours.
Milestone

Comments

@rtreacy
Copy link
Contributor

rtreacy commented Oct 23, 2023

What steps does it take to reproduce the issue?
This issue is reported by running static analysis with SonarQube or SonarCloud

  • When does this issue occur?
    There are 7 occurrences of this code:
    JsonObject json = Json.createReader(rdr).readObject();

in DataSets.java in the following methods -

public Response updateDraftVersion(...)
private Response processDatasetFieldDataDelete(...)
private Response processDatasetUpdate(...)
public Response createFileEmbargo(...)
public Response removeFileEmbargo(...)
public Response returnToAuthor(...)
public Response completeMPUpload(...)

  • What happens?
    This can prevent JsonReader objects from properly closing and being Garbage Collected and thus prevent referenced objects from being GCed as well.
@qqmyers
Copy link
Member

qqmyers commented Oct 23, 2023

As with earlier issues, replacing these with the appropriate JsonUtil method would be a good fix.

public static jakarta.json.JsonObject getJsonObject(String serializedJson) {
try (StringReader rdr = new StringReader(serializedJson)) {
try (JsonReader jsonReader = Json.createReader(rdr)) {
return jsonReader.readObject();
}
}
}
public static jakarta.json.JsonArray getJsonArray(String serializedJson) {
try (StringReader rdr = new StringReader(serializedJson)) {
try (JsonReader jsonReader = Json.createReader(rdr)) {
return jsonReader.readArray();
}
}
}
}

@qqmyers qqmyers added hacktoberfest It's Hacktoberfest! https://groups.google.com/g/dataverse-community/c/n_Nn_T2yA-w/m/BcoXO4tEAQAJ Size: 3 A percentage of a sprint. 2.1 hours. labels Oct 23, 2023
@rtreacy rtreacy added the D: SonarQubeCleanup Issues related to cleanup based on Sonar Cube tool label Oct 23, 2023
@scolapasta scolapasta moved this to SonarQube cleanup (Gustavo) in IQSS Dataverse Project Oct 23, 2023
@bencomp
Copy link
Contributor

bencomp commented Oct 23, 2023

Ah, this is the issue I didn't create for #9980. I think that PR fixes this.

@rtreacy
Copy link
Contributor Author

rtreacy commented Oct 24, 2023

Great, thanks for being proactive on this. We will be reviewing. #9980 soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D: SonarQubeCleanup Issues related to cleanup based on Sonar Cube tool hacktoberfest It's Hacktoberfest! https://groups.google.com/g/dataverse-community/c/n_Nn_T2yA-w/m/BcoXO4tEAQAJ Size: 3 A percentage of a sprint. 2.1 hours.
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

4 participants