OpenAPITools / OpenAPITools/openapi-generator

[REQ] In vertx-web template the response send is always json, need a mechanism to override the behaviour without changing the handler definition

Open
#9,725 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

The api handler mustache https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaVertXWebServer/apiHandler.mustache, has a code

 .onSuccess(apiResponse -> {
                routingContext.response().setStatusCode(apiResponse.getStatusCode());
                if (apiResponse.hasData()) {
                    routingContext.json(apiResponse.getData());
                } else {
                    routingContext.response().end();
                }
            })

Thus the response is sent always as json to send let's say binary, user has to modify the generated handler code.

Let's modify the ApiResponse mustache https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaVertXWebServer/supportFiles/ApiResponse.mustache as to include a function

    public void sendResponse(RoutingContext routingContext){
        if (this.hasData()) {
            routingContext.json(this.getData());
        } else {
            routingContext.response().end();
        }
    }

and use this function in the api handler mustache as

.onSuccess(apiResponse -> {
                routingContext.response().setStatusCode(apiResponse.getStatusCode());
                apiResponse.sendResponse(routingContext);
            })

This way user can simply override the function within ApiResponse as

                ApiResponse<String> apiResponse = new ApiResponse<String>(OK.code(),binaryString){
                    @Override
                    public void sendResponse(RoutingContext routingContext) {
                        routingContext.response().send(this.getData());
                    }
                };

I have done this and can raise PR if community feels can be used . Please suggest

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read modules/openapi-generator/src/main/resources/JavaVertXWebServer/apiHandler.mustache and its supportFiles/ApiResponse.mustache counterpart to trace how successful responses are sent. Verify that the generated Vert.x response path can be customized without editing the handler definition, and confirm that existing JSON and empty responses still behave as before.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.