Skip to content

SpringdocRouteBuilder.onError is overriding last route defined #2399

@ddeath

Description

@ddeath

Describe the bug
When you want to provide global error handler for routes which are created "programatically via api" (SpringdocRouteBuilder) we are using SpringdocRouteBuilder.onError()

The issue is that it requires us to provide operationsConsumer. First of all I dont understand why we have to provide it, what is reasoning behind that, but second of all it is not use properly.
As we can see here: https://github.com/springdoc/springdoc-openapi/blob/main/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/fn/SpringdocRouteBuilder.java#L675 operationConsumer is provided to function withAttribute()

Now from spring doc:
onError - Filters all exceptions of the given type by applying the given response provider function.
withAttribute - Add an attribute with the given name and value to the last route built with this builder.

So with onError we are defining global behaviour but since withAttribute is applied to LAST created route this looks like unwanted behaviour.

Example:

var routeBuilder = SpringdocRouteBuilder.route();
routeBuilder.POST(
         "some-path-A",
         request -> {
           // do something
         },
         operation ->
             operation
                 .operationId("OPERATION_A"));

routeBuilder.POST(
         "some-path-B",
         request -> {
           // do something
         },
         operation ->
             operation
                 .operationId("OPERATION_B"));

routeBuilder.onError(
         exceptionHandler.getHandledExceptionType(),
         exceptionHandler,
         operation -> operation..operationId("ERROR_RESPONSE").response(responseBuilder().implementation(WebhookError.class)));

Since we are forced to provide that operationConsumer with onError() method the generated openapi spect will be something like:

"/some-path-A":
    operationId: OPERATION_A

"/some-path-B":
    operationId: ERROR_RESPONSE

So some-path-B will get override with ERROR_RESPONSE which is imho unwanted behaviour.

My suggestion is to remove call of withAttributes() from onError()

To Reproduce
See example above

Expected behavior

  • A way how to programatically define GLOBAL error handlers which will not override routes definitions

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions