question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong operationid generated

See original GitHub issue

Hello, This bug is probably the same as bug 96. OperationID continues to be generated with a suffix, even when there’s no other method with the same operationid in the same class.

This issue is easy to reproduce:

  1. Create two controllers classes.
  2. Create one method with the same name in both classes.
  3. http://yoursite/v3/api-docs will produce one operationid correct and the other will have the 1 suffix.

Ex: MyFirstClass.java:

    @Operation(operationId = "save")
    @PostMapping(path = "/")
    @ApiResponse(responseCode = "200", description = "Success")
    public ResponseEntity save(@RequestBody String request) {
      return null;
    }
MySecondClass.java:
    @Operation(operationId = "save")
    @PostMapping(path = "/")
    @ApiResponse(responseCode = "200", description = "Success")
    public ResponseEntity save(@RequestBody String request) {
      return null;
    }

After generation, one method will have the correct operationid (save), but the other one will have a wrong operationid(save_1) This bug is happening with SpringDoc 1.3.0, SpringBoot 2.2.6, openJDK 11 64bits on Windows.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
jdbranhamcommented, Aug 4, 2020

Nvm - I just realized there is an OperationCustomizer class while browsing the source code -

https://github.com/springdoc/springdoc-openapi/blob/2da39aae2976ef1cc888e5696caf763fb6f05fcf/springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java#L641

I was able to provide a bean and customize my generated names =]

Thanks for a great project!!

@Bean
public OperationCustomizer operationIdCustomizer() {
  OperationCustomizer c = new OperationCustomizer() {
    @Override
    public Operation customize(Operation operation, HandlerMethod handlerMethod) {
	Class<?> superClazz = handlerMethod.getBeanType().getSuperclass();
	if (Objects.nonNull(superClazz) && superClazz.isAssignableFrom(CrudController.class)) {
		String beanName = handlerMethod.getBeanType().getSimpleName();
		operation.setOperationId(String.format("%s_%s", beanName, handlerMethod.getMethod().getName()));
	}
	return operation;
    }
  };
  return c;
}
2reactions
tezinecommented, May 19, 2020

Hi @tezine,

This is not a bug, but a normal behaviour as we don’t want to generate incorrect spec:

  • The operationId should be unique
  • When it not the case (for example in case of polymorphism), springdoc generates a unique id

If you have two operations with the same id, your spec will be incorrect.

Please make sure you use the last stable version: v1.3.9 indeed.

Thank you @bnasslahsen for the quick response. It just think it doesn’t make any sense. For instance, I’m generating REST calls to Angular in typescript. In this situation, I have two calls:

MyFirstClassService.save() and MySecondClassService.save_1(). //It doesn't make sense. 
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to customize the value of operationId generated in api ...
I have configured my spring project using springfox 2.0. I am able to generate the open api spec with it. ... This operationId...
Read more >
Rules - Redocly
OpenAPI-generated documentation tool with 17000+ stars on Github - for ... rules: specific-api-rule: warn rules: example-rule-name: error.
Read more >
Path Operation Advanced Configuration - FastAPI
You can set the OpenAPI operationId to be used in your path operation with the ... To exclude a path operation from the...
Read more >
API Standardization | SwaggerHub Documentation
To view the error list, open an API in the SwaggerHub editor and check the Validation ... API documentation also uses operationId to...
Read more >
Links - Swagger
HTTP/1.1 201 Created; Content-Type: application/json ... operationId is used for local links only, and operationRef can link to both ... Found a mistake?...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found