Skip to content
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

SpringdocRouteBuilder.onError is overriding last route defined #2399

Closed
ddeath opened this issue Oct 12, 2023 · 0 comments
Closed

SpringdocRouteBuilder.onError is overriding last route defined #2399

ddeath opened this issue Oct 12, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@ddeath
Copy link

ddeath commented Oct 12, 2023

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
@ddeath ddeath changed the title SpringdocRouteBuilder.onError should not accept operationsConsumer SpringdocRouteBuilder.onError is overriding last route defined Oct 12, 2023
@bnasslahsen bnasslahsen added the enhancement New feature or request label Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants