Skip to content

Enable UrlTemplating #2236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marccollin opened this issue May 19, 2023 · 5 comments
Closed

Enable UrlTemplating #2236

marccollin opened this issue May 19, 2023 · 5 comments
Labels
question Further information is requested

Comments

@marccollin
Copy link

marccollin commented May 19, 2023

If you ❤️ this project consider becoming a sponsor.
If you already use the springdoc-openapi, please do not forget adding a github star to this repository.

Actually with springfox it's possible to use url templating

@bean
public Docket categoryApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("category-api")
.apiInfo(apiInfo())
.select()
.paths(categoryPaths())
.build()
.ignoredParameterTypes(ApiIgnore.class)
.enableUrlTemplating(true);
}

Actually with springdoc when we have theses both endpoint

@GetMapping(value = "/{id}", params = "name")
@Operation(description = "check duplicate")
public ResponseEntity<List<DuplicatePerson>> findDuplicatePerson(@PathVariable Long id, @RequestParam String name) {
    return new ResponseEntity<>(personService.findDuplicatePerson(id, nom), HttpStatus.OK);
}

@GetMapping("/{id}")
@Operation(description = "Get person by its id")
public ResponseEntity<PersonDto> findPersonById(@PathVariable
                                                  @NotNull
                                                  Long id) {
    return new ResponseEntity<>(personService.findPersonById(id), HttpStatus.OK);
}

Only the first is displayed underswagger.

With sprindoc, when we enable url templating, both are displayed

Spring doc should have a similar option to support that

@uc4w6c
Copy link
Collaborator

uc4w6c commented May 27, 2023

Please see below.
#1726
#580
#71

@marccollin
Copy link
Author

i checked 1726, seem like a pull request should be already merged to fix this issue.... don't seem true, i use already the newest release

@uc4w6c
Copy link
Collaborator

uc4w6c commented May 31, 2023

@marccollin
sorry.
I was able to display both by adding the following code.

@Configuration
public class DuplicateConfiguration {
  @Bean
  public RouterOperationCustomizer addRouterOperationCustomizer() {
    return (routerOperation, handlerMethod) -> {
      if (routerOperation.getParams().length > 0) {
        routerOperation.setPath(routerOperation.getPath() + "?" + String.join("&", routerOperation.getParams()));
      }
      return routerOperation;
    };
  }
}

Controller

@RestController
@RequestMapping("duplicate2")
public class Duplicate2Controller {
  @GetMapping(value = "/{id}", params = "name")
  @Operation(description = "check duplicate")
  public String findDuplicatePerson(@PathVariable Long id, @RequestParam String name) {
    return "id:" + id.toString() + ", name:" + name;
  }

  @GetMapping("/{id}")
  @Operation(description = "Get person by its id")
  public String findPersonById(@PathVariable @NotNull Long id) {
    return id.toString();
  }
}

スクリーンショット 2023-05-31 21 46 31

@marccollin
Copy link
Author

you added code to your application. So the pull request was not merged?

@uc4w6c
Copy link
Collaborator

uc4w6c commented May 31, 2023

PR #1706 has been merged.
RouterOperationCustomizer is now available with this PR.

Since it is not a natural choice to create two methods for the same API resource, springdoc-openapi does not have this implementation by default, but you can customize it yourself using RouterOperationCustomizer.

@bnasslahsen bnasslahsen added the question Further information is requested label Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants