0

hope you doing great !

I am working currently on a bff (Back for frontend) project with ASP.Net Core, that consumes an external api. For simplicity purposes, we used **Nswag **which is a tool to generate code from an api specification compliant to the OpenApi Specification. It generates for me, date models in addition to methods for requesting each ressource of the api.

While testing my first Controller for the project, i relalized that the request body i am sending contains a new property "ValueKind" that i didn't send initialy, and that was added after the deserialization of the request body.

You ll find attatched to this description, examples of the data model of the request body, and the body i am recieving in the controller.

Has anyone run into the same problem before ?

The request when sent :

{ "someProperty": {} }

The request i get in the controller :

{ "SomeProperty": { "ValueKind": 3 } }

  • Is `ValueKind` a property in your model? If you don't want to serialize this property, you can try to add `[JsonIgnore]` attribute to this property. – Xinran Shen Aug 28 '23 at 08:21
  • Hello @XinranShen, thanks for your time. As i said in the description, the property "ValueKind" does not exist initially in the model, and i suspect that it was added during the deserialization. – Nourelayne Salah eddine Aug 28 '23 at 08:25
  • I can't reproduce your situation, But refer to this [link](https://www.reddit.com/r/dotnet/comments/w1f920/net_6_rest_api_is_deserializing_property_of/), It seems to have the same situation. – Xinran Shen Aug 28 '23 at 08:35
  • Thank you for your help. I'll check it out. – Nourelayne Salah eddine Aug 28 '23 at 08:36

1 Answers1

0

Update

I managed finally to solve the problem. The reason is that the default serializer for the ASP.Net core in my project was different from the one used in Nswag. In Nswag, we use NewtonSoft serializer, while in my ASP.Net core project we use System.Text.Json serializer.

Since we can't use the System.Text.Json library as a default serializer for Nswag, i changed the default serializer for my Asp.Net project to NewtonSoft Serializer. To do that we simply have to add this line of code:

enter image description here