spring-projects / spring-hateoas

Spring HATEOAS - Library to support implementing representations for hyper-text driven REST web services.
https://spring.io/projects/spring-hateoas
Apache License 2.0
1.04k stars 477 forks source link

UriTemplate does not handle empty non-mandatory variables with hyphens in their names #799

Closed LukeThomas1991 closed 5 years ago

LukeThomas1991 commented 5 years ago

Our Request Params have hyphens within their names.

The regex in the UriTemplate cannot handle the '-' within their names.

private static final Pattern VARIABLE_REGEX = Pattern.compile("\\{([\\?\\&#/]?)([\\w\\,*]+)\\}");

Can this regex get changed to:

private static final Pattern VARIABLE_REGEX = Pattern.compile("\\{([\\?\\&#/]?)([\\w\\,*-]+)\\}");

?

Otherwise we end up with the following exception:

java.lang.IllegalArgumentException: Illegal character in query at index 101: XXXXXXXXX
    at java.net.URI.create(URI.java:852)
    at org.springframework.hateoas.UriTemplate.expand(UriTemplate.java:219)
    at org.springframework.hateoas.client.Traverson$TraversalBuilder.traverseToFinalUrl(Traverson.java:384)
    at org.springframework.hateoas.client.Traverson$TraversalBuilder.toObject(Traverson.java:321)
gregturn commented 5 years ago

As shown in #736, RFC 6570 (UriTemplate spec) doesn't support hyphens in variable names.