Skip to content

Commit

Permalink
Merge pull request #275 from kairu-ms/fix-disc-special-chars-in-value
Browse files Browse the repository at this point in the history
Fix bug when special characters appreared in discription value
  • Loading branch information
kairu-ms authored Aug 4, 2023
2 parents a0e4f79 + ccfe2ef commit db08bd2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/aaz_dev/cli/controller/az_operation_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,8 @@ def _iter_request_scopes_by_schema_base(schema, name, scope_define, arg_key, cmd
for disc in discriminators:
key_name = disc.property
key_value = disc.value
disc_name = f"disc_{to_snack_case(disc.value)}"
disc_name = f"disc_{to_snack_case(disc.get_safe_value())}"

disc_scope_define = scope_define + "{" + key_name + ":" + key_value + "}"
disc_arg_key = arg_key
for scopes in _iter_request_scopes_by_schema_base(disc, disc_name, disc_scope_define, disc_arg_key, cmd_ctx):
Expand Down Expand Up @@ -879,7 +880,7 @@ def _iter_response_scopes_by_schema_base(schema, name, scope_define, cmd_ctx):
for disc in discriminators:
key_name = to_snack_case(disc.property)
key_value = disc.value
disc_name = f"disc_{to_snack_case(disc.value)}"
disc_name = f"disc_{to_snack_case(disc.get_safe_value())}"

disc_scope_define = f'{scope_define}.discriminate_by("{key_name}", "{key_value}")'
for scopes in _iter_response_scopes_by_schema_base(disc, disc_name, disc_scope_define, cmd_ctx):
Expand Down
8 changes: 4 additions & 4 deletions src/aaz_dev/command/controller/cfg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def find_sub_schema(cls, schema, idx):

elif schema.discriminators:
for disc in schema.discriminators:
if current_idx == disc.value:
if current_idx == disc.get_safe_value():
if not remain_idx:
return disc
return cls.find_sub_schema(disc, remain_idx)
Expand All @@ -716,7 +716,7 @@ def find_sub_schema(cls, schema, idx):

elif schema.discriminators:
for disc in schema.discriminators:
if current_idx == disc.value:
if current_idx == disc.get_safe_value():
if not remain_idx:
return disc
return cls.find_sub_schema(disc, remain_idx)
Expand Down Expand Up @@ -934,7 +934,7 @@ def _iter_sub_schema(cls, parent, schema_filter):
for disc in parent.discriminators:
for sub_parent, sub_schema, sub_schema_idx in cls._iter_sub_schema(disc, schema_filter):
if sub_schema:
sub_schema_idx = [disc.value, *sub_schema_idx]
sub_schema_idx = [disc.get_safe_value(), *sub_schema_idx]
yield sub_parent, sub_schema, sub_schema_idx

elif isinstance(parent, CMDObjectSchemaDiscriminator):
Expand All @@ -956,7 +956,7 @@ def _iter_sub_schema(cls, parent, schema_filter):
for disc in parent.discriminators:
for sub_parent, sub_schema, sub_schema_idx in cls._iter_sub_schema(disc, schema_filter):
if sub_schema:
sub_schema_idx = [disc.value, *sub_schema_idx]
sub_schema_idx = [disc.get_safe_value(), *sub_schema_idx]
yield sub_parent, sub_schema, sub_schema_idx

elif isinstance(parent, CMDArraySchemaBase):
Expand Down
4 changes: 2 additions & 2 deletions src/aaz_dev/command/controller/workspace_cfg_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ def _build_object_index_base(cls, schema, idx, index=None, prune=False, **kwargs

if schema.discriminators:
for disc in schema.discriminators:
if disc.value == current_idx:
if disc.get_safe_value() == current_idx:
index.discriminator = cls._build_object_index_discriminator(disc, remain_idx, **kwargs)
break

Expand Down Expand Up @@ -1519,7 +1519,7 @@ def _build_object_index_discriminator(cls, schema, idx, **kwargs):

if schema.discriminators:
for disc in schema.discriminators:
if disc.value == current_idx:
if disc.get_safe_value() == current_idx:
index.discriminator = cls._build_object_index_discriminator(disc, remain_idx, **kwargs)
break

Expand Down
4 changes: 2 additions & 2 deletions src/aaz_dev/command/model/configuration/_arg_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def new_builder(cls, schema, parent=None, var_prefix=None, ref_args=None, ref_ar
if not arg_var.endswith("$"):
arg_var += '.'
if isinstance(schema, CMDObjectSchemaDiscriminator):
arg_var += f'{schema.value}'
arg_var += schema.get_safe_value()
elif isinstance(schema, CMDSchema):
arg_var += f'{schema.name}'.replace('$', '') # some schema name may contain $
else:
Expand Down Expand Up @@ -302,7 +302,7 @@ def get_options(self):
return [*self._ref_arg.options]

if isinstance(self.schema, CMDObjectSchemaDiscriminator):
opt_name = self._build_option_name(self.schema.value)
opt_name = self._build_option_name(self.schema.get_safe_value())
elif isinstance(self.schema, CMDSchema):
name = self.schema.name.replace('$', '')
if name == "[Index]" or name == "{Key}":
Expand Down
7 changes: 6 additions & 1 deletion src/aaz_dev/command/model/configuration/_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from utils import exceptions

import logging

import re

logger = logging.getLogger('backend')

Expand Down Expand Up @@ -781,6 +781,11 @@ def reformat(self, **kwargs):
disc.reformat(**kwargs)
self.discriminators = sorted(self.discriminators, key=lambda disc: disc.value)

def get_safe_value(self):
"""Some value may contain special characters such as Microsoft.db/mysql, it will cause issues. This function will replase them by `_`
"""
safe_value = re.sub(r'[^A-Za-z0-9_-]', '_', self.value)
return safe_value

# additionalProperties
class CMDObjectSchemaAdditionalProperties(Model):
Expand Down

0 comments on commit db08bd2

Please sign in to comment.