private static Location GetBinding(ParameterInfo parameter, string httpPath, JSchema schema)
        {
            var attributes = parameter.GetCustomAttributes().ToDictionary(a => a.GetType().Name);

            if (attributes.ContainsKey(nameof(FromQueryAttribute)))
            {
                return(Location.Query);
            }
            if (attributes.ContainsKey(nameof(FromBodyAttribute)))
            {
                return(Location.Body);
            }
            if (attributes.ContainsKey(nameof(FromFormAttribute)))
            {
                return(Location.FormData);
            }
            if (attributes.ContainsKey(nameof(FromHeaderAttribute)))
            {
                return(Location.Header);
            }
            var paramRegex = new Regex(@"\{" + parameter.Name + @"[:\?\}]");

            if (attributes.ContainsKey(nameof(FromRouteAttribute)) || paramRegex.IsMatch(httpPath))
            {
                return(Location.Path);
            }

            return(SchemaGenerator.IsComplex(schema) ? Location.Body : Location.Query);
        }
        public WebApiGenerator(GeneratorSettings settings)
        {
            var xmlDocs = XmlDocGenerator.GetXmlDocs(settings.XmlDocPath);

            _scope = new Scope {
                Settings = settings, XmlDocs = xmlDocs
            };
            _schemaGenerator = SchemaGenerator.Create(_scope, settings.JsonSchemaLicense);
        }
        internal static Parameter CreateParameter(ParameterInfo parameter, string httpPath, SchemaGenerator schemaGenerator, XmlDoc doc)
        {
            var name  = parameter.Name;
            var param = new Parameter {
                Name = name
            };
            var jSchema = schemaGenerator.GetSchema(parameter.ParameterType);

            param.In          = GetBinding(parameter, httpPath, jSchema);
            param.Description = doc != null && doc.Parameters.ContainsKey(name)
                ? doc.Parameters[name]
                : string.Empty;
            param.Required = SchemaGenerator.IsParameterRequired(parameter);
            if (param.In == Location.Body)
            {
                param.Schema = schemaGenerator.MapToSchema(jSchema);
            }
            else
            {
                var item = schemaGenerator.MapToItem(jSchema);
                param.Map(item);
            }
            return(param);
        }
Beispiel #4
0
 internal static PathItemGenerator Create(string httpPath, SchemaGenerator schemaGenerator, Scope scope)
 {
     return(new PathItemGenerator {
         _httpPath = httpPath, _schemaGenerator = schemaGenerator, _scope = scope
     });
 }
Beispiel #5
0
        private IEnumerable <Tuple <string, Response> > GetResponses(MethodInfo method, Dictionary <string, List <Attribute> > methodAttr, XmlDoc doc)
        {
            Func <Type, bool> isVoid = type => type == null || type.FullName == "System.Void";
            var returnType           = method.ReturnType;

            if (returnType == typeof(Task))
            {
                returnType = typeof(void);
            }
            else if (returnType.Name == "Task`1")
            {
                returnType = returnType.GenericTypeArguments[0];
            }
            else if (returnType == typeof(HttpResponseMessage) || returnType == typeof(ActionResult))
            {
                returnType = typeof(object);
            }

            var description = doc?.Returns ?? string.Empty;

            var          mayBeNull             = !SchemaGenerator.IsParameterRequired(method.ReturnParameter);
            const string responsetypeattribute = nameof(ResponseTypeAttribute);

            if (methodAttr.ContainsKey(responsetypeattribute))
            {
                var responseTypeAttributes = methodAttr[responsetypeattribute].Cast <ResponseTypeAttribute>();
                foreach (var responseTypeAttribute in responseTypeAttributes)
                {
                    returnType = responseTypeAttribute.ResponseType;
                    var httpStatusCode = responseTypeAttribute.HttpStatusCode ??
                                         (isVoid(returnType) ? "204" : "200");

                    yield return(Tuple.Create(httpStatusCode, new Response
                    {
                        Description = isVoid(returnType) ? "No Content" : description,
                        Schema = _schemaGenerator.MapToSchema(_schemaGenerator.GetSchema(returnType))
                    }));
                }
            }
            else
            {
                yield return(isVoid(returnType)
                    ? Tuple.Create("204", new Response()
                {
                    Description = "No Content."
                })
                    : Tuple.Create("200", new Response
                {
                    Description = description,
                    Schema = _schemaGenerator
                             .MapToSchema(_schemaGenerator.GetSchema(returnType))
                }));
            }
            yield return
                (Tuple.Create("default",
                              new Response()
            {
                Description = "Unexected Error",
                Schema = new SchemaObject()
                {
                    Ref = "#/definitions/ErrorModel"
                }
            }));
        }
Beispiel #6
0
 internal static PathItemGenerator Create(string route, SchemaGenerator schemaGenerator, Scope scope)
 {
     return(new PathItemGenerator {
         _route = route, _schemaGenerator = schemaGenerator, _scope = scope
     });
 }