Generator-jhipster: swagger-ui generate issue when `DeleteMapping` with regex.

Created on 15 Aug 2020  路  6Comments  路  Source: jhipster/generator-jhipster

Overview of the issue

"jhipsterVersion": "6.10.1"

    /**
     * {@code DELETE /users/:login} : delete the "login" User.
     *
     * @param login the login of the user to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/users/{login:" + Constants.LOGIN_REGEX + "}")
    @PreAuthorize("hasAuthority(\"" + AuthoritiesConstants.ADMIN + "\")")
    public ResponseEntity<Void> deleteUser(@PathVariable String login) {
        log.debug("REST request to delete User: {}", login);
        userService.deleteUser(login);
        return ResponseEntity.noContent().headers(HeaderUtil.createAlert(applicationName,  "userManagement.deleted", login)).build();
    }

image

area swagger

Most helpful comment

I did not know that you could use regex in path variable declaration in URI templates but docs confirms it https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestmapping-uri-templates

However, I would think that it's simpler to not mix up things and it seems that Springfox has an issue with it as you discovered it :)

So, I would suggest that you report it to Springfox project.

For JHipster, I would only recommend to use validation annotations on the method parameter as below, this has the benefit of using LOGIN_REGEX in expected context without interfering with Springfox

~java
@DeleteMapping("/users/{login}")
public ResponseEntity deleteUser(@PathVariable("login") @Pattern(Constants.LOGIN_REGEX) String login) {
~

All 6 comments

@Michael2008S

The bug comes from Constants.java

public static final String LOGIN_REGEX = "^(?>[a-zA-Z0-9!$&*+=?^_`{|}~.-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*)|(?>[_.@A-Za-z0-9-]+)$";

Curly braces generate a bug in

@GetMapping("/users/{login:" + Constants.LOGIN_REGEX + "}")

and

@DeleteMapping("/users/{login:" + Constants.LOGIN_REGEX + "}")

because curly braces are specials characters in mapping definition. Even if I escape curly braces, it doesn't work.

A solution will be to remove curly braces in LOGIN_REGEX and in the front.

What do you think @pascalgrimaud & @mshima ?

I did not know that you could use regex in path variable declaration in URI templates but docs confirms it https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestmapping-uri-templates

However, I would think that it's simpler to not mix up things and it seems that Springfox has an issue with it as you discovered it :)

So, I would suggest that you report it to Springfox project.

For JHipster, I would only recommend to use validation annotations on the method parameter as below, this has the benefit of using LOGIN_REGEX in expected context without interfering with Springfox

~java
@DeleteMapping("/users/{login}")
public ResponseEntity deleteUser(@PathVariable("login") @Pattern(Constants.LOGIN_REGEX) String login) {
~

If @gmarziou solution works, it seems to be the right approach.
I think old generated validation is broken ...in:^(?>... (line start).

For those brave enough 馃槂

Here is the Paths class that is probably responsible for this issue in Springfox and its unit test that does not cover a regex using {} characters.

Ok then, it looks like @gmarziou's solution is the good one. We should apply this change in our templates

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RizziCR picture RizziCR  路  3Comments

edvjacek picture edvjacek  路  3Comments

lsadehaan picture lsadehaan  路  3Comments

SudharakaP picture SudharakaP  路  3Comments

sdoxsee picture sdoxsee  路  4Comments