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

fix(jans-cli): sample schema generetation (ref: #8713) #8746

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 86 additions & 17 deletions jans-cli-tui/cli_tui/cli/config_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def read_swagger(op_mode):
help="Arguments to pass endpoint separated by comma. For example limit:5,status:INACTIVE")

parser.add_argument("--schema-sample", help="Get sample json schema template")
parser.add_argument("--schema", help="Get the operation schema which describes all the keys of the schema and its values in detail.")

parser.add_argument("-CC", "--config-api-mtls-client-cert", help="Path to SSL Certificate file")
parser.add_argument("-CK", "--config-api-mtls-client-key", help="Path to SSL Key file")
Expand Down Expand Up @@ -1434,9 +1435,7 @@ def get_schema_from_reference(self, plugin_name, ref):

return schema_


def get_sample_schema(self, schema_name):

def get_schema_dict(self, schema_name):
if ':' in schema_name:
plugin_name, schema_str = schema_name.split(':')
else:
Expand All @@ -1451,36 +1450,104 @@ def get_sample_schema(self, schema_name):
print(self.colored_text("Schema not found.", error_color))
return

return schema

def get_sample_schema(self, schema_name):

schema = self.get_schema_dict(schema_name)
if not schema:
sys.exit()

sample_schema = OrderedDict()
for prop_name in schema.get('properties', {}):
prop = schema['properties'][prop_name]

def get_sample_prop(prop):
if 'default' in prop:
sample_schema[prop_name] = prop['default']
return prop['default']
elif 'example' in prop:
sample_schema[prop_name] = prop['example']
return prop['example']
elif 'enum' in prop:
sample_schema[prop_name] = random.choice(prop['enum'])
return random.choice(prop['enum'])
elif prop.get('type') == 'object':
sample_schema[prop_name] = prop.get('properties', {})
sub_prop = OrderedDict()
for sp in prop.get('properties', {}):
sub_prop[sp] = get_sample_prop(prop['properties'][sp])
return sub_prop
elif prop.get('type') == 'array':
if 'items' in prop:
if 'enum' in prop['items']:
sample_schema[prop_name] = [random.choice(prop['items']['enum'])]
return [random.choice(prop['items']['enum'])]
elif 'type' in prop['items']:
sample_schema[prop_name] = [prop['items']['type']]
return [prop['items']['type']]
else:
sample_schema[prop_name] = []
return []
elif prop.get('type') == 'boolean':
sample_schema[prop_name] = random.choice((True, False))
return random.choice((True, False))
elif prop.get('type') == 'integer':
sample_schema[prop_name] = random.randint(1,200)
return random.randint(1,200)
elif prop.get('type') == 'string' and prop.get('format') == 'binary':
sample_schema[prop_name] = file_data_type
return file_data_type
else:
sample_schema[prop_name]='string'
return 'string'

for prop_name in schema.get('properties', {}):
prop = schema['properties'][prop_name]
sample_schema[prop_name] = get_sample_prop(prop)

print(json.dumps(sample_schema, indent=2))

def get_schema(self, schema_name):

schema = self.get_schema_dict(schema_name)
if not schema:
sys.exit()

print_list = []
def get_prop_def(prop_name, prop):
propl = [prop_name]
ptype = prop.get('type', '__NA__')
enum = None
if ptype == 'array' and 'items' in prop:
if 'type' in prop['items']:
ptype += ' of ' + prop['items']['type']
if 'enum' in prop['items']:
enum = 'enum: ' + str(prop['items']['enum'])
pprop = [ptype]
if enum:
pprop.append(enum)

for props in prop:
if props in ('type', 'properties', 'items', 'title', '__schema_name__'):
continue
pprop.append(props+': ' + str(prop[props]))

if ptype == 'object':
for sub_prop_name in prop.get('properties', {}):
pprop.append(get_prop_def(sub_prop_name, prop['properties'][sub_prop_name]))


propl.append(pprop)

return propl

for prop_name in schema.get('properties', {}):
prop_def = get_prop_def(prop_name, schema['properties'][prop_name])
print_list.append(prop_def)

max_title_len = 0
for p in print_list:
len_= len(p[0])
if len_ > max_title_len:
max_title_len = len_
required = schema.get('required', [])
for pname, pprop in print_list:
if pname in required:
pname += '*'
print(pname.ljust(max_title_len+2), pprop[0])
for p in pprop[1:]:
if isinstance(p, list):
print(' ' *(max_title_len+4), p[0]+':', p[1][0])
else:
print(' ' *(max_title_len+2), p)

def unescaped_split(self, s, delimeter, escape_char='\\'):
ret_val = []
Expand Down Expand Up @@ -1527,8 +1594,10 @@ def main():

if args.info:
cli_object.help_for(args.info)
elif args.schema_sample:
cli_object.get_sample_schema(args.schema_sample)
elif args.schema:
cli_object.get_sample_schema(args.schema)
cli_object.get_schema(args.schema)
elif args.operation_id:
cli_object.process_command_by_id(args.operation_id, args.url_suffix, args.endpoint_args, args.data)
elif args.output_access_token:
Expand Down