diff --git a/AutoRest/AutoRest.Core/Utilities/Extensions.cs b/AutoRest/AutoRest.Core/Utilities/Extensions.cs index 23bea5173d9db..9a18e21f1f5b4 100644 --- a/AutoRest/AutoRest.Core/Utilities/Extensions.cs +++ b/AutoRest/AutoRest.Core/Utilities/Extensions.cs @@ -244,7 +244,7 @@ public static string EscapeXmlComment(this string comment) } /// - /// Returns true is the type is a PrimaryType with KnownPrimaryType matching typeToMatch. + /// Returns true if the type is a PrimaryType with KnownPrimaryType matching typeToMatch. /// /// /// @@ -263,5 +263,54 @@ public static bool IsPrimaryType(this IType type, KnownPrimaryType typeToMatch) } return false; } + + /// + /// Returns true if the is a PrimaryType with KnownPrimaryType matching + /// or a DictionaryType with ValueType matching or a SequenceType matching + /// + /// + /// + /// + public static bool IsOrContainsPrimaryType(this IType type, KnownPrimaryType typeToMatch) + { + if (type == null) + { + return false; + } + + if (type.IsPrimaryType(typeToMatch) || + type.IsDictionaryContainingType(typeToMatch) || + type.IsSequenceContainingType(typeToMatch)) + { + return true; + } + return false; + } + + /// + /// Returns true if the is a DictionaryType with ValueType matching + /// + /// + /// + /// + public static bool IsDictionaryContainingType(this IType type, KnownPrimaryType typeToMatch) + { + DictionaryType dictionaryType = type as DictionaryType; + PrimaryType dictionaryPrimaryType = dictionaryType?.ValueType as PrimaryType; + return dictionaryPrimaryType != null && dictionaryPrimaryType.IsPrimaryType(typeToMatch); + } + + /// + /// Returns true if the is a SequenceType matching + /// + /// + /// + /// + public static bool IsSequenceContainingType(this IType type, KnownPrimaryType typeToMatch) + { + SequenceType sequenceType = type as SequenceType; + PrimaryType sequencePrimaryType = sequenceType?.ElementType as PrimaryType; + return sequencePrimaryType != null && sequencePrimaryType.IsPrimaryType(typeToMatch); + } } } \ No newline at end of file diff --git a/AutoRest/Generators/CSharp/CSharp/TemplateModels/MethodTemplateModel.cs b/AutoRest/Generators/CSharp/CSharp/TemplateModels/MethodTemplateModel.cs index d4a380df2765c..22d1ca54614d0 100644 --- a/AutoRest/Generators/CSharp/CSharp/TemplateModels/MethodTemplateModel.cs +++ b/AutoRest/Generators/CSharp/CSharp/TemplateModels/MethodTemplateModel.cs @@ -325,37 +325,19 @@ public string ClientReference /// public string GetSerializationSettingsReference(IType serializationType) { - SequenceType sequenceType = serializationType as SequenceType; - DictionaryType dictionaryType = serializationType as DictionaryType; - if (serializationType.IsPrimaryType(KnownPrimaryType.Date) || - (sequenceType != null && sequenceType.ElementType is PrimaryType - && ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Date) || - (dictionaryType != null && dictionaryType.ValueType is PrimaryType - && ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Date)) + if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.Date)) { return "new DateJsonConverter()"; } - else if (serializationType.IsPrimaryType(KnownPrimaryType.DateTimeRfc1123) || - (sequenceType != null && sequenceType.ElementType is PrimaryType - && ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.DateTimeRfc1123) || - (dictionaryType != null && dictionaryType.ValueType is PrimaryType - && ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.DateTimeRfc1123)) + else if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.DateTimeRfc1123)) { return "new DateTimeRfc1123JsonConverter()"; } - else if (serializationType.IsPrimaryType(KnownPrimaryType.Base64Url) || - (sequenceType != null && sequenceType.ElementType is PrimaryType - && ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Base64Url) || - (dictionaryType != null && dictionaryType.ValueType is PrimaryType - && ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Base64Url)) + else if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.Base64Url)) { return "new Base64UrlJsonConverter()"; } - else if (serializationType.IsPrimaryType(KnownPrimaryType.UnixTime) || - (sequenceType != null && sequenceType.ElementType is PrimaryType - && ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.UnixTime) || - (dictionaryType != null && dictionaryType.ValueType is PrimaryType - && ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.UnixTime)) + else if (serializationType.IsOrContainsPrimaryType(KnownPrimaryType.UnixTime)) { return "new UnixTimeJsonConverter()"; } @@ -369,33 +351,18 @@ public string GetSerializationSettingsReference(IType serializationType) /// public string GetDeserializationSettingsReference(IType deserializationType) { - SequenceType sequenceType = deserializationType as SequenceType; - DictionaryType dictionaryType = deserializationType as DictionaryType; - if (deserializationType.IsPrimaryType(KnownPrimaryType.Date) || - (sequenceType != null && sequenceType.ElementType is PrimaryType - && ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Date) || - (dictionaryType != null && dictionaryType.ValueType is PrimaryType - && ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Date)) + if (deserializationType.IsOrContainsPrimaryType(KnownPrimaryType.Date)) { return "new DateJsonConverter()"; } - else if (deserializationType.IsPrimaryType(KnownPrimaryType.Base64Url) || - (sequenceType != null && sequenceType.ElementType is PrimaryType - && ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Base64Url) || - (dictionaryType != null && dictionaryType.ValueType is PrimaryType - && ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Base64Url)) + else if (deserializationType.IsOrContainsPrimaryType(KnownPrimaryType.Base64Url)) { return "new Base64UrlJsonConverter()"; } - else if (deserializationType.IsPrimaryType(KnownPrimaryType.UnixTime) || - (sequenceType != null && sequenceType.ElementType is PrimaryType - && ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.UnixTime) || - (dictionaryType != null && dictionaryType.ValueType is PrimaryType - && ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.UnixTime)) + else if (deserializationType.IsOrContainsPrimaryType(KnownPrimaryType.UnixTime)) { return "new UnixTimeJsonConverter()"; } - return ClientReference + ".DeserializationSettings"; }