/// <summary>
        /// Format the value of a sequence given the modeled element format.  Note that only sequences of strings are supported
        /// </summary>
        /// <param name="parameter">The parameter to format</param>
        /// <returns>return the separator</returns>
        public static string NeedsFormattedSeparator(Parameter parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            SequenceType sequence = parameter.Type as SequenceType;
            if (sequence == null)
            {
                return null;
            }

            PrimaryType primaryType = sequence.ElementType as PrimaryType;
            EnumType enumType = sequence.ElementType as EnumType;
            if (enumType != null)
            {
                primaryType = new PrimaryType(KnownPrimaryType.String)
                {
                    Name = "str"
                };
            }

            if (primaryType != null && primaryType.Type != KnownPrimaryType.String)
            {
                throw new InvalidOperationException(
                    string.Format(CultureInfo.InvariantCulture,
                    "Cannot generate a formatted sequence from a " +
                                  "non-string array parameter {0}", parameter));
            }

            return parameter.CollectionFormat.GetSeparator();
        }
        /// <summary>
        /// Format the value of a sequence given the modeled element format.  Note that only sequences of strings are supported
        /// </summary>
        /// <param name="parameter">The parameter to format</param>
        /// <returns>A reference to the formatted parameter value</returns>
        public static string GetFormattedReferenceValue(this Parameter parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            SequenceType sequence = parameter.Type as SequenceType;
            if (sequence == null)
            {
                return parameter.Type.ToString(parameter.Name);
            }

            PrimaryType primaryType = sequence.ElementType as PrimaryType;
            EnumType enumType = sequence.ElementType as EnumType;
            if (enumType != null && enumType.ModelAsString)
            {
                primaryType = new PrimaryType(KnownPrimaryType.String);
            }

            if (primaryType == null || primaryType.Type != KnownPrimaryType.String)
            {
                throw new InvalidOperationException(
                    string.Format(CultureInfo.InvariantCulture,
                    "Cannot generate a formatted sequence from a " +
                                  "non-string array parameter {0}", parameter));
            }

            return string.Format(CultureInfo.InvariantCulture,
                "{0}.join('{1}')", parameter.Name, parameter.CollectionFormat.GetSeparator());
        }
 /// <summary>
 /// Initializes static members of PrimaryType class.
 /// </summary>
 static PrimaryType()
 {
     KnownTypes = new Dictionary<SupportedPrimaryType, PrimaryType>();
     foreach (SupportedPrimaryType knownType in System.Enum.GetValues(typeof (SupportedPrimaryType)))
     {
         KnownTypes[knownType] = new PrimaryType {Name = knownType.ToString(), Type = knownType};
     }
 }
Exemple #4
0
 public static void Reset()
 {
     KnownTypes = new Dictionary<SupportedPrimaryType, PrimaryType>();
     foreach (SupportedPrimaryType knownType in System.Enum.GetValues(typeof (SupportedPrimaryType)))
     {
         var name = System.Enum.GetName(typeof (SupportedPrimaryType), knownType);
         KnownTypes[knownType] = new PrimaryType {Name = name, Type = knownType};
     }
 }
        public PrimaryTypeModel(PrimaryType primaryType)
            : base (primaryType != null ? primaryType.Type : KnownPrimaryType.None)
        {
            if (primaryType == null)
            {
                throw new ArgumentNullException("primaryType");
            }

            this.LoadFrom(primaryType);
            _imports = new List<string>();
            Initialize(primaryType);
        }
 protected override IType NormalizePrimaryType(PrimaryType primaryType)
 {
     if (primaryType != null && primaryType == PrimaryType.Credentials)
     {
         primaryType.Name = "ServiceClientCredentials";
         return primaryType;
     }
     else
     {
         return base.NormalizePrimaryType(primaryType);
     }
 }
        /// <summary>
        /// Normalizes primary type.
        /// </summary>
        /// <param name="primaryType">Primary type to normalize.</param>
        /// <returns>Normalized primary type.</returns>
        private IType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == PrimaryType.Boolean)
            {
                primaryType.Name = "Boolean";
            }
            else if (primaryType == PrimaryType.ByteArray)
            {
                primaryType.Name = "Array";
            }
            else if (primaryType == PrimaryType.DateTime)
            {
                primaryType.Name = "DateTime";
            }
            else if (primaryType == PrimaryType.DateTimeRfc1123)
            {
                primaryType.Name = "DateTime";
            }
            else if (primaryType == PrimaryType.Double)
            {
                primaryType.Name = "Float";
            }
            else if (primaryType == PrimaryType.Decimal)
            {
                primaryType.Name = "Float";
            }
            else if (primaryType == PrimaryType.Int)
            {
                primaryType.Name = "Number";
            }
            else if (primaryType == PrimaryType.Long)
            {
                primaryType.Name = "Bignum";
            }
            else if (primaryType == PrimaryType.Stream)
            {
                // TODO: Ruby doesn't supports streams.
                primaryType.Name = "System.IO.Stream";
            }
            else if (primaryType == PrimaryType.String)
            {
                primaryType.Name = "String";
            }
            else if (primaryType == PrimaryType.TimeSpan)
            {
                primaryType.Name = "Duration";
            }
            else if (primaryType == PrimaryType.Object)
            {
                primaryType.Name = "Object";
            }

            return primaryType;
        }
 private void Initialize(PrimaryType primaryType)
 {
     if (primaryType.Type == KnownPrimaryType.None)
     {
         Name = "void";
     }
     else if (primaryType.Type == KnownPrimaryType.Base64Url)
     {
         Name = "String";
     }
     else if (primaryType.Type == KnownPrimaryType.Boolean)
     {
         Name = "boolean";
     }
     else if (primaryType.Type == KnownPrimaryType.ByteArray)
     {
         Name = "byte[]";
     }
     else if (primaryType.Type == KnownPrimaryType.Date)
     {
         Name = "LocalDate";
         _imports.Add("org.joda.time.LocalDate");
     }
     else if (primaryType.Type == KnownPrimaryType.DateTime)
     {
         Name = "DateTime";
         _imports.Add("org.joda.time.DateTime");
     }
     else if (primaryType.Type == KnownPrimaryType.DateTimeRfc1123)
     {
         Name = "DateTimeRfc1123";
         _imports.Add("com.microsoft.rest.DateTimeRfc1123");
     }
     else if (primaryType.Type == KnownPrimaryType.Double)
     {
         Name = "double";
     }
     else if (primaryType.Type == KnownPrimaryType.Decimal)
     {
         Name = "BigDecimal";
         _imports.Add("java.math.BigDecimal");
     }
     else if (primaryType.Type == KnownPrimaryType.Int)
     {
         Name = "int";
     }
     else if (primaryType.Type == KnownPrimaryType.Long)
     {
         Name = "long";
     }
     else if (primaryType.Type == KnownPrimaryType.Stream)
     {
         Name = "InputStream";
         _imports.Add("java.io.InputStream");
     }
     else if (primaryType.Type == KnownPrimaryType.String)
     {
         Name = "String";
     }
     else if (primaryType.Type == KnownPrimaryType.TimeSpan)
     {
         Name = "Period";
         _imports.Add("org.joda.time.Period");
     }
     else if (primaryType.Type == KnownPrimaryType.Uuid)
     {
         Name = "UUID";
         _imports.Add("java.util.UUID");
     }
     else if (primaryType.Type == KnownPrimaryType.Object)
     {
         Name = "Object";
     }
     else if (primaryType.Type == KnownPrimaryType.Credentials)
     {
         Name = "ServiceClientCredentials";
         _imports.Add("com.microsoft.rest.ServiceClientCredentials");
     }
 }
        public override IType NormalizeTypeReference(IType type)
        {
            if (type == null)
            {
                return null;
            }
            var enumType = type as EnumType;
            if (enumType != null && enumType.Name.Length == 0 && enumType.ModelAsString)
            {
                type = new PrimaryType(KnownPrimaryType.String)
                {
                    Name = "str"
                };
            }

            // Using Any instead of Contains since object hash is bound to a property which is modified during normalization
            if (_normalizedTypes.Any(item => type.Equals(item)))
            {
                return _normalizedTypes.First(item => type.Equals(item));
            }

            _normalizedTypes.Add(type);
            if (type is PrimaryType)
            {
                return NormalizePrimaryType(type as PrimaryType);
            }
            if (type is SequenceType)
            {
                return NormalizeSequenceType(type as SequenceType);
            }
            if (type is DictionaryType)
            {
                return NormalizeDictionaryType(type as DictionaryType);
            }
            if (type is CompositeType)
            {
                return NormalizeCompositeType(type as CompositeType);
            }
            if (type is EnumType)
            {
                return NormalizeEnumType(type as EnumType);
            }


            throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
                "Type {0} is not supported.", type.GetType()));
        }
        private static IType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == PrimaryType.Boolean)
            {
                primaryType.Name = "bool?";
            }
            else if (primaryType == PrimaryType.ByteArray)
            {
                primaryType.Name = "byte[]";
            }
            else if (primaryType == PrimaryType.Date)
            {
                primaryType.Name = "DateTime?";
            }
            else if (primaryType == PrimaryType.DateTime)
            {
                primaryType.Name = "DateTime?";
            }
            else if (primaryType == PrimaryType.Double)
            {
                primaryType.Name = "double?";
            }
            else if (primaryType == PrimaryType.Int)
            {
                primaryType.Name = "int?";
            }
            else if (primaryType == PrimaryType.Long)
            {
                primaryType.Name = "long?";
            }
            else if (primaryType == PrimaryType.Stream)
            {
                primaryType.Name = "System.IO.Stream";
            }
            else if (primaryType == PrimaryType.String)
            {
                primaryType.Name = "string";
            }
            else if (primaryType == PrimaryType.TimeSpan)
            {
                primaryType.Name = "TimeSpan?";
            }
            else if (primaryType == PrimaryType.Object)
            {
                primaryType.Name = "object";
            }

            return primaryType;
        }
        /// <summary>
        /// Normalizes primary type.
        /// </summary>
        /// <param name="primaryType">Primary type to normalize.</param>
        /// <returns>Normalized primary type.</returns>
        private IType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == null)
            {
                throw new ArgumentNullException("primaryType");
            }

            if (primaryType.Type == KnownPrimaryType.Base64Url)
            {
                primaryType.Name = "String";
            }
            else if (primaryType.Type == KnownPrimaryType.Boolean)
            {
                primaryType.Name = "Boolean";
            }
            else if (primaryType.Type == KnownPrimaryType.ByteArray)
            {
                primaryType.Name = "Array";
            }
            else if (primaryType.Type == KnownPrimaryType.DateTime)
            {
                primaryType.Name = "DateTime";
            }
            else if (primaryType.Type == KnownPrimaryType.DateTimeRfc1123)
            {
                primaryType.Name = "DateTime";
            }
            else if (primaryType.Type == KnownPrimaryType.Double)
            {
                primaryType.Name = "Float";
            }
            else if (primaryType.Type == KnownPrimaryType.Decimal)
            {
                primaryType.Name = "Float";
            }
            else if (primaryType.Type == KnownPrimaryType.Int)
            {
                primaryType.Name = "Number";
            }
            else if (primaryType.Type == KnownPrimaryType.Long)
            {
                primaryType.Name = "Bignum";
            }
            else if (primaryType.Type == KnownPrimaryType.Stream)
            {
                // TODO: Ruby doesn't supports streams.
                primaryType.Name = "System.IO.Stream";
            }
            else if (primaryType.Type == KnownPrimaryType.String)
            {
                primaryType.Name = "String";
            }
            else if (primaryType.Type == KnownPrimaryType.TimeSpan)
            {
                primaryType.Name = "Duration";
            }
            else if (primaryType.Type == KnownPrimaryType.UnixTime)
            {
                primaryType.Name = "DateTime";
            }
            else if (primaryType.Type == KnownPrimaryType.Object)
            {
                primaryType.Name = "Object";
            }

            return primaryType;
        }
        public static string GetJavaType(PrimaryType primaryType)
        {
            if (primaryType == null)
            {
                return null;
            }

            if (primaryType == PrimaryType.Date ||
                primaryType.Name == "LocalDate")
            {
                return "org.joda.time.LocalDate";
            }
            else if (primaryType == PrimaryType.DateTime ||
                primaryType.Name == "DateTime")
            {
                return "org.joda.time.DateTime";
            }
            else if (primaryType == PrimaryType.Decimal ||
                primaryType.Name == "Decimal")
            {
                return "java.math.BigDecimal";
            }
            else if (primaryType == PrimaryType.DateTimeRfc1123 ||
               primaryType.Name == "DateTimeRfc1123")
            {
                return "com.microsoft.rest.DateTimeRfc1123";
            }
            else if (primaryType == PrimaryType.Stream ||
                primaryType.Name == "InputStream")
            {
                return "java.io.InputStream";
            }
            else if (primaryType == PrimaryType.TimeSpan ||
                primaryType.Name == "Period")
            {
                return "org.joda.time.Period";
            }
            else
            {
                return null;
            }
        }
        private static JsonSchema ParsePrimaryType(PrimaryType primaryType)
        {
            JsonSchema result = new JsonSchema();

            switch (primaryType.Type)
            {
                case KnownPrimaryType.Boolean:
                    result.JsonType = "boolean";
                    break;

                case KnownPrimaryType.Int:
                case KnownPrimaryType.Long:
                    result.JsonType = "integer";
                    break;

                case KnownPrimaryType.Double:
                    result.JsonType = "number";
                    break;

                case KnownPrimaryType.Object:
                    result.JsonType = "object";
                    break;

                case KnownPrimaryType.DateTime:
                case KnownPrimaryType.String:
                    result.JsonType = "string";
                    break;

                default:
                    Debug.Assert(false, "Unrecognized known property type: " + primaryType.Type);
                    break;
            }

            return result;
        }
        private static IType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == null)
            {
                throw new ArgumentNullException("primaryType");
            }

            if (primaryType.Type == KnownPrimaryType.Base64Url)
            {
                primaryType.Name = "Buffer";
            }
            else if (primaryType.Type == KnownPrimaryType.Boolean)
            {
                primaryType.Name = "Boolean";
            }
            else if (primaryType.Type == KnownPrimaryType.ByteArray)
            {
                primaryType.Name = "Buffer";
            }
            else if (primaryType.Type == KnownPrimaryType.Date)
            {
                primaryType.Name = "Date";
            }
            else if (primaryType.Type == KnownPrimaryType.DateTime)
            {
                primaryType.Name = "Date";
            }
            else if (primaryType.Type == KnownPrimaryType.DateTimeRfc1123)
            {
                primaryType.Name = "Date";
            }
            else if (primaryType.Type == KnownPrimaryType.UnixTime)
            {
                primaryType.Name = "Date";
            }
            else if (primaryType.Type == KnownPrimaryType.Double)
            {
                primaryType.Name = "Number";
            }
            else if (primaryType.Type == KnownPrimaryType.Decimal)
            {
                primaryType.Name = "Number";
            }
            else if (primaryType.Type == KnownPrimaryType.Int)
            {
                primaryType.Name = "Number";
            }
            else if (primaryType.Type == KnownPrimaryType.Long)
            {
                primaryType.Name = "Number";
            }
            else if (primaryType.Type == KnownPrimaryType.Stream)
            {
                primaryType.Name = "Object";
            }
            else if (primaryType.Type == KnownPrimaryType.String)
            {
                primaryType.Name = "String";
            }
            else if (primaryType.Type == KnownPrimaryType.TimeSpan)
            {
                primaryType.Name = "moment.duration"; 
            }
            else if (primaryType.Type == KnownPrimaryType.Uuid)
            {
                primaryType.Name = "Uuid";
            }
            else if (primaryType.Type == KnownPrimaryType.Object)
            {
                primaryType.Name = "Object";
            }

            return primaryType;
        }
Exemple #15
0
        public static String ImportedFrom(PrimaryType primaryType)
        {
            if (primaryType == null)
            {
                return null;
            }

            if (primaryType == PrimaryType.Date ||
                primaryType.Name == "LocalDate")
            {
                return "org.joda.time.LocalDate";
            }
            else if (primaryType == PrimaryType.DateTime || 
                primaryType.Name == "DateTime")
            {
                return "org.joda.time.DateTime";
            }
            else if (primaryType == PrimaryType.Decimal ||
                primaryType.Name == "Decimal")
            {
                return "java.math.BigDecimal";
            }
            else if (primaryType == PrimaryType.DateTimeRfc1123 ||
               primaryType.Name == "DateTimeRfc1123")
            {
                return "com.microsoft.rest.DateTimeRfc1123";
            }
            else if (primaryType == PrimaryType.Stream ||
                primaryType.Name == "InputStream")
            {
                return "java.io.InputStream";
            }
            else if (primaryType == PrimaryType.ByteArray ||
                primaryType.Name == "ByteArray")
            {
                return "org.apache.commons.codec.binary.Base64";
            }
            else if (primaryType == PrimaryType.TimeSpan ||
                primaryType.Name == "Period")
            {
                return "org.joda.time.Period";
            }
            else
            {
                return null;
            }
        }
        private Response BuildMethodReturnType(List<Stack<IType>> types, IType headerType)
        {
            IType baseType = new PrimaryType(KnownPrimaryType.Object);
            // Return null if no response is specified
            if (types.Count == 0)
            {
                return new Response(null, headerType);
            }
            // Return first if only one return type
            if (types.Count == 1)
            {
                return new Response(types.First().Pop(), headerType);
            }

            // BuildParameter up type inheritance tree
            types.ForEach(typeStack =>
            {
                IType type = typeStack.Peek();
                while (!Equals(type, baseType))
                {
                    if (type is CompositeType && _swaggerModeler.ExtendedTypes.ContainsKey(type.Name))
                    {
                        type = _swaggerModeler.GeneratedTypes[_swaggerModeler.ExtendedTypes[type.Name]];
                    }
                    else
                    {
                        type = baseType;
                    }
                    typeStack.Push(type);
                }
            });

            // Eliminate commonly shared base classes
            while (!types.First().IsNullOrEmpty())
            {
                IType currentType = types.First().Peek();
                foreach (var typeStack in types)
                {
                    IType t = typeStack.Pop();
                    if (!Equals(t, currentType))
                    {
                        return new Response(baseType, headerType);
                    }
                }
                baseType = currentType;
            }

            return new Response(baseType, headerType);
        }
 public static IType WrapPrimitiveType(IType type)
 {
     if (type is PrimaryType)
     {
         var primaryType = new PrimaryType();
         if (type.Name == "boolean")
         {
             primaryType.Name = "Boolean";
         }
         else if (type.Name == "double")
         {
             primaryType.Name = "Double";
         }
         else if (type.Name == "int")
         {
             primaryType.Name = "Integer";
         }
         else if (type.Name == "long")
         {
             primaryType.Name = "Long";
         }
         else
         {
             return type;
         }
         return primaryType;
     }
     else if (type == null)
     {
         var newType = new PrimaryType();
         newType.Name = "Void";
         return newType;
     }
     else
     {
         return type;
     }
 }
        private static PrimaryType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == PrimaryType.Boolean)
            {
                primaryType.Name = "boolean";
            }
            else if (primaryType == PrimaryType.ByteArray)
            {
                primaryType.Name = "byte[]";
            }
            else if (primaryType == PrimaryType.Date)
            {
                primaryType.Name = "LocalDate";
            }
            else if (primaryType == PrimaryType.DateTime)
            {
                primaryType.Name = "DateTime";
            }
            else if (primaryType == PrimaryType.DateTimeRfc1123)
            {
                primaryType.Name = "DateTimeRfc1123";
            }
            else if (primaryType == PrimaryType.Double)
            {
                primaryType.Name = "double";
            }
            else if (primaryType == PrimaryType.Decimal)
            {
                primaryType.Name = "BigDecimal";
            }
            else if (primaryType == PrimaryType.Int)
            {
                primaryType.Name = "int";
            }
            else if (primaryType == PrimaryType.Long)
            {
                primaryType.Name = "long";
            }
            else if (primaryType == PrimaryType.Stream)
            {
                primaryType.Name = "InputStream";
            }
            else if (primaryType == PrimaryType.String)
            {
                primaryType.Name = "String";
            }
            else if (primaryType == PrimaryType.TimeSpan)
            {
                primaryType.Name = "Period";
            }
            else if (primaryType == PrimaryType.Object)
            {
                primaryType.Name = "Object";
            }
            else if (primaryType == PrimaryType.Credentials)
            {
                primaryType.Name = "ServiceClientCredentials";
            }

            return primaryType;
        }
Exemple #19
0
        public static PrimaryTypeModel NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == null)
            {
                throw new ArgumentNullException("primaryType");
            }

            return new PrimaryTypeModel(primaryType);
        }
        protected virtual IType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == null)
            {
                return null;
            }

            if (primaryType.Type == KnownPrimaryType.Base64Url)
            {
                primaryType.Name = "byte[]";
            }
            else if (primaryType.Type == KnownPrimaryType.Boolean)
            {
                primaryType.Name = "bool";
            }
            else if (primaryType.Type == KnownPrimaryType.ByteArray)
            {
                primaryType.Name = "byte[]";
            }
            else if (primaryType.Type == KnownPrimaryType.Date)
            {
                primaryType.Name = "DateTime";
            }
            else if (primaryType.Type == KnownPrimaryType.DateTime)
            {
                primaryType.Name = UseDateTimeOffset ? "DateTimeOffset" : "DateTime";
            }
            else if (primaryType.Type == KnownPrimaryType.DateTimeRfc1123)
            {
                primaryType.Name = "DateTime";
            }
            else if (primaryType.Type == KnownPrimaryType.Double)
            {
                primaryType.Name = "double";
            }
            else if (primaryType.Type == KnownPrimaryType.Decimal)
            {
                primaryType.Name = "decimal";
            }
            else if (primaryType.Type == KnownPrimaryType.Int)
            {
                primaryType.Name = "int";
            }
            else if (primaryType.Type == KnownPrimaryType.Long)
            {
                primaryType.Name = "long";
            }
            else if (primaryType.Type == KnownPrimaryType.Stream)
            {
                primaryType.Name = "System.IO.Stream";
            }
            else if (primaryType.Type == KnownPrimaryType.String)
            {
                primaryType.Name = "string";
            }
            else if (primaryType.Type == KnownPrimaryType.TimeSpan)
            {
                primaryType.Name = "TimeSpan";
            }
            else if (primaryType.Type == KnownPrimaryType.Object)
            {
                primaryType.Name = "object";
            }
            else if (primaryType.Type == KnownPrimaryType.Credentials)
            {
                primaryType.Name = "ServiceClientCredentials";
            }
            else if (primaryType.Type == KnownPrimaryType.UnixTime)
            {
                primaryType.Name = "DateTime";
            }
            else if (primaryType.Type == KnownPrimaryType.Uuid)
            {
                primaryType.Name = "Guid";
            }

            return primaryType;
        }
        /// <summary>
        /// If the element type of a sequenece or value type of a dictionary 
        /// contains one of the following special types then it needs to be 
        /// deserialized. The special types are: Date, DateTime, ByteArray 
        /// and CompositeType
        /// </summary>
        /// <param name="type">The type to determine if special deserialization is required</param>
        /// <returns>True if special deserialization is required. False, otherwise.</returns>
        private static bool IsSpecialDeserializationRequired(IType type)
        {
            PrimaryType[] validTypes = new PrimaryType[] { PrimaryType.DateTime, PrimaryType.Date, PrimaryType.ByteArray };
            SequenceType sequence = type as SequenceType;
            DictionaryType dictionary = type as DictionaryType;
            bool result = false;
            if (sequence != null &&
                (validTypes.Any(t => t == sequence.ElementType) || sequence.ElementType is CompositeType))
            {
                result = true;
            }
            else if (dictionary != null &&
                (validTypes.Any(t => t == dictionary.ValueType) || dictionary.ValueType is CompositeType))
            {
                result = true;
            }

            return result;
        }
        /// <summary>
        /// Constructs blueprint of the given <paramref name="type"/>.
        /// </summary>
        /// <param name="type">Type for which mapper being generated.</param>
        /// <param name="serializedName">Serialized name to be used.</param>
        /// <param name="parameter">Parameter of the composite type to construct the parameter constraints.</param>
        /// <param name="expandComposite">Expand composite type if <c>true</c> otherwise specify class_name in the mapper.</param>
        /// <returns>Mapper for the <paramref name="type"/> as string.</returns>
        /// <exception cref="ArgumentNullException">Thrown when a required parameter is null.</exception>
        /// <example>
        /// One of the example of the mapper is 
        /// {
        ///   required: false,
        ///   serialized_name: 'Fish',
        ///   type: {
        ///     name: 'Composite',
        ///     polymorphic_discriminator: 'fishtype',
        ///     uber_parent: 'Fish',
        ///     class_name: 'Fish',
        ///     model_properties: {
        ///       species: {
        ///         required: false,
        ///         serialized_name: 'species',
        ///         type: {
        ///           name: 'String'
        ///         }
        ///       },
        ///       length: {
        ///         required: true,
        ///         serialized_name: 'length',
        ///         type: {
        ///           name: 'Double'
        ///         }
        ///       },
        ///       siblings: {
        ///         required: false,
        ///         serialized_name: 'siblings',
        ///         type: {
        ///           name: 'Sequence',
        ///           element: {
        ///               required: false,
        ///               serialized_name: 'FishElementType',
        ///               type: {
        ///                 name: 'Composite',
        ///                 polymorphic_discriminator: 'fishtype',
        ///                 uber_parent: 'Fish',
        ///                 class_name: 'Fish'
        ///               }
        ///           }
        ///         }
        ///       }
        ///     }
        ///   }
        /// }
        /// </example>
        public static string ConstructMapper(this IType type, string serializedName, IParameter parameter, bool expandComposite)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var builder = new IndentedStringBuilder("  ");

            CompositeType composite = type as CompositeType;
            SequenceType sequence = type as SequenceType;
            DictionaryType dictionary = type as DictionaryType;
            PrimaryType primary = type as PrimaryType;
            EnumType enumType = type as EnumType;
            if (enumType != null && enumType.ModelAsString)
            {
                primary = new PrimaryType(KnownPrimaryType.String);
            }
            builder.AppendLine("").Indent();

            builder.AppendLine(type.AddMetaData(serializedName, parameter));

            if (primary != null)
            {
                builder.AppendLine(primary.ContructMapperForPrimaryType());
            }
            else if (enumType != null && enumType.Name != null)
            {
                builder.AppendLine(enumType.ContructMapperForEnumType());
            }
            else if (sequence != null)
            {
                builder.AppendLine(sequence.ContructMapperForSequenceType());
            }
            else if (dictionary != null)
            {
                builder.AppendLine(dictionary.ContructMapperForDictionaryType());
            }
            else if (composite != null)
            {
                builder.AppendLine(composite.ContructMapperForCompositeType(expandComposite));
            }
            else
            {
                throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "{0} is not a supported Type.", type));
            }
            return builder.ToString();
        }
        private static IType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == PrimaryType.Boolean)
            {
                primaryType.Name = "Boolean";
            }
            else if (primaryType == PrimaryType.ByteArray)
            {
                primaryType.Name = "Buffer";
            }
            else if (primaryType == PrimaryType.Date)
            {
                primaryType.Name = "Date";
            }
            else if (primaryType == PrimaryType.DateTime)
            {
                primaryType.Name = "Date";
            }
            else if (primaryType == PrimaryType.Double)
            {
                primaryType.Name = "Number";
            }
            else if (primaryType == PrimaryType.Int)
            {
                primaryType.Name = "Number";
            }
            else if (primaryType == PrimaryType.Long)
            {
                primaryType.Name = "Number";
            }
            else if (primaryType == PrimaryType.Stream)
            {
                primaryType.Name = "Object";
            }
            else if (primaryType == PrimaryType.String)
            {
                primaryType.Name = "String";
            }
            else if (primaryType == PrimaryType.TimeSpan)
            {
                primaryType.Name = "TimeSpan";
            }
            else if (primaryType == PrimaryType.Object)
            {
                primaryType.Name = "Object";
            }

            return primaryType;
        }
        private static IType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == PrimaryType.Boolean)
            {
                primaryType.Name = "bool";
            }
            else if (primaryType == PrimaryType.ByteArray)
            {
                primaryType.Name = "bytearray";
            }
            else if (primaryType == PrimaryType.Date)
            {
                primaryType.Name = "date";
            }
            else if (primaryType == PrimaryType.DateTime)
            {
                primaryType.Name = "datetime";
            }
            else if (primaryType == PrimaryType.DateTimeRfc1123)
            {
                primaryType.Name = "datetime";
            }
            else if (primaryType == PrimaryType.Double)
            {
                primaryType.Name = "float";
            }
            else if (primaryType == PrimaryType.Int)
            {
                primaryType.Name = "int";
            }
            else if (primaryType == PrimaryType.Long)
            {
                primaryType.Name = "long";
            }
            else if (primaryType == PrimaryType.Stream)  // Revisit here
            {
                primaryType.Name = "Object";
            }
            else if (primaryType == PrimaryType.String)
            {
                primaryType.Name = "str";
            }
            else if (primaryType == PrimaryType.TimeSpan)
            {
                primaryType.Name = "timedelta";
            }
            else if (primaryType == PrimaryType.Decimal)
            {
                primaryType.Name = "Decimal";
            }
            else if (primaryType == PrimaryType.Object)  // Revisit here
            {
                primaryType.Name = "object";
            }

            return primaryType;
        }
Exemple #25
0
        protected virtual IType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == null)
            {
                return null;
            }

            if (primaryType == PrimaryType.Boolean)
            {
                primaryType.Name = "bool?";
            }
            else if (primaryType == PrimaryType.ByteArray)
            {
                primaryType.Name = "byte[]";
            }
            else if (primaryType == PrimaryType.Date)
            {
                primaryType.Name = "DateTime?";
            }
            else if (primaryType == PrimaryType.DateTime)
            {
                primaryType.Name = "DateTime?";
            }
            else if (primaryType == PrimaryType.DateTimeRfc1123)
            {
                primaryType.Name = "DateTime?";
            }
            else if (primaryType == PrimaryType.Double)
            {
                primaryType.Name = "double?";
            }
            else if (primaryType == PrimaryType.Decimal)
            {
                primaryType.Name = "decimal?";
            }
            else if (primaryType == PrimaryType.Int)
            {
                primaryType.Name = "int?";
            }
            else if (primaryType == PrimaryType.Long)
            {
                primaryType.Name = "long?";
            }
            else if (primaryType == PrimaryType.Stream)
            {
                primaryType.Name = "System.IO.Stream";
            }
            else if (primaryType == PrimaryType.String)
            {
                primaryType.Name = "string";
            }
            else if (primaryType == PrimaryType.TimeSpan)
            {
                primaryType.Name = "TimeSpan?";
            }
            else if (primaryType == PrimaryType.Object)
            {
                primaryType.Name = "object";
            }
            else if (primaryType == PrimaryType.Credentials)
            {
                primaryType.Name = "ServiceClientCredentials";
            }

            return primaryType;
        }
        private static IType NormalizePrimaryType(PrimaryType primaryType)
        {
            if (primaryType == null)
            {
                throw new ArgumentNullException("primaryType");
            }

            if (primaryType.Type == KnownPrimaryType.Base64Url)
            {
                primaryType.Name = "str";
            }
            else if (primaryType.Type == KnownPrimaryType.Boolean)
            {
                primaryType.Name = "bool";
            }
            else if (primaryType.Type == KnownPrimaryType.ByteArray)
            {
                primaryType.Name = "bytearray";
            }
            else if (primaryType.Type == KnownPrimaryType.Date)
            {
                primaryType.Name = "date";
            }
            else if (primaryType.Type == KnownPrimaryType.DateTime)
            {
                primaryType.Name = "datetime";
            }
            else if (primaryType.Type == KnownPrimaryType.DateTimeRfc1123)
            {
                primaryType.Name = "datetime";
            }
            else if (primaryType.Type == KnownPrimaryType.Double)
            {
                primaryType.Name = "float";
            }
            else if (primaryType.Type == KnownPrimaryType.Int)
            {
                primaryType.Name = "int";
            }
            else if (primaryType.Type == KnownPrimaryType.Long)
            {
                primaryType.Name = "long";
            }
            else if (primaryType.Type == KnownPrimaryType.Stream)  // Revisit here
            {
                primaryType.Name = "Object";
            }
            else if (primaryType.Type == KnownPrimaryType.String || primaryType.Type == KnownPrimaryType.Uuid)
            {
                primaryType.Name = "str";
            }
            else if (primaryType.Type == KnownPrimaryType.TimeSpan)
            {
                primaryType.Name = "timedelta"; 
            }
            else if (primaryType.Type == KnownPrimaryType.Decimal)
            {
                primaryType.Name = "Decimal";
            }
            else if (primaryType.Type == KnownPrimaryType.UnixTime)
            {
                primaryType.Name = "long"; 
            }
            else if (primaryType.Type == KnownPrimaryType.Object)  // Revisit here
            {
                primaryType.Name = "object";
            }

            return primaryType;
        }
Exemple #27
0
        /// <summary>
        /// The visitor method for building service types. This is called when an instance of this class is
        /// visiting a _swaggerModeler to build a service type.
        /// </summary>
        /// <param name="serviceTypeName">name for the service type</param>
        /// <returns>built service type</returns>
        public virtual IType BuildServiceType(string serviceTypeName)
        {
            PrimaryType type = SwaggerObject.ToType();
            Debug.Assert(type != null);

            if (type.Type == KnownPrimaryType.Object && "file".Equals(SwaggerObject.Format, StringComparison.OrdinalIgnoreCase))
            {
                type = new PrimaryType(KnownPrimaryType.Stream);
            }
            type.Format = SwaggerObject.Format;
            if (SwaggerObject.Enum != null && type.Type == KnownPrimaryType.String && !(IsSwaggerObjectConstant(SwaggerObject)))
            {
                var enumType = new EnumType();
                SwaggerObject.Enum.ForEach(v => enumType.Values.Add(new EnumValue { Name = v, SerializedName = v }));
                if (SwaggerObject.Extensions.ContainsKey(CodeGenerator.EnumObject))
                {
                    var enumObject = SwaggerObject.Extensions[CodeGenerator.EnumObject] as Newtonsoft.Json.Linq.JContainer;
                    if (enumObject != null)
                    {
                        enumType.Name= enumObject["name"].ToString();
                        if (enumObject["modelAsString"] != null)
                        {
                            enumType.ModelAsString = bool.Parse(enumObject["modelAsString"].ToString());
                        }
                    }
                    enumType.SerializedName = enumType.Name;
                    if (string.IsNullOrEmpty(enumType.Name))
                    {
                        throw new InvalidOperationException(
                            string.Format(CultureInfo.InvariantCulture, 
                                "{0} extension needs to specify an enum name.", 
                                CodeGenerator.EnumObject));
                    }
                    var existingEnum =
                        Modeler.ServiceClient.EnumTypes.FirstOrDefault(
                            e => e.Name.Equals(enumType.Name, StringComparison.OrdinalIgnoreCase));
                    if (existingEnum != null)
                    {
                        if (!existingEnum.Equals(enumType))
                        {
                            throw new InvalidOperationException(
                                string.Format(CultureInfo.InvariantCulture,
                                    "Swagger document contains two or more {0} extensions with the same name '{1}' and different values.",
                                    CodeGenerator.EnumObject,
                                    enumType.Name));
                        }
                    }
                    else
                    {
                        Modeler.ServiceClient.EnumTypes.Add(enumType);
                    }
                }
                else
                {
                    enumType.ModelAsString = true;
                    enumType.Name = string.Empty;
                    enumType.SerializedName = string.Empty;
                }
                return enumType;
            }
            if (SwaggerObject.Type == DataType.Array)
            {
                string itemServiceTypeName;
                if (SwaggerObject.Items.Reference != null)
                {
                    itemServiceTypeName = SwaggerObject.Items.Reference.StripDefinitionPath();
                }
                else
                {
                    itemServiceTypeName = serviceTypeName + "Item";
                }

                var elementType =
                    SwaggerObject.Items.GetBuilder(Modeler).BuildServiceType(itemServiceTypeName);
                return new SequenceType
                {
                    ElementType = elementType
                };
            }
            if (SwaggerObject.AdditionalProperties != null)
            {
                string dictionaryValueServiceTypeName;
                if (SwaggerObject.AdditionalProperties.Reference != null)
                {
                    dictionaryValueServiceTypeName = SwaggerObject.AdditionalProperties.Reference.StripDefinitionPath();
                }
                else
                {
                    dictionaryValueServiceTypeName = serviceTypeName + "Value";
                }
                return new DictionaryType
                {
                    ValueType =
                        SwaggerObject.AdditionalProperties.GetBuilder(Modeler)
                            .BuildServiceType((dictionaryValueServiceTypeName))
                };
            }

            return type;
        }