Spring data rest sorting fields with underscores

It is unclear whether you can do this in some Spring Data Rest specific way however you should be able to handle it by means of a standard Servlet filter which would look something like the below:

public class SortParameterConversionFilter extends GenericFilterBean {

    // as we are extending Spring's GenericFilterBean
    // you can then *possibly* inject the RepositoryRestConfiguration
    // and use  RepositoryRestConfiguration#getSortParamName
    // to avoid hard coding
    private static final String SORT_PARAM_KEY = "sort";

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;

        if (shouldApply(request)) {
            chain.doFilter(new CollectionResourceRequestWrapper(request), res);
        } else {
            chain.doFilter(req, res);
        }
    }

    /**
     * 
     * @param request
     * @return True if this filter should be applied for this request, otherwise
     *         false.
     */
    protected boolean shouldApply(HttpServletRequest request) {
        return request.getServletPath().matches("some-pattern");
    }

    /**
     * HttpServletRequestWrapper implementation which allows us to wrap and
     * modify the incoming request.
     *
     */
    public class CollectionResourceRequestWrapper extends HttpServletRequestWrapper {

        public ResourceRequestWrapper(HttpServletRequest request) {
            super(request);
        }

        @Override
        public String getParameter(final String name) {
            if (name.equals(SORT_PARAM_KEY)) {
                String [] parts = super.getParameter(SORT_PARAM_KEY).split(",");
                StringBuilder builder = new StringBuilder();

                int index = 0;

                for (String part : parts) {
                    // using some mechanism of you choosing
                    // convert from underscore to camelCase
                    // Using the Guava library for example
                    String convertedPart = CaseFormat.LOWER_UNDERSCORE.to(
                         CaseFormat.LOWER_CAMEL, part);
                    ++index;
                    builder.append(convertedPart).append(index < parts.length ? "," : "");
                }

                return builder.toString();
            }

            return super.getParameter(name);
        }
    }
}

There is a bug for this - DATAREST-883. It was fixed and released. But then, due to regressions (DATAREST-909) this has been dropped in the very next release. I asked them on Github if they plan to have this again as this has bitten me in the past as well. We'll see what they have to say about this.

For now you can:

  • leave it be
  • go with the camel case property names
  • work around this (e.g. go with Alan Haye's answer) - this seems fragile IMHO, but probably will do in a short term.

The status of the feature in the recent spring-boot versions I've tested with:

  • 1.4.0 (spring-data-rest 2.5.2): not yet implemented
  • 1.4.1 (spring-data-rest 2.5.3): works -> code
  • 1.4.2 (spring-data-rest 2.5.5): dropped