Пример #1
0
    public async Task ReadAsync_RegistersFileStreamForDisposal()
    {
        // Arrange
        var formatter = new NewtonsoftJsonInputFormatter(
            GetLogger(),
            _serializerSettings,
            ArrayPool <char> .Shared,
            _objectPoolProvider,
            new MvcOptions(),
            new MvcNewtonsoftJsonOptions());
        var         httpContext        = new Mock <HttpContext>();
        IDisposable registerForDispose = null;

        var content = Encoding.UTF8.GetBytes("\"Hello world\"");

        httpContext.Setup(h => h.Request.Body).Returns(new NonSeekableReadStream(content, allowSyncReads: false));
        httpContext.Setup(h => h.Request.ContentType).Returns("application/json");
        httpContext.Setup(h => h.Response.RegisterForDispose(It.IsAny <IDisposable>()))
        .Callback((IDisposable disposable) => registerForDispose = disposable)
        .Verifiable();

        var formatterContext = CreateInputFormatterContext(typeof(string), httpContext.Object);

        // Act
        var result = await formatter.ReadAsync(formatterContext);

        // Assert
        Assert.Equal("Hello world", result.Model);
        Assert.NotNull(registerForDispose);
        Assert.IsType <FileBufferingReadStream>(registerForDispose);
        httpContext.Verify();
    }
Пример #2
0
    public async Task ReadAsync_AllowInputFormatterExceptionMessages_DoesNotWrapJsonInputExceptions()
    {
        // Arrange
        var formatter = new NewtonsoftJsonInputFormatter(
            GetLogger(),
            _serializerSettings,
            ArrayPool <char> .Shared,
            _objectPoolProvider,
            new MvcOptions(),
            new MvcNewtonsoftJsonOptions()
        {
            AllowInputFormatterExceptionMessages = true,
        });

        var contentBytes = Encoding.UTF8.GetBytes("{");
        var httpContext  = GetHttpContext(contentBytes);

        var formatterContext = CreateInputFormatterContext(typeof(User), httpContext);

        // Act
        var result = await formatter.ReadAsync(formatterContext);

        // Assert
        Assert.True(result.HasError);
        Assert.True(!formatterContext.ModelState.IsValid);
        Assert.True(formatterContext.ModelState.ContainsKey(string.Empty));

        var modelError = formatterContext.ModelState[string.Empty].Errors.Single();

        Assert.Null(modelError.Exception);
        Assert.NotEmpty(modelError.ErrorMessage);
    }
Пример #3
0
    public async Task ReadAsync_DoesNotRethrowOverflowExceptions()
    {
        // Arrange
        _serializerSettings.Converters.Add(new IsoDateTimeConverter());

        var formatter = new NewtonsoftJsonInputFormatter(
            GetLogger(),
            _serializerSettings,
            ArrayPool <char> .Shared,
            _objectPoolProvider,
            new MvcOptions(),
            new MvcNewtonsoftJsonOptions());

        var contentBytes = Encoding.UTF8.GetBytes("{\"shortValue\":\"32768\"}");
        var httpContext  = GetHttpContext(contentBytes);

        var formatterContext = CreateInputFormatterContext(typeof(TypeWithPrimitives), httpContext);

        // Act
        var result = await formatter.ReadAsync(formatterContext);

        // Assert
        Assert.True(result.HasError);
        Assert.False(formatterContext.ModelState.IsValid);

        var modelError = Assert.Single(formatterContext.ModelState["shortValue"].Errors);

        Assert.Null(modelError.Exception);
        Assert.Equal("The supplied value is invalid.", modelError.ErrorMessage);
    }
Пример #4
0
        public static MvcOptions ConfigureJsonFormatter([NotNull] this MvcOptions thisValue, [NotNull] Action <JsonSerializerSettings> configureJson, [NotNull] ILogger logger)
        {
            thisValue.RespectBrowserAcceptHeader = true;

            MvcNewtonsoftJsonOptions mvcJsonOptions = new MvcNewtonsoftJsonOptions
            {
                AllowInputFormatterExceptionMessages = true
            };
            JsonSerializerSettings settings = mvcJsonOptions.SerializerSettings;

            configureJson(settings);

            NewtonsoftJsonInputFormatter input = thisValue.InputFormatters.OfType <NewtonsoftJsonInputFormatter>()
                                                 .FirstOrDefault();

            if (input == null)
            {
                input = new NewtonsoftJsonInputFormatter(logger, settings, ArrayPool <char> .Shared, new DefaultObjectPoolProvider(), thisValue, mvcJsonOptions);
                thisValue.InputFormatters.Add(input);
            }

            NewtonsoftJsonOutputFormatter output = thisValue.OutputFormatters.OfType <NewtonsoftJsonOutputFormatter>()
                                                   .FirstOrDefault();

            if (output == null)
            {
                output = new NewtonsoftJsonOutputFormatter(settings, ArrayPool <char> .Shared, thisValue);
                thisValue.OutputFormatters.Add(output);
            }

            thisValue.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValues.ApplicationJson);
            return(thisValue);
        }
Пример #5
0
    public async Task Constructor_BuffersRequestBody_UsingDefaultOptions()
    {
        // Arrange
        var formatter = new NewtonsoftJsonInputFormatter(
            GetLogger(),
            _serializerSettings,
            ArrayPool <char> .Shared,
            _objectPoolProvider,
            new MvcOptions(),
            new MvcNewtonsoftJsonOptions());

        var content      = "{name: 'Person Name', Age: '30'}";
        var contentBytes = Encoding.UTF8.GetBytes(content);
        var httpContext  = new DefaultHttpContext();

        httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature());
        httpContext.Request.Body        = new NonSeekableReadStream(contentBytes, allowSyncReads: false);
        httpContext.Request.ContentType = "application/json";

        var formatterContext = CreateInputFormatterContext(typeof(User), httpContext);

        // Act
        var result = await formatter.ReadAsync(formatterContext);

        // Assert
        Assert.False(result.HasError);

        var userModel = Assert.IsType <User>(result.Model);

        Assert.Equal("Person Name", userModel.Name);
        Assert.Equal(30, userModel.Age);
    }
Пример #6
0
        public NewtonsoftJsonModelBinder(
            IHttpRequestStreamReaderFactory readerFactory,
            ILoggerFactory loggerFactory,
            IOptions <MvcOptions> optionsProvider,
            IOptions <MvcNewtonsoftJsonOptions> newtonsoftOptionsProvider,
            ArrayPool <char> charPool,
            ObjectPoolProvider objectPoolProvider)
        {
            var options    = optionsProvider.Value;
            var formatters = options.InputFormatters.ToList();

            var jsonFormatter           = formatters.OfType <SystemTextJsonInputFormatter>().FirstOrDefault();
            var newtonsoftJsonFormatter = formatters.OfType <NewtonsoftJsonInputFormatter>().FirstOrDefault();

            if (jsonFormatter != null && newtonsoftJsonFormatter == null)
            {
                var jsonFormatterIndex = formatters.IndexOf(jsonFormatter);
                var logger             = loggerFactory.CreateLogger <NewtonsoftJsonInputFormatter>();
                var settings           = JsonSerializerSettingsProvider.CreateSerializerSettings();

                formatters[jsonFormatterIndex] = new NewtonsoftJsonInputFormatter(
                    logger, settings, charPool, objectPoolProvider, options, newtonsoftOptionsProvider.Value);
            }

            _bodyBinder = new BodyModelBinder(formatters, readerFactory, loggerFactory, options);
        }
Пример #7
0
    public AbpHybridJsonInputFormatter(SystemTextJsonInputFormatter systemTextJsonInputFormatter, NewtonsoftJsonInputFormatter newtonsoftJsonInputFormatter)
    {
        _systemTextJsonInputFormatter = systemTextJsonInputFormatter;
        _newtonsoftJsonInputFormatter = newtonsoftJsonInputFormatter;

        SupportedEncodings.Add(UTF8EncodingWithoutBOM);
        SupportedEncodings.Add(UTF16EncodingLittleEndian);

        SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationJson);
        SupportedMediaTypes.Add(MediaTypeHeaderValues.TextJson);
        SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationAnyJsonSyntax);
    }
Пример #8
0
    public async Task ReadAsync_WithReadJsonWithRequestCulture_DeserializesUsingRequestCulture(
        string dateString,
        string culture)
    {
        // Arrange
        var formatter = new NewtonsoftJsonInputFormatter(
            GetLogger(),
            _serializerSettings,
            ArrayPool <char> .Shared,
            _objectPoolProvider,
            new MvcOptions(),
            new MvcNewtonsoftJsonOptions()
        {
            ReadJsonWithRequestCulture = true
        });

        var originalCulture = CultureInfo.CurrentCulture;

        CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);

        try
        {
            var content      = $"{{'DateValue': '{dateString}'}}";
            var contentBytes = Encoding.UTF8.GetBytes(content);
            var httpContext  = new DefaultHttpContext();
            httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature());
            httpContext.Request.Body        = new NonSeekableReadStream(contentBytes, allowSyncReads: false);
            httpContext.Request.ContentType = "application/json";

            var formatterContext = CreateInputFormatterContext(typeof(TypeWithPrimitives), httpContext);

            // Act
            var result = await formatter.ReadAsync(formatterContext);

            // Assert
            Assert.False(result.HasError);

            var userModel = Assert.IsType <TypeWithPrimitives>(result.Model);
            Assert.Equal(new DateTime(2019, 05, 15, 00, 00, 00, DateTimeKind.Unspecified), userModel.DateValue);
        }
        finally
        {
            CultureInfo.CurrentCulture = originalCulture;
        }
    }
Пример #9
0
    public async Task Version_2_1_Constructor_SuppressInputFormatterBufferingSetToTrue_UsingMutatedOptions()
    {
        // Arrange
        var mvcOptions = new MvcOptions()
        {
            SuppressInputFormatterBuffering = false,
        };
        var formatter = new NewtonsoftJsonInputFormatter(
            GetLogger(),
            _serializerSettings,
            ArrayPool <char> .Shared,
            _objectPoolProvider,
            mvcOptions,
            new MvcNewtonsoftJsonOptions());

        var content      = "{name: 'Person Name', Age: '30'}";
        var contentBytes = Encoding.UTF8.GetBytes(content);
        var httpContext  = new DefaultHttpContext();

        httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature());
        httpContext.Request.Body        = new NonSeekableReadStream(contentBytes);
        httpContext.Request.ContentType = "application/json";

        var formatterContext = CreateInputFormatterContext(typeof(User), httpContext);

        // Act
        // Mutate options after passing into the constructor to make sure that the value type is not store in the constructor
        mvcOptions.SuppressInputFormatterBuffering = true;
        var result = await formatter.ReadAsync(formatterContext);

        // Assert
        Assert.False(result.HasError);

        var userModel = Assert.IsType <User>(result.Model);

        Assert.Equal("Person Name", userModel.Name);
        Assert.Equal(30, userModel.Age);

        Assert.False(httpContext.Request.Body.CanSeek);
        result = await formatter.ReadAsync(formatterContext);

        // Assert
        Assert.False(result.HasError);
        Assert.Null(result.Model);
    }
Пример #10
0
        public static void UseSanitizerInputFormatter(this MvcOptions opts, IServiceCollection services)
        {
            opts.InputFormatters.RemoveType <NewtonsoftJsonInputFormatter>();

            var serializerSettings = new JsonSerializerSettings
            {
                ContractResolver = new SanitizerContractResolver()
            };

            var sp                 = services.BuildServiceProvider();
            var logger             = sp.GetService <ILoggerFactory>().CreateLogger <MvcOptions>();
            var objectPoolProvider = sp.GetService <ObjectPoolProvider>();
            var jsonOpt            = new MvcNewtonsoftJsonOptions();

            var jsonInputFormatter = new NewtonsoftJsonInputFormatter(logger, serializerSettings, ArrayPool <char> .Shared, objectPoolProvider, opts, jsonOpt);

            opts.InputFormatters.Add(jsonInputFormatter);
        }
Пример #11
0
        public void Configure(MvcOptions options)
        {
            var systemTextJsonInputFormatter = new SystemTextJsonInputFormatter(
                _jsonOptions.Value,
                _loggerFactory.CreateLogger <SystemTextJsonInputFormatter>());

            var newtonsoftJsonInputFormatter = new NewtonsoftJsonInputFormatter(
                _loggerFactory.CreateLogger <NewtonsoftJsonInputFormatter>(),
                _mvcNewtonsoftJsonOptions.Value.SerializerSettings,
                _charPool,
                _objectPoolProvider,
                options,
                _mvcNewtonsoftJsonOptions.Value);

            options.InputFormatters.RemoveType <SystemTextJsonInputFormatter>();
            options.InputFormatters.RemoveType <NewtonsoftJsonInputFormatter>();
            options.InputFormatters.Add(new AbpHybridJsonInputFormatter(systemTextJsonInputFormatter, newtonsoftJsonInputFormatter));

            var jsonSerializerOptions = _jsonOptions.Value.JsonSerializerOptions;

            if (jsonSerializerOptions.Encoder is null)
            {
                // If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters.
                jsonSerializerOptions = new JsonSerializerOptions(jsonSerializerOptions)
                {
                    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                };
            }

            var systemTextJsonOutputFormatter = new SystemTextJsonOutputFormatter(jsonSerializerOptions);
            var newtonsoftJsonOutputFormatter = new NewtonsoftJsonOutputFormatter(
                _mvcNewtonsoftJsonOptions.Value.SerializerSettings,
                _charPool,
                options);

            options.OutputFormatters.RemoveType <SystemTextJsonOutputFormatter>();
            options.OutputFormatters.RemoveType <NewtonsoftJsonOutputFormatter>();
            options.OutputFormatters.Add(new AbpHybridJsonOutputFormatter(systemTextJsonOutputFormatter, newtonsoftJsonOutputFormatter));
        }
    public async Task ReadAsync_AllowUserCodeToHandleDeserializationErrors()
    {
        // Arrange
        var serializerSettings = new JsonSerializerSettings
        {
            Error = (sender, eventArgs) =>
            {
                eventArgs.ErrorContext.Handled = true;
            }
        };
        var formatter = new NewtonsoftJsonInputFormatter(
            GetLogger(),
            serializerSettings,
            ArrayPool <char> .Shared,
            _objectPoolProvider,
            new MvcOptions(),
            new MvcNewtonsoftJsonOptions());

        var content      = $"{{'id': 'should be integer', 'name': 'test location'}}";
        var contentBytes = Encoding.UTF8.GetBytes(content);
        var httpContext  = new DefaultHttpContext();

        httpContext.Features.Set <IHttpResponseFeature>(new TestResponseFeature());
        httpContext.Request.Body        = new NonSeekableReadStream(contentBytes, allowSyncReads: false);
        httpContext.Request.ContentType = "application/json";

        var formatterContext = CreateInputFormatterContext(typeof(Location), httpContext);

        // Act
        var result = await formatter.ReadAsync(formatterContext);

        // Assert
        Assert.False(result.HasError);
        var location = (Location)result.Model;

        Assert.Equal(0, location?.Id);
        Assert.Equal("test location", location?.Name);
    }
Пример #13
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers()
            .AddNewtonsoftJson();

            services.AddOptions <MvcOptions>()
            .Configure <ILoggerFactory, ObjectPoolProvider>((options, loggerFactory, objectPoolProvider) =>
            {
                JsonApiSerializerSettings serializerSettings = new JsonApiSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore
                };

                MvcNewtonsoftJsonOptions jsonOptions = new MvcNewtonsoftJsonOptions();

                NewtonsoftJsonOutputFormatter jsonApiOutputFormatter = new NewtonsoftJsonOutputFormatter(serializerSettings, ArrayPool <Char> .Shared, options);
                options.OutputFormatters.RemoveType <NewtonsoftJsonOutputFormatter>();
                options.OutputFormatters.Add(jsonApiOutputFormatter);

                NewtonsoftJsonInputFormatter jsonApiInputFormatter = new NewtonsoftJsonInputFormatter(loggerFactory.CreateLogger <NewtonsoftJsonInputFormatter>(), serializerSettings, ArrayPool <Char> .Shared, objectPoolProvider, options, jsonOptions);
                options.InputFormatters.RemoveType <NewtonsoftJsonInputFormatter>();
                options.InputFormatters.Add(jsonApiInputFormatter);
            });
        }