Skip to content

Commit

Permalink
fix(jans-cli-tui): properties object with no keys (#10411)
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Baser <[email protected]>
  • Loading branch information
devrimyatar authored Dec 12, 2024
1 parent 19296b7 commit e0f55a0
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions jans-cli-tui/cli_tui/plugins/010_auth_server/view_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from prompt_toolkit.widgets import Button, Dialog, Label

from cli import config_cli
from utils.static import DialogResult, cli_style
from utils.static import DialogResult, cli_style, common_strings
from utils.utils import DialogUtils
from utils.multi_lang import _
from wui_components.jans_cli_dialog import JansGDialog
Expand Down Expand Up @@ -48,6 +48,7 @@ def __init__(
self.op_type = op_type
self.value_content = HSplit([],width=D())
self.tab_widget = None
self.check_json_types = []
self.widgets = []
self.buttons = [Button(text=_("Cancel"), handler=self.cancel), Button(text=_("Save"), handler=self.save)]
self.prepare_properties()
Expand All @@ -59,6 +60,20 @@ def cancel(self) -> None:

self.future.set_result(DialogResult.CANCEL)


def check_json_type(self, data):
try:
json.loads(data)
return True
except Exception as _:
self.app.show_message(
title=common_strings.error,
message=f"Value should {data} be in correct JSON format.",
tobefocused=self.dialog.body
)
return False


def save(self) -> None:
"""method to invoked when saving the dialog (Save button is pressed)
"""
Expand All @@ -67,6 +82,10 @@ def save(self) -> None:
item_data = self.get_item_data(self.widgets[0])
data = item_data['value']

if item_data['key'] in self.check_json_types:
if not self.check_json_type(data):
return

elif self.tab_widget:
data = []
tabn = []
Expand All @@ -84,6 +103,9 @@ def save(self) -> None:
item_data = self.get_item_data(widget)
if item_data:
data[item_data['key']] = item_data['value']
if item_data['key'] in self.check_json_types:
if not self.check_json_type(item_data['value']):
return

cli_args = {'operation_id': 'patch-properties', 'data': [ {'op':self.op_type, 'path': self.property_name, 'value': data } ]}

Expand Down Expand Up @@ -343,7 +365,12 @@ def prepare_properties(self):
self.buttons.append(Button(_("Delete"), handler=self.delete_tab_element))

elif properties['type'] == 'object':
self.widgets = self.get_widgets(properties['properties'], values=self.value)

if 'properties' in properties:
self.widgets = self.get_widgets(properties['properties'], values=self.value)
else:
self.widgets = self.get_widgets({properties['title']: {'type': 'string'}}, values={properties['title']: self.value})
self.check_json_types.append(properties['title'])

if not self.tab_widget:
self.value_content = HSplit(self.widgets, width=D())
Expand Down

0 comments on commit e0f55a0

Please sign in to comment.