POST /api/v1/Employees returned 500 with a runtime mapping error:
- source:
TalentManagementAPI.Application.Features.Employees.Commands.CreateEmployee.CreateEmployeeCommand - destination:
TalentManagementAPI.Domain.Entities.Employee - type:
Map
The failure happened while compiling/executing the mapping expression for create employee.
Employee creation relied on runtime mapping compilation for CreateEmployeeCommand -> Employee.
Employee.Name is a value object (PersonName) and this path was susceptible to runtime mapping compile failures.
Updated create mapping to explicit construction using MapWith(...) in:
TalentManagementAPI.Application/Mappings/GeneralProfile.cs
config.NewConfig<CreateEmployeeCommand, Employee>()
.Map(dest => dest.Name, src => new PersonName(src.FirstName, src.MiddleName, src.LastName));config.NewConfig<CreateEmployeeCommand, Employee>()
.MapWith(src => new Employee
{
Name = new PersonName(src.FirstName, src.MiddleName, src.LastName),
PositionId = src.PositionId,
DepartmentId = src.DepartmentId,
Salary = src.Salary,
Birthday = src.Birthday,
Email = src.Email,
Gender = src.Gender,
EmployeeNumber = src.EmployeeNumber,
Prefix = src.Prefix,
Phone = src.Phone
});This removes ambiguity in runtime mapping generation and ensures all fields required for create are explicitly mapped.
- Applied the same explicit mapping pattern to other create commands:
CreateDepartmentCommand -> DepartmentCreatePositionCommand -> PositionCreateSalaryRangeCommand -> SalaryRange
- Improved error diagnostics:
TalentManagementAPI.WebApi/Middlewares/ErrorHandlerMiddleware.csnow logs full exception details:- from
_logger.LogError(error.Message); - to
_logger.LogError(error, "Unhandled exception");
- from
Added mapping tests to prevent recurrence:
TalentManagementAPI.Application.Tests/Mappings/GeneralProfileCreateMappingsTests.cs- verifies
CreateEmployeeCommand -> Employeemapping includingPersonName - verifies other create-command mappings
- verifies
Also updated:
TalentManagementAPI.Application.Tests/Positions/UpdatePositionCommandHandlerTests.cs- to validate recently fixed update fields (
PositionNumber,DepartmentId,SalaryRangeId)
- to validate recently fixed update fields (
Application tests were executed and passed:
TalentManagementAPI.Application.Tests: Passed50, Failed0.