Exemple #1
0
        private ParameterBase GetParameter(TypeFormat typeFormat, ParameterInfo parameter,
                                           SwaggerWcfParameterAttribute settings, string uriTemplate,
                                           IList <Type> definitionsTypesList)
        {
            string description = settings != null ? settings.Description : null;
            bool   required    = settings != null && settings.Required;
            string name        = parameter.Name;
            DataMemberAttribute dataMemberAttribute = parameter.GetCustomAttribute <DataMemberAttribute>();

            if (dataMemberAttribute != null && !string.IsNullOrEmpty(dataMemberAttribute.Name))
            {
                name = dataMemberAttribute.Name;
            }

            InType inType = GetInType(uriTemplate, parameter.Name);

            if (inType == InType.Path)
            {
                required = true;
            }

            if (!required && !parameter.HasDefaultValue)
            {
                required = true;
            }

            Type paramType = settings == null || settings.ParameterType == null
                ? parameter.ParameterType
                : settings.ParameterType;

            if (paramType.IsGenericType && paramType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                required  = false;
                paramType = paramType.GenericTypeArguments[0];
            }

            if (typeFormat.Type == ParameterType.Object)
            {
                return(new ParameterSchema
                {
                    Name = name,
                    Description = description,
                    In = inType,
                    Required = required,
                    SchemaRef = typeFormat.Format
                });
            }

            if (inType == InType.Body)
            {
                if (typeFormat.Type == ParameterType.Array)
                {
                    Type t = paramType.GetElementType() ?? GetEnumerableType(paramType);
                    ParameterPrimitive arrayParam = new ParameterPrimitive
                    {
                        Name        = name,
                        Description = description,
                        In          = inType,
                        Required    = required,
                        TypeFormat  = typeFormat,
                        Items       = new ParameterItems
                        {
                            Items = new ParameterSchema
                            {
                                SchemaRef = t.FullName
                            }
                        },
                        CollectionFormat = CollectionFormat.Csv
                    };

                    //it's a complex type, so we'll need to map it later
                    if (definitionsTypesList != null && !definitionsTypesList.Contains(t))
                    {
                        definitionsTypesList.Add(t);
                    }

                    return(arrayParam);
                }
                if (typeFormat.IsPrimitiveType)
                {
                    ParameterPrimitive paramPrimitive = new ParameterPrimitive
                    {
                        Name        = name,
                        Description = description,
                        In          = inType,
                        Required    = required,
                        TypeFormat  = typeFormat
                    };
                    return(paramPrimitive);
                }

                //it's a complex type, so we'll need to map it later
                if (definitionsTypesList != null && !definitionsTypesList.Contains(paramType))
                {
                    definitionsTypesList.Add(paramType);
                }
                typeFormat = new TypeFormat(ParameterType.Object,
                                            HttpUtility.HtmlEncode(paramType.FullName));

                return(new ParameterSchema
                {
                    Name = name,
                    Description = description,
                    In = inType,
                    Required = required,
                    SchemaRef = typeFormat.Format
                });
            }

            ParameterPrimitive param = new ParameterPrimitive
            {
                Name        = name,
                Description = description,
                In          = inType,
                Required    = required,
                TypeFormat  = typeFormat
            };

            return(param);
        }
Exemple #2
0
        private ParameterBase GetParameter(TypeFormat typeFormat,
                                           MethodInfo declaration,
                                           MethodInfo implementation,
                                           ParameterInfo parameter,
                                           SwaggerWcfParameterAttribute settings,
                                           string uriTemplate,
                                           bool wrappedRequest,
                                           IList <Type> definitionsTypesList,
                                           InType inType)
        {
            string description = settings?.Description;
            bool   required    = settings != null && settings.Required;
            string name        = inType == InType.Query ? ResolveParameterNameFromUri(uriTemplate, parameter) : parameter.Name;

            if (inType == InType.Path)
            {
                required = true;
            }

            if (!required && !parameter.HasDefaultValue)
            {
                required = true;
            }

            Type paramType = settings == null || settings.ParameterType == null
                ? parameter.ParameterType
                : settings.ParameterType;

            if (paramType.IsGenericType && paramType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                required  = false;
                paramType = paramType.GenericTypeArguments[0];
            }

            if (typeFormat.Type == ParameterType.Object)
            {
                return(new ParameterSchema
                {
                    Name = name,
                    Description = description,
                    In = inType,
                    Required = required,
                    SchemaRef = typeFormat.Format
                });
            }

            if (inType == InType.Body)
            {
                if (typeFormat.Type == ParameterType.Array)
                {
                    Type       t             = paramType.GetElementType() ?? GetEnumerableType(paramType);
                    TypeFormat subTypeFormat = Helpers.MapSwaggerType(t);

                    ParameterItems items;

                    if (subTypeFormat.IsPrimitiveType || subTypeFormat.IsEnum)
                    {
                        items = new ParameterItems
                        {
                            TypeFormat = subTypeFormat
                        };
                    }
                    else
                    {
                        items = new ParameterItems
                        {
                            Items = new ParameterSchema
                            {
                                SchemaRef = t.GetModelName()
                            }
                        };
                    }

                    ParameterPrimitive arrayParam = new ParameterPrimitive
                    {
                        Name             = name,
                        Description      = description,
                        In               = inType,
                        Required         = required,
                        TypeFormat       = typeFormat,
                        Items            = items,
                        CollectionFormat = CollectionFormat.Csv
                    };

                    //it's a complex type, so we'll need to map it later
                    if (definitionsTypesList != null && !definitionsTypesList.Contains(t))
                    {
                        definitionsTypesList.Add(t);
                    }

                    return(arrayParam);
                }
                if (typeFormat.IsPrimitiveType || typeFormat.IsEnum)
                {
                    bool isGetRequest = implementation.GetCustomAttributes <WebGetAttribute>().Any() ||
                                        declaration.GetCustomAttributes <WebGetAttribute>().Any();
                    if (!isGetRequest)
                    {
                        WebInvokeAttribute webInvoke = implementation.GetCustomAttribute <WebInvokeAttribute>()
                                                       ?? declaration.GetCustomAttribute <WebInvokeAttribute>();
                        if (webInvoke != null && webInvoke.Method == "GET")
                        {
                            isGetRequest = true;
                        }
                    }
                    if (!wrappedRequest && isGetRequest)
                    {
                        ParameterPrimitive paramPrimitive = new ParameterPrimitive
                        {
                            Name        = name,
                            Description = description,
                            In          = InType.Query,
                            Required    = required,
                            TypeFormat  = typeFormat
                        };
                        return(paramPrimitive);
                    }
                    else
                    {
                        ParameterPrimitive paramPrimitive = new ParameterPrimitive
                        {
                            Name        = name,
                            Description = description,
                            In          = inType,
                            Required    = required,
                            TypeFormat  = typeFormat
                        };
                        return(paramPrimitive);
                    }
                }

                //it's a complex type, so we'll need to map it later
                if (definitionsTypesList != null && !definitionsTypesList.Contains(paramType))
                {
                    definitionsTypesList.Add(paramType);
                }

                typeFormat = new TypeFormat(ParameterType.Object,
                                            HttpUtility.HtmlEncode(paramType.GetModelName()));

                return(new ParameterSchema
                {
                    Name = name,
                    Description = description,
                    In = inType,
                    Required = required,
                    SchemaRef = typeFormat.Format
                });
            }

            ParameterPrimitive param = new ParameterPrimitive
            {
                Name        = name,
                Description = description,
                In          = inType,
                Required    = required,
                TypeFormat  = typeFormat
            };

            return(param);
        }
Exemple #3
0
        private ParameterBase GetParameter(TypeFormat typeFormat, ParameterInfo parameter,
                                           SwaggerWcfParameterAttribute settings, string uriTemplate,
                                           IList <Type> definitionsTypesList)
        {
            string description = settings != null ? settings.Description : null;
            bool   required    = settings != null && settings.Required;
            string name        = parameter.Name;

            //Check for set DataContractAttribute to set Name and - Should this check for, and set DataContractAttribute.Namespace as well? /GustafRG
            //DataContractAttribute was not accessible on parameter, but on parameter.ParameterType. /GustafRG
            DataContractAttribute dataContractAttribute = parameter.ParameterType.GetCustomAttribute <DataContractAttribute>();

            if (dataContractAttribute != null)
            {
                if (!string.IsNullOrEmpty(dataContractAttribute.Name))
                {
                    name = dataContractAttribute.Name;
                }
                else
                {
                    name = parameter.ParameterType.Name;
                }
            }

            //Removed this. DataMemberAttribute is not valid on Object, Only on Object Members. Also, it does not seem to be accessible on "parameter" /GustafRG
            //DataMemberAttribute dataMemberAttribute = parameter.ParameterType.GetCustomAttribute<DataMemberAttribute>();

            //         if (dataMemberAttribute != null && !string.IsNullOrEmpty(dataMemberAttribute.Name))
            //             name = dataMemberAttribute.Name;

            InType inType = GetInType(uriTemplate, parameter.Name);

            if (inType == InType.Path)
            {
                required = true;
            }

            if (!required && !parameter.HasDefaultValue)
            {
                required = true;
            }

            Type paramType = parameter.ParameterType;

            if (paramType.IsGenericType && paramType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                required  = false;
                paramType = paramType.GenericTypeArguments[0];
            }

            if (typeFormat.Type == ParameterType.Object)
            {
                return(new ParameterSchema
                {
                    Name = name,
                    Description = description,
                    In = inType,
                    Required = required,
                    SchemaRef = typeFormat.Format
                });
            }

            if (inType == InType.Body)
            {
                if (typeFormat.Type == ParameterType.Array)
                {
                    Type t = paramType.GetElementType() ?? GetEnumerableType(paramType);
                    ParameterPrimitive arrayParam = new ParameterPrimitive
                    {
                        Name        = name,
                        Description = description,
                        In          = inType,
                        Required    = required,
                        TypeFormat  = typeFormat,
                        Items       = new ParameterItems
                        {
                            Items = new ParameterSchema
                            {
                                SchemaRef = t.FullName
                            }
                        },
                        CollectionFormat = CollectionFormat.Csv
                    };

                    //it's a complex type, so we'll need to map it later
                    if (definitionsTypesList != null && !definitionsTypesList.Contains(t))
                    {
                        definitionsTypesList.Add(t);
                    }

                    return(arrayParam);
                }

                //it's a complex type, so we'll need to map it later
                if (definitionsTypesList != null && !definitionsTypesList.Contains(paramType))
                {
                    definitionsTypesList.Add(paramType);
                }
                typeFormat = new TypeFormat(ParameterType.Object,
                                            HttpUtility.HtmlEncode(paramType.FullName));

                return(new ParameterSchema
                {
                    Name = name,
                    Description = description,
                    In = inType,
                    Required = required,
                    SchemaRef = typeFormat.Format
                });
            }

            ParameterPrimitive param = new ParameterPrimitive
            {
                Name        = name,
                Description = description,
                In          = inType,
                Required    = required,
                TypeFormat  = typeFormat
            };

            return(param);
        }