Exemple #1
0
        private object GenerateInstance(string typePrefix, string name, HashSet <string> acceptableEmptyArgs, JsonDocument documentBody = null)
        {
            Type t = Type.GetType(typePrefix + name);

            if (t == null)
            {
                throw new HttpException(HttpStatusCode.BadRequest, String.Format("Requested type {0} is not not recognized.", typePrefix + name));
            }

            var arg_list = new List <Object> {
            };

            // we are deliberately assuming here that there will only be a single constructor
            var ctor      = t.GetConstructors()[0];
            var paramsSet = ctor.GetParameters();

            // walk across our constructor params. check inside the body for a resulting value for each of them
            foreach (var param in paramsSet)
            {
                if (documentBody != null && documentBody.RootElement.TryGetProperty(param.Name, out var jsonElement))
                {
                    if (DebugLogger.CheckLogLevel(LogLevel.Debug))
                    {
                        _logger.LogDebug("Request Body Content" + JsonSerializer.Serialize(documentBody.RootElement));
                    }

                    object argumentValue = null;
                    switch (jsonElement.ValueKind)
                    {
                    case JsonValueKind.Null:
                    case JsonValueKind.String:
                        argumentValue = jsonElement.GetString();
                        break;

                    case JsonValueKind.True:
                    case JsonValueKind.False:
                        argumentValue = jsonElement.GetBoolean();
                        break;

                    case JsonValueKind.Object:
                        try
                        {
                            argumentValue = Activator.CreateInstance(param.ParameterType, new List <object> {
                                jsonElement
                            }.ToArray());
                        }
                        catch (Exception e)
                        {
                            if (e.InnerException is HttpException)
                            {
                                throw e.InnerException;
                            }
                            else
                            {
                                throw;
                            }
                        }
                        break;

                    default:
                        throw new HttpException(HttpStatusCode.BadRequest, $"{jsonElement.ValueKind} parameters are not supported");
                    }

                    if (argumentValue == null || (argumentValue is string stringResult && string.IsNullOrEmpty(stringResult)))
                    {
                        if (!acceptableEmptyArgs.Contains(param.Name))
                        {
                            throw new HttpException(HttpStatusCode.BadRequest, $"Parameter {param.Name} was passed with no value. Please check the request body and try again.");
                        }
                    }

                    arg_list.Add((object)argumentValue);
                }
                else
                {
                    if (param.IsOptional)
                    {
                        arg_list.Add(param.DefaultValue);
                    }
                    else
                    {
                        throw new HttpException(HttpStatusCode.BadRequest, $"Required parameter key {param} was not found in the request body.");
                    }
                }
            }

            try
            {
                return(Activator.CreateInstance(t, arg_list.ToArray()));
            }
            catch (Exception e)
            {
                if (e.InnerException is HttpException)
                {
                    throw e.InnerException;
                }
                else
                {
                    throw;
                }
            }
        }