Skip to content

Commit

Permalink
Do not pad name for enum when generate SDK from Typespec (#2638)
Browse files Browse the repository at this point in the history
* fix

* changelog

* update
  • Loading branch information
msyyc committed Jun 17, 2024
1 parent 369124f commit bd7d1fd
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/do-not-pad-for-enum-2024-5-14-16-6-32.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@autorest/python"
---

Do not pad name for enum when generate SDK from Typespec
21 changes: 17 additions & 4 deletions packages/autorest.python/autorest/preprocess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .python_mappings import CADL_RESERVED_WORDS, RESERVED_WORDS, PadType

from .. import YamlUpdatePlugin, YamlUpdatePluginAutorest
from .._utils import parse_args, get_body_type_for_description, JSON_REGEXP, KNOWN_TYPES
from .._utils import parse_args, get_body_type_for_description, JSON_REGEXP, KNOWN_TYPES, update_enum_value


def update_overload_section(
Expand Down Expand Up @@ -241,11 +241,24 @@ def update_types(self, yaml_data: List[Dict[str, Any]]) -> None:
type["snakeCaseName"] = to_snake_case(type["name"])
if type.get("values"):
# we're enums
values_to_add = []
for value in type["values"]:
padded_name = self.pad_reserved_words(value["name"].lower(), PadType.ENUM).upper()
if padded_name[0] in "0123456789":
padded_name = "ENUM_" + padded_name
value["name"] = padded_name
if self.version_tolerant:
if padded_name[0] in "0123456789":
padded_name = "ENUM_" + padded_name
value["name"] = padded_name
else:
if value["name"] != padded_name:
values_to_add.append(
update_enum_value(
name=padded_name,
value=value["value"],
description=value["description"],
enum_type=value["enumType"],
)
)
type["values"].extend(values_to_add)

# add type for reference
for v in HEADERS_CONVERT_IN_METHOD.values():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ async def test_operation_with_url(client):

@pytest.mark.asyncio
async def test_operation_with_enum(client):
await client.reserved_enum(models.MyEnum.IMPORT)
await client.reserved_enum(models.MyEnum.IMPORT_ENUM)
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ def test_operation_with_url(client):


def test_operation_with_enum(client):
client.reserved_enum(models.MyEnum.IMPORT)
client.reserved_enum(models.MyEnum.IMPORT_ENUM)
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ async def operation_with_url(
async def reserved_enum(self, enum_parameter: Union[str, _models.MyEnum], **kwargs: Any) -> JSON:
"""Operation that accepts a reserved enum value.
:param enum_parameter: Pass in MyEnum.IMPORT to pass. Known values are: "import" and "other".
Required.
:param enum_parameter: Pass in MyEnum.IMPORT to pass. Known values are: "import", "other", and
"import". Required.
:type enum_parameter: str or ~reservedwords.models.MyEnum
:return: JSON or the result of cls(response)
:rtype: JSON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
class MyEnum(str, Enum, metaclass=CaseInsensitiveEnumMeta):
"""MyEnum."""

IMPORT_ENUM = "import"
IMPORT = "import"
OTHER = "other"
IMPORT_ENUM = "import"
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ def operation_with_url(
def reserved_enum(self, enum_parameter: Union[str, _models.MyEnum], **kwargs: Any) -> JSON:
"""Operation that accepts a reserved enum value.
:param enum_parameter: Pass in MyEnum.IMPORT to pass. Known values are: "import" and "other".
Required.
:param enum_parameter: Pass in MyEnum.IMPORT to pass. Known values are: "import", "other", and
"import". Required.
:type enum_parameter: str or ~reservedwords.models.MyEnum
:return: JSON or the result of cls(response)
:rtype: JSON
Expand Down

0 comments on commit bd7d1fd

Please sign in to comment.