Skip to content

Commit

Permalink
Support for x-ms-enum ModelAsString
Browse files Browse the repository at this point in the history
  • Loading branch information
annatisch committed May 17, 2016
1 parent 1d37145 commit 4104a56
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
<storagemanagementclient.models.Reason>`
:param message: Gets an error message explaining the Reason value in more
detail.
:type message: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<storagemanagementclient.models.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
<storagemanagementclient.models.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.
Expand All @@ -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
<storagemanagementclient.models.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
Expand All @@ -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
<storagemanagementclient.models.AccountStatus>`
:param creation_time: Gets the creation date and time of the storage
account in UTC.
:type creation_time: datetime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<storagemanagementclient.models.AccountType>`
"""

_validation = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<storagemanagementclient.models.KeyName>`
"""

_attribute_map = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<storagemanagementclient.models.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<storagemanagementclient.models.UsageUnit>`
:param current_value: Gets the current count of the allocated resources
in the subscription.
:type current_value: int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<storagemanagementclient.models.KeyName>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,7 @@ def get_base64_url(
deserialized response
:param operation_config: :ref:`Operation configuration
overrides<msrest:optionsforoperations>`.
:rtype: list of str
:rtype: list of bytes
:rtype: :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
if raw=true
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<autorestcomplextestservice.models.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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_not_expandable(
deserialized response
:param operation_config: :ref:`Operation configuration
overrides<msrest:optionsforoperations>`.
:rtype: str
:rtype: :class:`Colors <autorestswaggerbatservice.models.Colors>`
:rtype: :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
if raw=true
"""
Expand Down Expand Up @@ -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
<autorestswaggerbatservice.models.Colors>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def get_base64_encoded(
deserialized response
:param operation_config: :ref:`Operation configuration
overrides<msrest:optionsforoperations>`.
:rtype: str
:rtype: bytes
:rtype: :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
if raw=true
"""
Expand Down Expand Up @@ -491,7 +491,7 @@ def get_base64_url_encoded(
deserialized response
:param operation_config: :ref:`Operation configuration
overrides<msrest:optionsforoperations>`.
:rtype: str
:rtype: bytes
:rtype: :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
if raw=true
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -578,7 +578,7 @@ def get_null_base64_url_encoded(
deserialized response
:param operation_config: :ref:`Operation configuration
overrides<msrest:optionsforoperations>`.
:rtype: str
:rtype: bytes
:rtype: :class:`ClientRawResponse<msrest.pipeline.ClientRawResponse>`
if raw=true
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<autorestswaggerbatheaderservice.models.GreyscaleColors>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<autoresturltestservice.models.UriColor>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down Expand Up @@ -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
<autoresturltestservice.models.UriColor>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<autoresturltestservice.models.UriColor>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down Expand Up @@ -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
<autoresturltestservice.models.UriColor>`
:param dict custom_headers: headers that will be added to the request
:param bool raw: returns the direct response alongside the
deserialized response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
<autorestvalidationtest.models.EnumConst>`
"""

_validation = {
Expand Down
12 changes: 12 additions & 0 deletions AutoRest/Generators/Python/Python/ClientModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions AutoRest/Generators/Python/Python/PythonCodeNamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -301,6 +301,7 @@ private IType NormalizeEnumType(EnumType enumType)
{
enumType.Values[i].Name = GetEnumMemberName(enumType.Values[i].Name);
}

return enumType;
}

Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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)
Expand All @@ -575,15 +577,22 @@ 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)
{
result = "dict";
}
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);
Expand Down
Loading

0 comments on commit 4104a56

Please sign in to comment.