Search code examples
javaspring-bootcode-generationopenapi

How to get OpenApi Generator for Spring Java server generate ResponseEntity<Object> for PUT request?


I am using the 4.3.1 version of the openapi-generator-maven-plugin to generate a SpringBoot server in Java 11.

For a PUT request I want to be able to return the URI to the created/updated object when successful, and plain text with information about the problem when unsuccessful.

My json for the API has the following content for the PUT request:

"put": {
        "summary": "Create or update a Service",
        "deprecated": false,
        "operationId": "putIndividualServiceUsingPUT",
        "responses": {
          "200": {
            "description": "Service updated"
          },
          "201": {
            "description": "Service created",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string",
                  "example": "services/DroneIdentifier"
                }
              }
            }
          },
          "400": {
            "description": "Provided service is not correct",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string",
                  "example": "Service is missing required property version"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized"
          },
          "403": {
            "description": "Forbidden"
          }
        },
        "parameters": [
          {
            "name": "serviceName",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "example": "DroneIdentifier"
          }
        ],
        "requestBody": {
          "description": "Service to create/update",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/service"
              }
            }
          }
        }

The generated API:

    /**
     * PUT /services/{serviceName} : Create or update a Service
     *
     * @param serviceName  (required)
     * @param service Service to create/update (required)
     * @return Service updated (status code 200)
     *         or Service created (status code 201)
     *         or Provided service is not correct (status code 400)
     *         or Unauthorized (status code 401)
     *         or Forbidden (status code 403)
     */
    @ApiOperation(value = "Create or update a Service", nickname = "putIndividualServiceUsingPUT", notes = "", tags={ "rAPP Catalogue API", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Service updated"),
        @ApiResponse(code = 201, message = "Service created", response = String.class),
        @ApiResponse(code = 400, message = "Provided service is not correct", response = String.class),
        @ApiResponse(code = 401, message = "Unauthorized"),
        @ApiResponse(code = 403, message = "Forbidden") })
    @RequestMapping(value = "/services/{serviceName}",
        produces = { "text/plain" }, 
        consumes = { "application/json" },
        method = RequestMethod.PUT)
    default ResponseEntity<Void> putIndividualServiceUsingPUT(@ApiParam(value = "",required=true) @PathVariable("serviceName") String serviceName,@ApiParam(value = "Service to create/update" ,required=true )  @Valid @RequestBody Service service) {
        return getDelegate().putIndividualServiceUsingPUT(serviceName, service);
    }

However, the return type of the method is ResponseEntity<Void>, which means that I am not able to put anything in the body of the response.

Am I doing something wrong? Or is the generator hard coded to not permit a body in the response of a PUT request?


Solution

  • Generated code templates are stored as resources in the .mustache format. But you can easily overwrite it by creating a modified file with the same name and adding a link to the folder if you have Maven or Gradle.

    In your case, copy the api.mustache and methodBody.mustache files to your computer or project resources from https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/Java, and change them by replacing Response<> with a URI.

    Add the item <templateResourcePath>folderInYourResources</templateResourcePath> or <templateDirectory>pathToDirectory</templateDirectory> into your Maven <configuration> and everything should work