Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Remove `FromBodyOnProperty_RequiredOnValueTypeSubProperty_AddsModelSt…
Browse files Browse the repository at this point in the history
…ateError`

- this integration test depends on the old `[Required]` special case
- #2388 explicitly removes that bahaviour so the test goes with it
  • Loading branch information
dougbu committed May 12, 2015
1 parent de63075 commit fcf7b15
Showing 1 changed file with 0 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,67 +220,5 @@ public async Task FromBodyOnTopLevelProperty_RequiredOnSubProperty_AddsModelStat
var error = Assert.Single(modelState[street].Errors);
Assert.Equal("The Street field is required.", error.ErrorMessage);
}

private class Person3
{
[FromBody]
public Address3 Address { get; set; }
}

private class Address3
{
public string Street { get; set; }

[Required]
public int Zip { get; set; }
}

[Theory]
[InlineData("{ \"Street\" : \"someStreet\" }")]
[InlineData("{}")]
public async Task FromBodyOnProperty_RequiredOnValueTypeSubProperty_AddsModelStateError(string inputText)
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
BindingInfo = new BindingInfo()
{
BinderModelName = "CustomParameter",
},
ParameterType = typeof(Person3)
};

var operationContext = ModelBindingTestHelper.GetOperationBindingContext(
request =>
{
request.Body = new MemoryStream(Encoding.UTF8.GetBytes(inputText));
request.ContentType = "application/json";
});
var httpContext = operationContext.HttpContext;
var actionContext = httpContext.RequestServices.GetRequiredService<IScopedInstance<ActionContext>>().Value;
var modelState = actionContext.ModelState;

// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);

// Assert
Assert.NotNull(modelBindingResult);
Assert.True(modelBindingResult.IsModelSet);
var boundPerson = Assert.IsType<Person3>(modelBindingResult.Model);
Assert.NotNull(boundPerson);
Assert.False(modelState.IsValid);
var street = Assert.Single(modelState.Keys, k => k == "CustomParameter.Address.Street");
Assert.Equal(ModelValidationState.Valid, modelState[street].ValidationState);

// The error with an empty key is a bug(#2416) in our implementation which does not append the prefix and
// use that along with the path. The expected key here would be Address.
var zip = Assert.Single(modelState.Keys, k => k == "CustomParameter.Address.Zip");
Assert.Equal(ModelValidationState.Valid, modelState[zip].ValidationState);
var error = Assert.Single(modelState[""].Errors);
Assert.StartsWith(
"Required property 'Zip' not found in JSON. Path ''",
error.Exception.Message);
}
}
}

0 comments on commit fcf7b15

Please sign in to comment.