Skip to content

[ZEPPELIN-6309] Improve method by replacing JsonObject parameter - #5063

Merged
ParkGyeongTae merged 4 commits into
apache:masterfrom
celinayk:ZEPPELIN-6309
Sep 15, 2025
Merged

[ZEPPELIN-6309] Improve method by replacing JsonObject parameter#5063
ParkGyeongTae merged 4 commits into
apache:masterfrom
celinayk:ZEPPELIN-6309

Conversation

@celinayk

Copy link
Copy Markdown
Contributor

What is this PR for?

Refactored the convertPermissionsFromUsersToOwners method in InterpreterSetting.java to improve code readability and maintainability by separating JSON parsing logic from business logic, addressing the TODO comment that identified this as "ugly code".

What type of PR is it?

Refactoring

Todos

  • - Refactor convertPermissionsFromUsersToOwners method to remove JsonObject parameter
  • - Extract JSON parsing logic into separate static helper method
  • - Update all callers to use new method signature

What is the Jira issue?

ZEPPELIN-6309

How should this be tested?

Screenshots (if appropriate)

Questions:

  • Does the license files need to update? No
  • Is there breaking changes for older versions? No
  • Does this needs documentation? No

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the convertPermissionsFromUsersToOwners method in InterpreterSetting.java by removing the JsonObject parameter and separating JSON parsing logic from business logic, addressing a TODO comment about "ugly code".

  • Extracted JSON parsing into a new static helper method extractUsersFromJsonObject
  • Simplified the main method to accept a List<String> instead of JsonObject
  • Updated all callers to use the new method signature with proper JSON parsing

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

FileDescription
InterpreterSetting.javaRefactored main method and added new static helper method for JSON parsing
ConfigStorage.javaUpdated caller to use new method signature with extracted users list
InterpreterInfoSaving.javaUpdated caller to use new method signature with extracted users list

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +1029 to +1041
List<String> users = new ArrayList<>();
if (jsonObject != null) {
JsonObject option = jsonObject.getAsJsonObject("option");
if (option != null) {
JsonArray users = option.getAsJsonArray("users");
if (users != null) {
if (this.option.getOwners() == null) {
this.option.owners = new LinkedList<>();
}
for (JsonElement user : users) {
this.option.getOwners().add(user.getAsString());
JsonArray usersArray = option.getAsJsonArray("users");
if (usersArray != null) {
for (JsonElement userElement : usersArray) {
users.add(userElement.getAsString());
}
}
}
}
return users;

CopilotAISep 1, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] Consider using ArrayList constructor with initial capacity or LinkedList to match the pattern used in the convertPermissionsFromUsersToOwners method where LinkedList is used for owners.

Suggested change
List<String> users = newArrayList<>();
if (jsonObject != null) {
JsonObjectoption = jsonObject.getAsJsonObject("option");
if (option != null) {
JsonArrayusers = option.getAsJsonArray("users");
if (users != null) {
if (this.option.getOwners() == null) {
this.option.owners = newLinkedList<>();
}
for (JsonElementuser : users) {
this.option.getOwners().add(user.getAsString());
JsonArrayusersArray = option.getAsJsonArray("users");
if (usersArray != null) {
for (JsonElementuserElement : usersArray) {
users.add(userElement.getAsString());
}
}
}
}
returnusers;
List<String> users = null;
if (jsonObject != null) {
JsonObjectoption = jsonObject.getAsJsonObject("option");
if (option != null) {
JsonArrayusersArray = option.getAsJsonArray("users");
if (usersArray != null) {
users = newArrayList<>(usersArray.size());
for (JsonElementuserElement : usersArray) {
users.add(userElement.getAsString());
}
}
}
}
returnusers == null ? newArrayList<>() : users;

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Good catch! Updated to use LinkedList for consistency with the rest of the class. This maintains consistency with the owners field initialization on line.

celinaykand others added 2 commits September 2, 2025 18:27
…figStorage.java
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
interpreterSetting.convertPermissionsFromUsersToOwners(
jsonObject.getAsJsonObject("interpreterSettings")
.getAsJsonObject(interpreterSetting.getId()));
JsonObject interpreterSettingJson = jsonObject.getAsJsonObject("interpreterSettings")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are you sure this is what is meant by “ugly” here?
I think we should use GSON here to create objects directly.

GSON.fromJson(json, myFanceClass.class);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Are you sure this is what is meant by “ugly” here? I think we should use GSON here to create objects directly.

GSON.fromJson(json, myFanceClass.class);

Thank you for the feedback! You're absolutely right. I've refactored the code to use GSON.fromJson() directly
instead of JsonObject parameter.

@ParkGyeongTaeParkGyeongTae left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 👍

@ParkGyeongTae
ParkGyeongTae merged commit 75a9caa into apache:masterSep 15, 2025
14 of 18 checks passed
ParkGyeongTae pushed a commit that referenced this pull request Sep 15, 2025
### What is this PR for?
Refactored the `convertPermissionsFromUsersToOwners` method in InterpreterSetting.java to improve code readability and maintainability by separating JSON parsing logic from business logic, addressing the TODO comment that identified this as "ugly code".
### What type of PR is it?
Refactoring
### Todos
* [x] - Refactor convertPermissionsFromUsersToOwners method to remove JsonObject parameter
* [x] - Extract JSON parsing logic into separate static helper method
* [x] - Update all callers to use new method signature
### What is the Jira issue?
[ZEPPELIN-6309](https://issues.apache.org/jira/browse/ZEPPELIN-6309)
### How should this be tested?
### Screenshots (if appropriate)
### Questions:
* Does the license files need to update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No
Closes#5063 from celinayk/ZEPPELIN-6309.
Signed-off-by: ParkGyeongTae <gyeongtae@apache.org>
(cherry picked from commit 75a9caa)
Signed-off-by: ParkGyeongTae <gyeongtae@apache.org>
@ParkGyeongTaeParkGyeongTae self-assigned this Sep 15, 2025
@ParkGyeongTae

Copy link
Copy Markdown
Member

Merged into master and branch-0.12

tbonelee added a commit that referenced this pull request Sep 27, 2025
…ers.json
### What is this PR for?
Tests in the `zeppelin-zengine` module were failing. The main reason was that the `interpreter.json` in test resources were invalid: the value object did not have an `id` field matching its corresponding key.
As a result, the deserialized `InterpreterSetting` instance had an auto-genarated `id` field, causing the key and value.id to be inconsistent.
Before #5063, invalid settings were simply skipped. However, after that change, they caused a NPE.
This PR fixes the invalid JSON files and adds minor validation logic in the deserialization method so that such issues can be detected early by users.
### What type of PR is it?
Bug Fix
### What is the Jira issue?
https://issues.apache.org/jira/browse/ZEPPELIN-6335
### How should this be tested?
* Check `core-modules` - `zeppelin-zengine` tests in CI
### Questions:
* Does the license files need to update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No
Closes#5081 from tbonelee/fix-test-resource.
Signed-off-by: ChanHo Lee <chanholee@apache.org>
tbonelee added a commit that referenced this pull request Sep 27, 2025
…ers.json
### What is this PR for?
Tests in the `zeppelin-zengine` module were failing. The main reason was that the `interpreter.json` in test resources were invalid: the value object did not have an `id` field matching its corresponding key.
As a result, the deserialized `InterpreterSetting` instance had an auto-genarated `id` field, causing the key and value.id to be inconsistent.
Before #5063, invalid settings were simply skipped. However, after that change, they caused a NPE.
This PR fixes the invalid JSON files and adds minor validation logic in the deserialization method so that such issues can be detected early by users.
### What type of PR is it?
Bug Fix
### What is the Jira issue?
https://issues.apache.org/jira/browse/ZEPPELIN-6335
### How should this be tested?
* Check `core-modules` - `zeppelin-zengine` tests in CI
### Questions:
* Does the license files need to update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No
Closes#5081 from tbonelee/fix-test-resource.
Signed-off-by: ChanHo Lee <chanholee@apache.org>
(cherry picked from commit 80ff51c)
Signed-off-by: ChanHo Lee <chanholee@apache.org>
Sign up for freeto 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.

4 participants

@celinayk@ParkGyeongTae@Reamer