I just came up with one issue. I have 2 models Department
and Student
:
public class Department
{
public int Id { get; set; }
public string DepartmentName { get; set; }
public int DepartmentCapacity { get; set; }
public List<Student>? Students { get; set; }
}
public class Student
{
public int id { get; set; }
public string StudentName { get; set; }
public string StudentAge { get; set; }
public int Department_id { get; set; }
public Department? Department { get; set; }
}
In Swagger, when I'm going to add a Department
, it asks me to insert these details:
{
"id": 1,
"departmentName": "IT",
"departmentCapacity": 20,
"students": [
{
"id": 0,
"studentName": "string",
"studentAge": "string",
"department_id": 0,
"department": "string"
}
]
}
I'm not passing the student details as it's a nullable collection.
When clicking on Execute
(the controller's action method is not called yet) I get this error:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-aab9a33164e1e58039404c004dfbec6b-317a50661e166115-00",
"errors": {
"department": [
"The department field is required."
],
"$.students[0].department": [
"The JSON value could not be converted to OneToOneRelationshipWebApi.Model.Department. Path: $.students[0].department | LineNumber: 10 | BytePositionInLine: 28."
]
}
}
And while adding a Student
, it asks me to enter these details:
{
"id": 0,
"studentName": "Abhishek",
"studentAge": "23",
"department_id": 1,
"department": {
"id": 0,
"departmentName": "string",
"departmentCapacity": 0,
"students": [
"string"
]
}
}
When executing, I get this error:
"errors": {
"student": [
"The student field is required."
],
"$.department.students[0]": [
"The JSON value could not be converted to OneToOneRelationshipWebApi.Model.Student. Path: $.department.students[0] | LineNumber: 10 | BytePositionInLine: 14."
]
}
Please, can you confirm what I'm doing wrong here and how I can fix it?
I tried to add the NewtonSoft Json but it didn't help, also tried to fix it in all possible ways but still getting the same error.
The error in case one (when you want to add Department) is thrown, because you are passing as value of "department" property "string". If you don't want to pass "department" object, just remove property from json:
{
"id": 1,
"departmentName": "IT",
"departmentCapacity": 20,
"students": [
{
"id": 0,
"studentName": "string",
"studentAge": "string",
"department_id": 0//, remove this comma
//"department": "string" //remove this line
}
]
}
I you want to have "department" property fulfilled, you have to do it like this:
{
"id": 1,
"departmentName": "IT",
"departmentCapacity": 20,
"students": [
{
"id": 0,
"studentName": "string",
"studentAge": "string",
"department_id": 0,
"department": {
"id": 0,
"departmentName": "string",
"departmentCapacity": 0
}
}
]
}
Again in case 2, when you are trying to add Student, remove
{
"id": 0,
"studentName": "Abhishek",
"studentAge": "23",
"department_id": 1,
"department": {
"id": 0,
"departmentName": "string",
"departmentCapacity": 0//, -> remove this comma
//"students":[ -> remove this part
// "string" -> remove this part
// ] -> remove this part
}
}
I you want to have "students" property fulfilled, you have to do it like this:
{
"id": 0,
"studentName": "Abhishek",
"studentAge": "23",
"department_id": 1,
"department": {
"id": 0,
"departmentName": "string",
"departmentCapacity": 0,
"students":[
{
"id": 0,
"studentName": "Abhishek",
"studentAge": "23",
"department_id": 1
},
{
"id": 1,
"studentName": "Abhishek",
"studentAge": "23",
"department_id": 2
}
]
}
}
Conclusion is, if you want to have that property "null", you have to remove it from json, or fulfill it with all parameters, which will have some default empty values.