Ejemplo n.º 1
0
 public ParameterDataType(ParameterDataType existingType, IEnumerable <ParameterDefinition> customMembers)
 {
     this.Type = existingType.Type;
     this.CollectionResourceType = existingType.CollectionResourceType;
     this.CustomTypeName         = existingType.CustomTypeName;
     if (null != customMembers)
     {
         this.CustomMembers = customMembers.ToList();
     }
 }
Ejemplo n.º 2
0
 public static ParameterDataType ChooseBest(ParameterDataType a, ParameterDataType b)
 {
     if (b.IsLessSpecificThan(a))
     {
         return(a);
     }
     else
     {
         return(b);
     }
 }
Ejemplo n.º 3
0
 public override bool Equals(object obj)
 {
     if (obj is ParameterDataType)
     {
         ParameterDataType other = (ParameterDataType)obj;
         return(this.Type == other.Type &&
                this.CollectionResourceType == other.CollectionResourceType &&
                this.CustomTypeName == other.CustomTypeName);
     }
     return(false);
 }
Ejemplo n.º 4
0
 static ParameterDataType()
 {
     String            = new ParameterDataType(SimpleDataType.String);
     GenericObject     = new ParameterDataType(SimpleDataType.Object);
     GenericCollection = new ParameterDataType(SimpleDataType.Object, true);
     Boolean           = new ParameterDataType(SimpleDataType.Boolean);
     Double            = new ParameterDataType(SimpleDataType.Double);
     Float             = new ParameterDataType(SimpleDataType.Float);
     Guid           = new ParameterDataType(SimpleDataType.Guid);
     Int64          = new ParameterDataType(SimpleDataType.Int64);
     Int32          = new ParameterDataType(SimpleDataType.Int32);
     DateTimeOffset = new ParameterDataType(SimpleDataType.DateTimeOffset);
 }
Ejemplo n.º 5
0
 static ParameterDataType()
 {
     String = new ParameterDataType(SimpleDataType.String);
     GenericObject = new ParameterDataType(SimpleDataType.Object);
     GenericCollection = new ParameterDataType(SimpleDataType.Object, true);
     Boolean = new ParameterDataType(SimpleDataType.Boolean);
     Double = new ParameterDataType(SimpleDataType.Double);
     Float = new ParameterDataType(SimpleDataType.Float);
     Guid = new ParameterDataType(SimpleDataType.Guid);
     Int64 = new ParameterDataType(SimpleDataType.Int64);
     Int32 = new ParameterDataType(SimpleDataType.Int32);
     DateTimeOffset = new ParameterDataType(SimpleDataType.DateTimeOffset);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns a new instance that represents a collection of a given type of object.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ParameterDataType CollectionOfType(ParameterDataType type)
        {
            if (type.IsCollection)
            {
                throw new ArgumentException("Cannot create a collection for a collection");
            }

            return(new ParameterDataType
            {
                Type = SimpleDataType.Collection,
                CustomTypeName = type.CustomTypeName,
                CollectionResourceType = type.Type
            });
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Returns true if the current ParameterDataType instance is less
        /// qualified than the type provided in the arguments.
        /// {complex type} > {simple type} > {string} > {object}
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal bool IsLessSpecificThan(ParameterDataType type)
        {
            /*
             *  For JSON, we basically have the following hierarchy of data types:
             *
             *  string
             *  -> GUID
             *  -> DateTimeOffset
             *  -> TimeSpan
             *
             *  int64
             *  ->int32
             *  ->int16
             *  ->boolean
             *
             *  float
             *  -> double
             *
             *  object
             *  -> complex type
             *
             *  stream
             *
             *  We should never allow something from one three to be considered less specific than something in a different tree.
             */

            if (this.IsCollection != type.IsCollection)
            {
                return(false);
            }

            if (this.Type == SimpleDataType.String &&
                (type.Type == SimpleDataType.Guid || type.Type == SimpleDataType.DateTimeOffset || type.Type == SimpleDataType.TimeSpan))
            {
                return(true);
            }
            else if (this.Type == SimpleDataType.Int64 && (type.Type == SimpleDataType.Int32 || type.Type == SimpleDataType.Int16 || type.Type == SimpleDataType.Boolean))
            {
                return(true);
            }
            else if (this.Type == SimpleDataType.Double && (type.Type == SimpleDataType.Float))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Helper method that converts a string value into a ParameterDataType instance
        /// </summary>
        /// <param name="value"></param>
        /// <param name="addErrorAction"></param>
        /// <returns></returns>
        public static ParameterDataType ParseParameterDataType(this string value, Action <ValidationError> addErrorAction = null, ParameterDataType defaultValue = null)
        {
            if (value == null)
            {
                return(null);
            }

            // Value could have markdown formatting in it, so we do some basic work to try and remove that if it exists
            if (value.IndexOf('[') != -1)
            {
                value = value.TextBetweenCharacters('[', ']');
            }

            var            lowerValue = value.ToLowerInvariant();
            SimpleDataType simpleType = ParseSimpleTypeString(lowerValue);

            if (simpleType != SimpleDataType.None)
            {
                return(new ParameterDataType(simpleType));
            }

            const string collectionPrefix = "collection(";

            if (lowerValue.StartsWith(collectionPrefix))
            {
                string innerType = value.Substring(collectionPrefix.Length).TrimEnd(')');
                simpleType = ParseSimpleTypeString(innerType.ToLowerInvariant());

                if (simpleType != SimpleDataType.None)
                {
                    return(new ParameterDataType(simpleType, true));
                }
                else
                {
                    return(new ParameterDataType(innerType, true));
                }
            }

            if (lowerValue.Contains("etag"))
            {
                return(ParameterDataType.String);
            }
            if (lowerValue.Contains("timestamp"))
            {
                return(ParameterDataType.DateTimeOffset);
            }
            if (lowerValue.Contains("string"))
            {
                return(ParameterDataType.String);
            }


            if (defaultValue != null)
            {
                return(defaultValue);
            }

            if (null != addErrorAction)
            {
                addErrorAction(new ValidationWarning(ValidationErrorCode.TypeConversionFailure, "Couldn't convert '{0}' into understood data type. Assuming Object type.", value));
            }
            return(new ParameterDataType(value, false));
        }
 private static object SwaggerPropertyForResponse(MethodDefinition definition)
 {
     string customDataType = definition.ExpectedResponseMetadata.ResourceType;
     var type = new ParameterDataType(customDataType, true);
     string description = null;
     return MakeSwaggerProperty(type, description);
 }
 private static object MakeSwaggerProperty(ParameterDataType type, string description)
 {
     Dictionary<string, object> definition = null;
     if (type.IsObject)
     {
         definition = new Dictionary<string, object> {
             { "$ref", "#/definitions/" + type.CustomTypeName.SwaggerResourceName() }
         };
     }
     else
     {
         definition = new Dictionary<string, object>
         {
             { "type", type.ToSwaggerTypeString() }
         };
     }
     if (!string.IsNullOrEmpty(description))
     {
         definition.Add("description", description);
     }
     return definition;
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Merge values from the param object into this object.
        /// </summary>
        /// <param name="param"></param>
        internal void AddMissingDetails(ParameterDefinition param)
        {
            if (!this.Required.HasValue && param.Required.HasValue)
                this.Required = param.Required;

            this.IsNavigatable = this.IsNavigatable | param.IsNavigatable;

            if (this.Type != param.Type)
            {
                if (this.Type.IsObject)
                {
                    if (this.Type.CustomTypeName == null && param.Type.CustomTypeName != null)
                    {
                        this.Type = param.Type;
                    }
                }
                else if (this.Type.IsCollection)
                {
                    if (this.Type.CollectionResourceType == SimpleDataType.Object && this.Type.CustomTypeName == null &&
                    param.Type.CollectionResourceType == SimpleDataType.Object && param.Type.CustomTypeName != null)
                    {
                        this.Type = param.Type;
                    }
                }
                else if (this.Type.IsLessSpecificThan(param.Type))
                {
                    // TODO: This should probably be logged out as documentation warnings!
                    System.Diagnostics.Debug.WriteLine(
                        "Parameter '{2}' type changed, {0} --> {1}",
                        this.Type.Type,
                        param.Type.Type,
                        param.Name);
                    this.Type = param.Type;
                }
            }

            if (string.IsNullOrEmpty(this.Title))
                this.Title = param.Title;
            if (string.IsNullOrEmpty(this.Description))
                this.Description = param.Description;

            if (param.EnumeratedValues != null)
                this.EnumeratedValues.AddRange(param.EnumeratedValues);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Returns true if the current ParameterDataType instance is less
        /// qualified than the type provided in the arguments.
        /// {complex type} > {simple type} > {string} > {object}
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal bool IsLessSpecificThan(ParameterDataType type)
        {
            /*
                For JSON, we basically have the following hierarchy of data types:

                string
                -> GUID
                -> DateTimeOffset
                -> TimeSpan

                int64
                ->int32
                ->int16
                ->boolean

                float
                -> double

                object
                -> complex type

                stream

                We should never allow something from one three to be considered less specific than something in a different tree.
            */

            if (this.IsCollection != type.IsCollection)
                return false;

            if (this.Type == SimpleDataType.String &&
                (type.Type == SimpleDataType.Guid || type.Type == SimpleDataType.DateTimeOffset || type.Type == SimpleDataType.TimeSpan))
            {
                return true;
            }
            else if (this.Type == SimpleDataType.Int64 && (type.Type == SimpleDataType.Int32 || type.Type == SimpleDataType.Int16 || type.Type == SimpleDataType.Boolean))
            {
                return true;
            }
            else if (this.Type == SimpleDataType.Double && (type.Type == SimpleDataType.Float))
            {
                return true;
            }

            return false;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Returns a new instance that represents a collection of a given type of object.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ParameterDataType CollectionOfType(ParameterDataType type)
        {
            if (type.IsCollection)
            {
                throw new ArgumentException("Cannot create a collection for a collection");
            }

            return new ParameterDataType
            {
                Type = SimpleDataType.Collection,
                CustomTypeName = type.CustomTypeName,
                CollectionResourceType = type.Type
            };
        }
Ejemplo n.º 14
0
 public static ParameterDataType ChooseBest(ParameterDataType a, ParameterDataType b)
 {
     if (b.IsLessSpecificThan(a))
         return a;
     else
         return b;
 }
Ejemplo n.º 15
0
 public ParameterDataType(ParameterDataType existingType, IEnumerable<ParameterDefinition> customMembers)
 {
     this.Type = existingType.Type;
     this.CollectionResourceType = existingType.CollectionResourceType;
     this.CustomTypeName = existingType.CustomTypeName;
     if (null != customMembers)
     {
         this.CustomMembers = customMembers.ToList();
     }
 }