I have a DTO object which I used in the controller method which initially had all the getters and setters as public. Due to a SAST scanning (Unsafe object binding) in checkmarx tool, I have to make the setters as private in that DTO. Now checkmarx is not complaining. But in my codebase, I have used some of those setters so now my build is failing. How to resolve this problem, or what technique should be used so that the checkmarx tool doesnot complain and my code builds and runs fine at the same time.
@SuppressWarnings("serial")
@NoArgsConstructor @AllArgsConstructor @ToString
@Builder(toBuilder=true)
public class PartDto implements Serializable {
private Date autoCsoCommit;
private Integer statusId;
public Integer getStatusId() {
return statusId;
}
private void setStatusId(Integer statusId) {
this.statusId = statusId;
}
public Date getAutoCsoCommit() {
return autoCsoCommit;
}
private void setAutoCsoCommit(Date autoCsoCommit) {
this.autoCsoCommit = autoCsoCommit;
}
Code where its used:
public PartDto createPart(final PartDto partDto) {
log.debug("PartDto: {}", partDto);
if (partExists(partDto.getPartNumber(), partDto.getEsnId(), partDto.getPoNumber()))
throw new ValidationException("For this ESN, the Part Number and PO Number combination already exists.");
log.debug("Part: {}", getPart(partDto, new Part()));
if (partDto.getStatusId() == null)
partDto.setStatusId(0);
Part finalPart = savePartDtoUpdateAutoCso(partDto, new Part());
return getPartDtoByPartId(finalPart.getPartId());
}
PartDto updateAutoCsoCommitDateForPartDto(PartDto partDto) {
if (partDto == null) {
log.error("partDto is null");
return null;
}
partDto.setAutoCsoCommit(retrieveAutoCsoDate(
partDto.getMaterialStream(),
partDto.getSource(),
partDto.getSourceId(),
partDto.getPartNumber(),
partDto.getPoNumber())
);
return partDto;
};
Error in build:
$ gradle bootrun
> Task :compileJava
C:\Users\502622018\moa_workspace\moa-svc\src\main\java\com\ge\digital\oa\moa\service\PartService.java:123: error: setStatusId(Integer) has private access in PartDto
partDto.setStatusId(0);
^
C:\Users\502622018\moa_workspace\moa-svc\src\main\java\com\ge\digital\oa\moa\service\PartService.java:366: error: setAutoCsoCommit(Date) has private access in PartDto
partDto.setAutoCsoCommit(retrieveAutoCsoDate(
^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Users\502622018\moa_workspace\moa-svc\src\main\java\com\ge\digital\oa\common\config\aws\SecretsManagerPropertySourceLocator.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
> Task :compileJava FAILED
FAILURE: Build failed with an exception.
The method where the partDTO object is saved is
private Part savePartDtoUpdateAutoCso(final PartDto partDto, final Part existingPart) {
// we need to save and retrieve the part so all string fields
// have values, since we use them for the auto cso commit determination
Part savedPart = partRepo.saveAndFlush(getPart(partDto, existingPart));
entityManager.refresh(savedPart);
log.debug("saved domain part: {}",savedPart);
PartDto savedPartDto = getPartDtoByPartId(savedPart.getPartId());
log.debug("Part after save: {}", savedPartDto);
updateAutoCsoCommitDateForPartDto(savedPartDto);
Part finalPart = partRepo.save(getPart(savedPartDto, savedPart));
return finalPart;
}
PostMapping code:
@PostMapping
public HttpEntity<PartDto> createPart(@RequestBody @Valid PartDto partDto) {
if (!securityService.isAdmin()) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
log.debug("Start..." + partDto);
return new ResponseEntity<>(partService.createPart(partDto), HttpStatus.CREATED);
}