Total Pageviews

2017/01/08

[Redmine] Fail to Create Project via Rest API again!

Problem
I am using Redmine rest api to create project, and had checked the project identifier only allowed lower letter (a-z), numbers, dashes and underscores. But it still go wrong when I only provide numbers in project identifier:
1
2
3
4
5
org.springframework.web.client.HttpClientErrorException: 422 Unprocessable Entity
 at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
 at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]


How-to
According to this issue, the project identifier cannot accept number only although the document does not say it.

Therefore, we need to do one more check as we create project (you need to import org.apache.commons.lang.StringUtils class):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    private void checkProjectIdentifier(String identifier) {
        Pattern pattern = Pattern.compile("^[a-z0-9_=]*$");
        if (!pattern.matcher(identifier).matches()) {
            throw new RuntimeException("專案代碼僅允許使用小寫英文字母 (a-z), 阿拉伯數字, 虛線與底線");
        }
        
        if(StringUtils.isNumeric(identifier)) {
            throw new RuntimeException("專案代碼不可僅有數字");
        }
    }


Reference
[1] https://www.redmine.org/issues/5877

No comments: