springdoc-openapi icon indicating copy to clipboard operation
springdoc-openapi copied to clipboard

The operationId is unnecessarily deduplicated for a requestBody with multiple content types

Open scrhartley opened this issue 1 year ago • 3 comments

Describe the bug

In a Spring Boot RestController that wants to support POST request bodies of both form data or JSON, to use the built-in functionality, multiple endpoint methods must be used to support both content types. The JSON accepting method expects the Spring MVC @RequestBody annotation to be used, while the form data version expects Spring MVC @RequestBody to not be used, so the Swagger annotation must be used instead.

Although the output OpenAPI JSON correctly puts the two as the same request body with different supported content, the operationId is incorrectly deduplicated, changing its value. n.b. This does require the operationId to be set explicitly on both methods, because otherwise the last automatically generated operationId will be picked to represent both methods.

To Reproduce

  • What version of spring-boot you are using? 3.3.1
  • What modules and versions of springdoc-openapi are you using? springdoc-openapi-starter-webmvc-api version 2.6.0, creating OpenAPI version 3.0.1 JSON.
  • What is the actual and the expected result using OpenAPI Description (yml or json)? Actual (snippet):
"post": {
    "tags": [
        "books"
    ],
    "operationId": "create_1_1",
    "requestBody": {
        "content": {
            "application/x-www-form-urlencoded": {
                "schema": {
                    "$ref": "#/components/schemas/Book"
                }
            },
            "application/json": {
                "schema": {
                    "$ref": "#/components/schemas/Book"
                }
            }
        },
        "required": true
    },
    "responses": {
        "200": {
            "description": "OK"
        }
    }
}

Expected the same except:

    "operationId": "create",
  • Provide with a sample code (HelloController) or Test that reproduces the problem
@RestController
@RequestMapping("books")
public class DemoController {

    @PostMapping(value = "/book", consumes = APPLICATION_JSON_VALUE)
    @Operation(operationId = "create", tags = "books")
    public void createBookFromJson(
            @org.springframework.web.bind.annotation.RequestBody Book book) {
        System.out.println("creating book: " + book);
    }

    @PostMapping(value = "/book", consumes = APPLICATION_FORM_URLENCODED_VALUE)
    @Operation(operationId = "create", tags = "books")
    public void createBookFromFormData(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true) Book book) {
        createBookFromJson(book);
    }

    record Book(@NotNull String title, @NotNull String author) {}

}

Expected behavior

  • When the different content types are merged for a single requestBody, this should not trigger deduplication and the originally specified operationId should be used.

scrhartley avatar Jul 14 '24 15:07 scrhartley

Hello, The same apply for multiple response content type.

grimly avatar Jul 17 '24 16:07 grimly

Here is a customizer that can circumvent the issue until resolved but then, be careful, it removes any deduplication capabilities:

@Component
public class PreserveOperationIdOpenApiCustomizer implements GlobalOperationCustomizer {
  @Override
  public Operation customize(Operation operation, HandlerMethod handlerMethod) {
    operation.setOperationId(
      Optional
        .ofNullable(handlerMethod.getMethodAnnotation(io.swagger.v3.oas.annotations.Operation.class))
        .map(io.swagger.v3.oas.annotations.Operation::operationId)
        .orElseGet(handlerMethod.getMethod()::getName)
    );
    return operation;
  }
}

grimly avatar Jul 17 '24 17:07 grimly

I ended up with a less aggressive customizer:

@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
    Optional
        .ofNullable(handlerMethod.getMethodAnnotation(io.swagger.v3.oas.annotations.Operation.class))
        .map(io.swagger.v3.oas.annotations.Operation::operationId)
        .ifPresent(operation::setOperationId);
    return operation;
}

scrhartley avatar Aug 28 '24 17:08 scrhartley

Thank you, I'll look forward to the next release.

scrhartley avatar Sep 22 '24 14:09 scrhartley