<commit_before>package org.ngrinder.user.controller;

import com.google.common.collect.Lists;
import org.ngrinder.common.controller.BaseController;
import org.ngrinder.common.controller.RestAPI;
import org.ngrinder.infra.config.Config;
import org.ngrinder.model.Permission;
import org.ngrinder.model.User;
import org.ngrinder.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.ngrinder.common.util.CollectionUtils.newArrayList;
import static org.ngrinder.common.util.Preconditions.checkNotEmpty;

@RestController
@RequestMapping("/user/api")
public class UserApiController extends BaseController {

	@Autowired
	protected Config config;

	@Autowired
	private UserService userService;

	/**
	 * Get the follower list.
	 *
	 * @param user     current user
	 * @param keywords search keyword.
	 * @return json message
	 */
	@RestAPI
	@GetMapping("/switch_options")
	public HttpEntity<String> switchOptions(User user, @RequestParam(defaultValue = "") final String keywords) {
		return toJsonHttpEntity(getSwitchableUsers(user, keywords));
	}

	/**
	 * Get the current user profile.
	 *
	 * @param user  current user
	 */
	@RestAPI
	@GetMapping("/profile")
	public HttpEntity<String> getOne(User user) {
		checkNotEmpty(user.getUserId(), "UserID should not be NULL!");
		Map<String, Object> model = new HashMap<>();
		User one = userService.getOneWithFollowers(user.getUserId());
		model.put("user", one);
		model.put("allowPasswordChange", !config.isDemo());
		model.put("allowRoleChange", false);
		model.put("showPasswordByDefault", false);
		attachCommonAttribute(one, model);
		return toJsonHttpEntity(model);
	}

	/**
	 * Get user list that current user will be shared, excluding current user.
	 *
	 * @param user  current user
	 * @param model model
	 */
	private void attachCommonAttribute(User user, Map<String, Object> model) {
		List<User> list = user.getFollowers() == null ? Lists.newArrayList() : user.getFollowers();
		// TODO handle this when remove Gson.
		// prevent stack overflow when serialize user list.
		user.setFollowers(null);
		model.put("followers", list.stream().map(UserController.UserSearchResult::new).collect(Collectors.toList()));
		model.put("allowShareChange", true);
		model.put("userSecurityEnabled", config.isUserSecurityEnabled());
	}

	private List<UserController.UserSearchResult> getSwitchableUsers(User user, String keywords) {
		if (user.getRole().hasPermission(Permission.SWITCH_TO_ANYONE)) {
			List<UserController.UserSearchResult> result = newArrayList();
			for (User each : userService.getPagedAll(keywords, PageRequest.of(0, 10))) {
				result.add(new UserController.UserSearchResult(each));
			}
			return result;
		} else {
			return userService.getSharedUser(user);
		}
	}
}<commit_msg>Avoid stack overflow exception when serialize User list<commit_after>package org.ngrinder.user.controller;

import com.google.common.collect.Lists;
import org.ngrinder.common.controller.BaseController;
import org.ngrinder.common.controller.RestAPI;
import org.ngrinder.infra.config.Config;
import org.ngrinder.model.Permission;
import org.ngrinder.model.User;
import org.ngrinder.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.ngrinder.common.util.CollectionUtils.newArrayList;
import static org.ngrinder.common.util.Preconditions.checkNotEmpty;

@RestController
@RequestMapping("/user/api")
public class UserApiController extends BaseController {

	@Autowired
	protected Config config;

	@Autowired
	private UserService userService;

	/**
	 * Get the follower list.
	 *
	 * @param user     current user
	 * @param keywords search keyword.
	 * @return json message
	 */
	@RestAPI
	@GetMapping("/switch_options")
	public HttpEntity<String> switchOptions(User user, @RequestParam(defaultValue = "") final String keywords) {
		return toJsonHttpEntity(getSwitchableUsers(user, keywords));
	}

	/**
	 * Get the current user profile.
	 *
	 * @param user  current user
	 */
	@RestAPI
	@GetMapping("/profile")
	public HttpEntity<String> getOne(User user) {
		checkNotEmpty(user.getUserId(), "UserID should not be NULL!");
		Map<String, Object> model = new HashMap<>();
		User one = userService.getOneWithFollowers(user.getUserId());
		model.put("user", one);
		model.put("allowPasswordChange", !config.isDemo());
		model.put("allowRoleChange", false);
		model.put("showPasswordByDefault", false);
		attachCommonAttribute(one, model);
		return toJsonHttpEntity(model);
	}

	/**
	 * Get user list that current user will be shared, excluding current user.
	 *
	 * @param user  current user
	 * @param model model
	 */
	private void attachCommonAttribute(User user, Map<String, Object> model) {
		List<User> followers = user.getFollowers() == null ? Lists.newArrayList() : user.getFollowers();
		List<User> owners = user.getOwners() == null ? Lists.newArrayList() : user.getOwners();

		// TODO handle this when remove Gson.
		// prevent stack overflow when serialize user list.
		user.setFollowers(null);
		user.setOwners(null);

		model.put("followers", followers.stream().map(UserController.UserSearchResult::new).collect(Collectors.toList()));
		model.put("owners", owners.stream().map(UserController.UserSearchResult::new).collect(Collectors.toList()));
		model.put("allowShareChange", true);
		model.put("userSecurityEnabled", config.isUserSecurityEnabled());
	}

	private List<UserController.UserSearchResult> getSwitchableUsers(User user, String keywords) {
		if (user.getRole().hasPermission(Permission.SWITCH_TO_ANYONE)) {
			List<UserController.UserSearchResult> result = newArrayList();
			for (User each : userService.getPagedAll(keywords, PageRequest.of(0, 10))) {
				result.add(new UserController.UserSearchResult(each));
			}
			return result;
		} else {
			return userService.getSharedUser(user);
		}
	}
}
{'fraction_non_alphanumeric': 0.12106430155210643, 'fraction_numerical': 0.0008869179600886918, 'mean_word_length': 5.462273161413562, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3232531', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1961, 'n_words': 424}