Reputation: 1045
I'm creating an integration test to validate one REST API. All happens as expected. But when I'm trying to deserialize the content of the response, one property with IEnumerable type is not filled with the values present into the string json.
See below part of the test code:
var inputValue = new List<InputValueType> {
new InputValueType()
};
var request = new HttpRequestMessage(new HttpMethod("POST"), "/api/endpoint")
{
Content = new StringContent(
Newtonsoft.Json.JsonConvert.SerializeObject(inputValue),
Encoding.UTF8,
MediaTypeNames.Application.Json
)
};
var response = await _client.SendAsync(request);
var contentString = await response.Content.ReadAsStringAsync();
var content = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseValueType>(contentString);
The contentString is
{
"status": "ERRO",
"codigo": "400",
"descricao": "Quantidade de Registros não processados: 1",
"folhas": [
{
"tpInscEmpregador": null,
"nrInscEmpregador": null,
"tpInscEstabelecimento": null,
"nrInscEstabelecimento": null,
"matricula": null,
"cpf": null,
"idFolha": null,
"tpFolha": null,
"erro": "CPF,TpInscEmpregador,NrInscEmpregador,Matricula,IdFolha,IdFolhaPai,TpFolha,CargoCodigo,CargoNome,LotacaoCodigo,LotacaoNome,TpInscEstabelecimento,NrInscEstabelecimento,AnoMes,DataPagamento,DataInicial,DataFinal,Periodicidade,ValorProventos,ValorDescontos,ValorLiquido,SalarioContratual,BaseCalculoINSS,BaseCalculoFGTS,BaseCalculoIRRF,SaldoFGTS,ValorMargem,FGTS,FGTSContSocial,Rubricas"
}
]
}
The ResponseValueType is
public class ResponseValueType
{
public string Status { get; set; }
public string Codigo { get; set; }
public string Descricao { get; set; }
public virtual IEnumerable<ResponseValueTypeErro> Folhas { get; }
public ResponseValueType()
{
Status = "OK";
Codigo = "200";
Descricao = "Sucesso";
}
public ResponseValueType(string status, string codigo, string descricao)
{
Status = status;
Codigo = codigo;
Descricao = descricao;
}
public ResponseValueType(string status, string codigo, string descricao, IEnumerable<ResponseValueTypeErro> errors)
: this(status, codigo, descricao)
{
Folhas = errors.ToList();
}
}
public class ResponseValueTypeErro
{
public string TpInscEmpregador { get; set; }
public string NrInscEmpregador { get; set; }
public string TpInscEstabelecimento { get; set; }
public string NrInscEstabelecimento { get; set; }
public string Matricula { get; set; }
public string CPF { get; set; }
public string IdFolha { get; set; }
public string TpFolha { get; set; }
public string Erro { get; set; }
}
However, when the contentString is deserialized, the content keep with property Folhas equal null.
What can I do to do Folhas property deserialize correctly?
Upvotes: 0
Views: 373
Reputation: 1045
The property just need to have a set too. The code works changing it:
public virtual IEnumerable<ResponseValueTypeErro> Folhas { get; }
To it:
public virtual IEnumerable<ResponseValueTypeErro> Folhas { get; set; }
Upvotes: 0