C
C#7mo ago
OukiShogun

Field required but I don't mention "required" above

Hello there, I have a problem. My field is considered as required by .net but I don't created it as required.
public abstract class ScheduledJobBaseDto
{
/// <summary>
/// Job group name
/// </summary>
public string JobGroupName { get; set; }

//...
}

public class ScheduledJobWriteDto : ScheduledJobBaseDto
{
}
public abstract class ScheduledJobBaseDto
{
/// <summary>
/// Job group name
/// </summary>
public string JobGroupName { get; set; }

//...
}

public class ScheduledJobWriteDto : ScheduledJobBaseDto
{
}
Below my controller
[SwaggerRequestExample(typeof(ScheduledJobWriteDtoDefault), typeof(ScheduledJobWriteDto))]
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Post(ScheduledJobWriteDto writeDto)
{
try
{
var readDto = await this._jobService.CreateScheduledJobAsync(writeDto);

// Build URI
string uri = $"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}/{writeDto.JobName}";

return Created(uri, readDto);
} // ...
}
[SwaggerRequestExample(typeof(ScheduledJobWriteDtoDefault), typeof(ScheduledJobWriteDto))]
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Post(ScheduledJobWriteDto writeDto)
{
try
{
var readDto = await this._jobService.CreateScheduledJobAsync(writeDto);

// Build URI
string uri = $"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}/{writeDto.JobName}";

return Created(uri, readDto);
} // ...
}
And the response { "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title": "One or more validation errors occurred.", "status": 400, "traceId": "00-5588746f27392f4367a0d759b70b8f78-20699cede6741c50-00", "errors": { "JobGroupName": [ "The JobGroupName field is required." ] } } I don't understand why. Can someone help me please
3 Replies
OukiShogun
OukiShogun7mo ago
The execution don't enter in my breakpoint at the first "{" of my Post function
Thinker
Thinker7mo ago
Have you tried making the property nullable?
OukiShogun
OukiShogun7mo ago
No 🤦‍♂️ I will try and came back to you