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

Close unreleased InputStreams in the Rule Engines and ApiReader #1234

Merged
merged 7 commits into from
Aug 13, 2024

Conversation

luis-pabon-tf
Copy link
Contributor

Close unreleased InputStreams in the Rule Engines and ApiReader

Added closing statements in such a way that output is unchanged for a couple methods.

Issue

#1212

Checklist

  • I have added tests to cover my changes
  • I have added logging where useful (with appropriate log level)
  • I have added JavaDocs where required
  • I have updated the documentation accordingly

Note: You may remove items that are not applicable

Add closing statement before returning the output of openAsString
@luis-pabon-tf luis-pabon-tf marked this pull request as ready for review August 8, 2024 20:24
Copy link
Contributor

@basiliskus basiliskus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I just left a couple of comments/suggestions

@@ -143,7 +144,8 @@ public Map<HttpEndpoint, Function<DomainRequest, DomainResponse>> domainRegistra
@Override
public String openApiSpecification() throws UnableToReadOpenApiSpecificationException {
String fileName = "openapi_etor.yaml";
return OpenApiReaderImplementation.getInstance().openAsString(fileName);
return OpenApiReaderImplementation.getInstance()
.openAsString(fileName, StandardCharsets.UTF_8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't pass in StandardCharset.UTF_8. There's no reason for .openAsString(...) to support other charsets. Instead, hardcode StandardCharsets.UTF_8 in OpenApiReaderImplementation#openAsString.

This applies to the other places where we call OpenApiReaderImplementation.getInstance() .openAsString(...).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had made this change for this PR. Reverted the change, the diff should be a bit smaller now too

public String openAsString(String fileName, Charset charset)
throws UnableToReadOpenApiSpecificationException {
try (InputStream stream = getClass().getClassLoader().getResourceAsStream(fileName)) {
var out = new String(Objects.requireNonNull(stream).readAllBytes(), charset);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the Objects.requireNonNull(). stream will be not be null if we get to this point.

Copy link
Contributor Author

@luis-pabon-tf luis-pabon-tf Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have that there to silence a warning on the IDE, but since we're catching the NullPointerException that it's worried about it's probably a false positive. Will do!

@@ -53,6 +54,7 @@ public void ensureRulesLoaded() throws RuleLoaderException {
ruleLoader.loadRules(resourceStream, new TypeReference<>() {});
rules.addAll(parsedRules);
rulesLoaded = true;
IOUtils.closeQuietly(resourceStream);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use this. Use the "try with resources" mechanism. I.e. the same thing that @basiliskus mentioned below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the catch, I would catch IOException | NullPointerException and throw a throw new RuleLoaderException("File not found: " + ruleDefinitionsFileName, new FileNotFoundException()); like above.

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
70.0% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

@luis-pabon-tf
Copy link
Contributor Author

luis-pabon-tf commented Aug 12, 2024

Need to refactor the rule-loading code so it's less duplicated across Transformation and Validation Rule Engines...

We're keeping the engine code separate as they are expected to be very different in the future.

@halprin halprin merged commit 39769b2 into main Aug 13, 2024
15 of 16 checks passed
@halprin halprin deleted the story/1212/release-resources branch August 13, 2024 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants