Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning null with Optional.orElseGet() and setting response value

I have a class TeamResponse used to form a JSON response. It has a handful of parameters, and one of them can be optional:

public TeamResponse(Team team, List<TeamSkillTemplateResponse> teamSkillTemplateResponses) {
    this.id = team.getId();
    this.name = team.getName();
    this.department = new DepartmentResponse(team.getDepartment());
    this.division = new DivisionResponse(team.getDepartment().getDivision());
    this.valueStream = team.getValueStream().map(ValueStreamResponse::new).orElseGet(null);
    this.skillTemplate =  teamSkillTemplateResponses;
}

I have actually two questions on this one: 1. Is it appropriate to return null in the response, if the value is not present? 2. Is it appropriate to return null THIS way? (method getTeam() returns Optional)

Thank you.

like image 783
pkey Avatar asked Jul 31 '17 07:07

pkey


1 Answers

  1. yes it is. json output will be field not being present, or field null; it depends on json framework configuration. both kind of results are ok from json point of view. Difference only matters if talking about partial updates.
  2. no, you should use .orElse(null) not orElseGet (there you are passing a null supplier, not a null result value).
like image 114
albert_nil Avatar answered Nov 14 '22 23:11

albert_nil