12

We are using a very simple setup of @RepositoryRestResource on top of a PagingAndSortingRepository connected to a postgres database. Also we have configured spring.jackson.property-naming-strategy=SNAKE_CASE to return pretty json. It was all fine and dandy until we started sorting. As we have discovered - sorting requires us to provide the actual class field names (which we of course have in camel case):

get("/thing?sort=dateCreated,desc")

And when we try to do javascript friendly

get("/thing?sort=date_created,desc")

it fails miserably because jpa tries to split the parameter by the underscore.

Is there a simple way to have the path params the same format as we have them in the json that we are returning?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Rince
  • 2,195
  • 3
  • 21
  • 35
  • Did you try `__` (2 underscores)? (see http://stackoverflow.com/questions/29983047/spring-data-jpa-entity-property-names-contain-underscores) –  Nov 24 '16 at 17:37
  • Yep and that did not work either. Seems that `__` is just for custom methods. – Rince Nov 24 '16 at 17:45
  • Tried to reproduce it but it works fine -> [code on github](https://github.com/jannis-baratheon/stackoverflow/tree/master/40746303-spring-data-rest-sorting-fields-with-underscores). What versions are you using? – jannis Nov 26 '16 at 00:37

2 Answers2

10

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 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
jannis
  • 4,843
  • 1
  • 23
  • 53
4

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);
        }
    }
}
Alan Hay
  • 22,665
  • 4
  • 56
  • 110