diff --git a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/check_name_availability_result.py b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/check_name_availability_result.py index cc1c87848bdf4..dc3874fdaac0e 100644 --- a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/check_name_availability_result.py +++ b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/check_name_availability_result.py @@ -23,7 +23,8 @@ class CheckNameAvailabilityResult(Model): :param reason: Gets the reason that a storage account name could not be used. The Reason element is only returned if NameAvailable is false. Possible values include: 'AccountNameInvalid', 'AlreadyExists' - :type reason: str + :type reason: str or :class:`Reason + ` :param message: Gets an error message explaining the Reason value in more detail. :type message: str diff --git a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account.py b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account.py index bff1707d78076..8ed5eace271c2 100644 --- a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account.py +++ b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account.py @@ -32,11 +32,13 @@ class StorageAccount(Resource): :param provisioning_state: Gets the status of the storage account at the time the operation was called. Possible values include: 'Creating', 'ResolvingDNS', 'Succeeded' - :type provisioning_state: str + :type provisioning_state: str or :class:`ProvisioningState + ` :param account_type: Gets the type of the storage account. Possible values include: 'Standard_LRS', 'Standard_ZRS', 'Standard_GRS', 'Standard_RAGRS', 'Premium_LRS' - :type account_type: str + :type account_type: str or :class:`AccountType + ` :param primary_endpoints: Gets the URLs that are used to perform a retrieval of a public blob, queue or table object.Note that StandardZRS and PremiumLRS accounts only return the blob endpoint. @@ -48,7 +50,8 @@ class StorageAccount(Resource): :param status_of_primary: Gets the status indicating whether the primary location of the storage account is available or unavailable. Possible values include: 'Available', 'Unavailable' - :type status_of_primary: str + :type status_of_primary: str or :class:`AccountStatus + ` :param last_geo_failover_time: Gets the timestamp of the most recent instance of a failover to the secondary location. Only the most recent timestamp is retained. This element is not returned if there has never @@ -63,7 +66,8 @@ class StorageAccount(Resource): secondary location of the storage account is available or unavailable. Only available if the accountType is StandardGRS or StandardRAGRS. Possible values include: 'Available', 'Unavailable' - :type status_of_secondary: str + :type status_of_secondary: str or :class:`AccountStatus + ` :param creation_time: Gets the creation date and time of the storage account in UTC. :type creation_time: datetime diff --git a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_create_parameters.py b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_create_parameters.py index 31535a2dc8c20..33c7e0b3d7bc1 100644 --- a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_create_parameters.py +++ b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_create_parameters.py @@ -32,7 +32,8 @@ class StorageAccountCreateParameters(Resource): :param account_type: Gets or sets the account type. Possible values include: 'Standard_LRS', 'Standard_ZRS', 'Standard_GRS', 'Standard_RAGRS', 'Premium_LRS' - :type account_type: str + :type account_type: str or :class:`AccountType + ` """ _validation = { diff --git a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_regenerate_key_parameters.py b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_regenerate_key_parameters.py index 7fe2da5d56729..7a1da3a0e5757 100644 --- a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_regenerate_key_parameters.py +++ b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_regenerate_key_parameters.py @@ -16,7 +16,8 @@ class StorageAccountRegenerateKeyParameters(Model): """StorageAccountRegenerateKeyParameters :param key_name: Possible values include: 'key1', 'key2' - :type key_name: str + :type key_name: str or :class:`KeyName + ` """ _attribute_map = { diff --git a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_update_parameters.py b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_update_parameters.py index e60b187d613bd..f858c85e39aaf 100644 --- a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_update_parameters.py +++ b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/storage_account_update_parameters.py @@ -34,7 +34,8 @@ class StorageAccountUpdateParameters(Resource): other account types cannot be changed to StandardZRS or PremiumLRS. Possible values include: 'Standard_LRS', 'Standard_ZRS', 'Standard_GRS', 'Standard_RAGRS', 'Premium_LRS' - :type account_type: str + :type account_type: str or :class:`AccountType + ` :param custom_domain: User domain assigned to the storage account. Name is the CNAME source. Only one custom domain is supported per storage account at this time. To clear the existing custom domain, use an empty diff --git a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/usage.py b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/usage.py index 53d96b70adf16..9b3983b234a51 100644 --- a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/usage.py +++ b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/models/usage.py @@ -19,7 +19,8 @@ class Usage(Model): :param unit: Gets the unit of measurement. Possible values include: 'Count', 'Bytes', 'Seconds', 'Percent', 'CountsPerSecond', 'BytesPerSecond' - :type unit: str + :type unit: str or :class:`UsageUnit + ` :param current_value: Gets the current count of the allocated resources in the subscription. :type current_value: int diff --git a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/operations/storage_accounts_operations.py b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/operations/storage_accounts_operations.py index 2250ab10c1468..8d54b890d73fd 100644 --- a/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/operations/storage_accounts_operations.py +++ b/AutoRest/Generators/Python/Azure.Python.Tests/Expected/AcceptanceTests/StorageManagementClient/storagemanagementclient/operations/storage_accounts_operations.py @@ -619,7 +619,8 @@ def regenerate_key( only. :type account_name: str :param key_name: Possible values include: 'key1', 'key2' - :type key_name: str + :type key_name: str or :class:`KeyName + ` :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response diff --git a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/complex_tests.py b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/complex_tests.py index 88d8e9ee7db3b..fa065c09c6e6a 100644 --- a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/complex_tests.py +++ b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/complex_tests.py @@ -68,9 +68,11 @@ def test_complex(self): basic_result = client.basic_operations.get_valid() self.assertEqual(2, basic_result.id) self.assertEqual("abc", basic_result.name); - self.assertEqual(CMYKColors.yellow, basic_result.color); + self.assertEqual(CMYKColors.yellow.value, basic_result.color); # PUT basic/valid + basic_result = Basic(id=2, name='abc', color="Magenta") + client.basic_operations.put_valid(basic_result) basic_result = Basic(id=2, name='abc', color=CMYKColors.magenta) client.basic_operations.put_valid(basic_result) diff --git a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/header_tests.py b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/header_tests.py index ef5ccdbec97ae..e6649e6378f07 100644 --- a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/header_tests.py +++ b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/header_tests.py @@ -107,6 +107,7 @@ def test_headers(self): self.assertEqual("", raw.headers.get("value")) client.header.param_enum("valid", GreyscaleColors.grey) + client.header.param_enum("valid", 'GREY') client.header.param_enum("null", None) raw = client.header.response_enum("valid", raw=True) diff --git a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/string_tests.py b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/string_tests.py index 93c4d937535cc..4236d75b66ed3 100644 --- a/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/string_tests.py +++ b/AutoRest/Generators/Python/Python.Tests/AcceptanceTests/string_tests.py @@ -44,7 +44,7 @@ sys.path.append(join(tests, "BodyString")) from msrest.serialization import Deserializer -from msrest.exceptions import DeserializationError +from msrest.exceptions import DeserializationError, SerializationError from autorestswaggerbatservice import AutoRestSwaggerBATService from autorestswaggerbatservice.models.auto_rest_swagger_bat_service_enums import * @@ -102,7 +102,10 @@ def test_string(self): self.assertIsNone(client.string.get_not_provided()) self.assertEqual(Colors.redcolor, client.enum.get_not_expandable()) + client.enum.put_not_expandable('red color') client.enum.put_not_expandable(Colors.redcolor) + with self.assertRaises(SerializationError): + client.enum.put_not_expandable('not a colour') self.assertEqual(client.string.get_base64_encoded(), 'a string that gets encoded with base64'.encode()) self.assertEqual(client.string.get_base64_url_encoded(), 'a string that gets encoded with base64url'.encode()) diff --git a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyArray/autorestswaggerbatarrayservice/operations/array.py b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyArray/autorestswaggerbatarrayservice/operations/array.py index cf60a316ab363..51e2f30f7330f 100644 --- a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyArray/autorestswaggerbatarrayservice/operations/array.py +++ b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyArray/autorestswaggerbatarrayservice/operations/array.py @@ -2064,7 +2064,7 @@ def get_base64_url( deserialized response :param operation_config: :ref:`Operation configuration overrides`. - :rtype: list of str + :rtype: list of bytes :rtype: :class:`ClientRawResponse` if raw=true """ diff --git a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyComplex/autorestcomplextestservice/models/basic.py b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyComplex/autorestcomplextestservice/models/basic.py index 83a9721456ac7..69a638c4f89c8 100644 --- a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyComplex/autorestcomplextestservice/models/basic.py +++ b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyComplex/autorestcomplextestservice/models/basic.py @@ -21,13 +21,14 @@ class Basic(Model): :type name: str :param color: Possible values include: 'cyan', 'Magenta', 'YELLOW', 'blacK' - :type color: str + :type color: str or :class:`CMYKColors + ` """ _attribute_map = { 'id': {'key': 'id', 'type': 'int'}, 'name': {'key': 'name', 'type': 'str'}, - 'color': {'key': 'color', 'type': 'CMYKColors'}, + 'color': {'key': 'color', 'type': 'str'}, } def __init__(self, id=None, name=None, color=None): diff --git a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyString/autorestswaggerbatservice/operations/enum.py b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyString/autorestswaggerbatservice/operations/enum.py index 3036a22fa9a19..18169ce12b9c3 100644 --- a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyString/autorestswaggerbatservice/operations/enum.py +++ b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyString/autorestswaggerbatservice/operations/enum.py @@ -42,7 +42,7 @@ def get_not_expandable( deserialized response :param operation_config: :ref:`Operation configuration overrides`. - :rtype: str + :rtype: :class:`Colors ` :rtype: :class:`ClientRawResponse` if raw=true """ @@ -84,7 +84,8 @@ def put_not_expandable( :param string_body: Possible values include: 'red color', 'green-color', 'blue_color' - :type string_body: str + :type string_body: str or :class:`Colors + ` :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response diff --git a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyString/autorestswaggerbatservice/operations/string.py b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyString/autorestswaggerbatservice/operations/string.py index 854f52c97752b..c6a765bba296a 100644 --- a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyString/autorestswaggerbatservice/operations/string.py +++ b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/BodyString/autorestswaggerbatservice/operations/string.py @@ -447,7 +447,7 @@ def get_base64_encoded( deserialized response :param operation_config: :ref:`Operation configuration overrides`. - :rtype: str + :rtype: bytes :rtype: :class:`ClientRawResponse` if raw=true """ @@ -491,7 +491,7 @@ def get_base64_url_encoded( deserialized response :param operation_config: :ref:`Operation configuration overrides`. - :rtype: str + :rtype: bytes :rtype: :class:`ClientRawResponse` if raw=true """ @@ -531,7 +531,7 @@ def put_base64_url_encoded( Put value that is base64url encoded :param string_body: - :type string_body: str + :type string_body: bytes :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response @@ -578,7 +578,7 @@ def get_null_base64_url_encoded( deserialized response :param operation_config: :ref:`Operation configuration overrides`. - :rtype: str + :rtype: bytes :rtype: :class:`ClientRawResponse` if raw=true """ diff --git a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Header/autorestswaggerbatheaderservice/operations/header.py b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Header/autorestswaggerbatheaderservice/operations/header.py index 55f4a77cbee80..609425c074ab7 100644 --- a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Header/autorestswaggerbatheaderservice/operations/header.py +++ b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Header/autorestswaggerbatheaderservice/operations/header.py @@ -1196,7 +1196,8 @@ def param_enum( :type scenario: str :param value: Send a post request with header values 'GREY' . Possible values include: 'White', 'black', 'GREY' - :type value: str + :type value: str or :class:`GreyscaleColors + ` :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response diff --git a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Url/autoresturltestservice/operations/paths.py b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Url/autoresturltestservice/operations/paths.py index c0e56ad997211..9084ae4dc9647 100644 --- a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Url/autoresturltestservice/operations/paths.py +++ b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Url/autoresturltestservice/operations/paths.py @@ -641,7 +641,8 @@ def enum_valid( :param enum_path: send the value green. Possible values include: 'red color', 'green color', 'blue color' - :type enum_path: str + :type enum_path: str or :class:`UriColor + ` :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response @@ -686,7 +687,8 @@ def enum_null( :param enum_path: send null should throw. Possible values include: 'red color', 'green color', 'blue color' - :type enum_path: str + :type enum_path: str or :class:`UriColor + ` :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response @@ -1033,7 +1035,7 @@ def base64_url( Get 'lorem' encoded value as 'bG9yZW0' (base64url) :param base64_url_path: base64url encoded value - :type base64_url_path: str + :type base64_url_path: bytes :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response diff --git a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Url/autoresturltestservice/operations/queries.py b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Url/autoresturltestservice/operations/queries.py index 4503b7fc304ec..b0e4ea1955430 100644 --- a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Url/autoresturltestservice/operations/queries.py +++ b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Url/autoresturltestservice/operations/queries.py @@ -805,7 +805,8 @@ def enum_valid( :param enum_query: 'green color' enum value. Possible values include: 'red color', 'green color', 'blue color' - :type enum_query: str + :type enum_query: str or :class:`UriColor + ` :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response @@ -847,7 +848,8 @@ def enum_null( :param enum_query: null string value. Possible values include: 'red color', 'green color', 'blue color' - :type enum_query: str + :type enum_query: str or :class:`UriColor + ` :param dict custom_headers: headers that will be added to the request :param bool raw: returns the direct response alongside the deserialized response diff --git a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Validation/autorestvalidationtest/models/product.py b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Validation/autorestvalidationtest/models/product.py index 65d49c5d12e17..a8e00da0b0808 100644 --- a/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Validation/autorestvalidationtest/models/product.py +++ b/AutoRest/Generators/Python/Python.Tests/Expected/AcceptanceTests/Validation/autorestvalidationtest/models/product.py @@ -39,7 +39,8 @@ class Product(Model): :vartype const_string: str :param const_string_as_enum: Constant string as Enum. Possible values include: 'constant_string_as_enum' - :type const_string_as_enum: str + :type const_string_as_enum: str or :class:`EnumConst + ` """ _validation = { diff --git a/AutoRest/Generators/Python/Python/ClientModelExtensions.cs b/AutoRest/Generators/Python/Python/ClientModelExtensions.cs index 00622b4d9e1a3..131b0ef8a7941 100644 --- a/AutoRest/Generators/Python/Python/ClientModelExtensions.cs +++ b/AutoRest/Generators/Python/Python/ClientModelExtensions.cs @@ -159,6 +159,12 @@ public static string ToPythonRuntimeTypeString(this IType type) } } + var enumType = type as EnumType; + if (enumType != null && enumType.ModelAsString) + { + return "str"; + } + var sequenceType = type as SequenceType; if (sequenceType != null) { @@ -307,6 +313,12 @@ public static string GetPythonSerializationType(IType type) return "{" + innerTypeName + "}"; } + EnumType enumType = type as EnumType; + if (enumType != null && enumType.ModelAsString) + { + return "str"; + } + // CompositeType or EnumType return type.Name; } diff --git a/AutoRest/Generators/Python/Python/PythonCodeNamer.cs b/AutoRest/Generators/Python/Python/PythonCodeNamer.cs index bcc2eceecf754..53cbd4e2d938f 100644 --- a/AutoRest/Generators/Python/Python/PythonCodeNamer.cs +++ b/AutoRest/Generators/Python/Python/PythonCodeNamer.cs @@ -254,7 +254,7 @@ public override IType NormalizeTypeReference(IType type) return null; } var enumType = type as EnumType; - if (enumType != null && enumType.Name.Length == 0 && enumType.ModelAsString) + if (enumType != null && enumType.Name.Length == 0) { type = new PrimaryType(KnownPrimaryType.String) { @@ -301,6 +301,7 @@ private IType NormalizeEnumType(EnumType enumType) { enumType.Values[i].Name = GetEnumMemberName(enumType.Values[i].Name); } + return enumType; } @@ -326,7 +327,7 @@ private static IType NormalizePrimaryType(PrimaryType primaryType) if (primaryType.Type == KnownPrimaryType.Base64Url) { - primaryType.Name = "str"; + primaryType.Name = "bytes"; } else if (primaryType.Type == KnownPrimaryType.Boolean) { diff --git a/AutoRest/Generators/Python/Python/TemplateModels/MethodTemplateModel.cs b/AutoRest/Generators/Python/Python/TemplateModels/MethodTemplateModel.cs index ee71dc5b08b4d..3ce192fdbb416 100644 --- a/AutoRest/Generators/Python/Python/TemplateModels/MethodTemplateModel.cs +++ b/AutoRest/Generators/Python/Python/TemplateModels/MethodTemplateModel.cs @@ -436,7 +436,8 @@ protected void AddHeaderDictionary(IndentedStringBuilder builder, CompositeType foreach (var prop in headersType.Properties) { - if (this.ServiceClient.EnumTypes.Contains(prop.Type)) + var enumType = prop.Type as EnumType; + if (this.ServiceClient.EnumTypes.Contains(prop.Type) && !enumType.ModelAsString) { builder.AppendLine(String.Format(CultureInfo.InvariantCulture, "'{0}': models.{1},", prop.SerializedName, prop.Type.ToPythonRuntimeTypeString())); } @@ -555,9 +556,10 @@ public string GetDocumentationType(IType type) } string result = "object"; - + var modelNamespace = ServiceClient.Name.ToPythonCase().Replace("_", ""); var primaryType = type as PrimaryType; var listType = type as SequenceType; + var enumType = type as EnumType; if (primaryType != null) { if (primaryType.Type == KnownPrimaryType.Stream) @@ -575,7 +577,15 @@ public string GetDocumentationType(IType type) } else if (type is EnumType) { - result = "str"; + if (type == ReturnType.Body) + { + if (enumType.ModelAsString) + result = "str"; + else + result = string.Format(CultureInfo.InvariantCulture, ":class:`{0} <{1}.models.{0}>`", type.Name, modelNamespace); + } + else + result = string.Format(CultureInfo.InvariantCulture, "str or :class:`{0} <{1}.models.{0}>`", type.Name, modelNamespace); } else if (type is DictionaryType) { @@ -583,7 +593,6 @@ public string GetDocumentationType(IType type) } else if (type is CompositeType) { - var modelNamespace = ServiceClient.Name.ToPythonCase().Replace("_", ""); if (!ServiceClient.Namespace.IsNullOrEmpty()) modelNamespace = ServiceClient.Namespace.ToPythonCase().Replace("_", ""); result = string.Format(CultureInfo.InvariantCulture, ":class:`{0} <{1}.models.{0}>`", type.Name, modelNamespace); diff --git a/AutoRest/Generators/Python/Python/TemplateModels/ModelTemplateModel.cs b/AutoRest/Generators/Python/Python/TemplateModels/ModelTemplateModel.cs index 2c43e7c092ca7..8ae4061b940b5 100644 --- a/AutoRest/Generators/Python/Python/TemplateModels/ModelTemplateModel.cs +++ b/AutoRest/Generators/Python/Python/TemplateModels/ModelTemplateModel.cs @@ -465,7 +465,7 @@ public string GetPropertyDocumentationType(IType type) } string result = "object"; - + var modelNamespace = ServiceClient.Name.ToPythonCase().Replace("_", ""); var listType = type as SequenceType; if (type is PrimaryType) { @@ -477,7 +477,7 @@ public string GetPropertyDocumentationType(IType type) } else if (type is EnumType) { - result = "str"; + result = string.Format(CultureInfo.InvariantCulture, "str or :class:`{0} <{1}.models.{0}>`", type.Name, modelNamespace); } else if (type is DictionaryType) { @@ -485,7 +485,6 @@ public string GetPropertyDocumentationType(IType type) } else if (type is CompositeType) { - var modelNamespace = ServiceClient.Name.ToPythonCase().Replace("_", ""); if (!ServiceClient.Namespace.IsNullOrEmpty()) modelNamespace = ServiceClient.Namespace.ToPythonCase().Replace("_", ""); result = string.Format(CultureInfo.InvariantCulture, ":class:`{0} <{1}.models.{0}>`", type.Name, modelNamespace); diff --git a/ClientRuntimes/Python/msrest/msrest/serialization.py b/ClientRuntimes/Python/msrest/msrest/serialization.py index b2c7e67ca57ac..c26596c95659d 100644 --- a/ClientRuntimes/Python/msrest/msrest/serialization.py +++ b/ClientRuntimes/Python/msrest/msrest/serialization.py @@ -154,6 +154,8 @@ def _convert_to_datatype(data, data_type, localtypes): key: _convert_to_datatype( data[key], data_type[1:-1], localtypes) for key in data } + elif issubclass(data_obj, Enum): + return data elif not isinstance(data, data_obj): result = { key: _convert_to_datatype( @@ -414,8 +416,9 @@ def serialize_data(self, data, data_type, **kwargs): elif data_type in self.serialize_type: return self.serialize_type[data_type](data, **kwargs) - elif isinstance(data, Enum): - return data.value + enum_type = self.dependencies.get(data_type) + if enum_type and issubclass(enum_type, Enum): + return self.serialize_enum(data, enum_obj=enum_type, **kwargs) iter_type = data_type[0] + data_type[-1] if iter_type in self.serialize_type: @@ -537,6 +540,21 @@ def serialize_object(self, attr, **kwargs): else: return str(attr) + def serialize_enum(self, attr, enum_obj=None, **kwagrs): + try: + return attr.value + except AttributeError: + pass + try: + enum_obj(attr) + return attr + except ValueError: + for enum_value in enum_obj: + if enum_value.value.lower() == str(attr).lower(): + return enum_value.value + error = "{!r} is not valid value for enum {!r}" + raise SerializationError(error.format(attr, enum_obj)) + @staticmethod def serialize_bytearray(attr, **kwargs): """Serialize bytearray into base-64 string. @@ -710,7 +728,7 @@ def __call__(self, target_obj, response_data): if isinstance(response, basestring): return self.deserialize_data(data, response) - elif isinstance(response, Enum) or class_name == 'EnumMeta': + elif issubclass(response, Enum): return self.deserialize_enum(data, response) if data is None: