Search code examples
javaspring-bootopenapi-generator

Open api generated client fails when calling PATCH endpoint


I am using openapi-generator to generate client and server code. I have generated a patch endpoint with the following logic:

/renting-interested-persons/v1/interested-persons:
      patch:
  description: Change status on interested persons
  operationId: changeStatusOnPersons
  tags:
    - renting
  requestBody:
    required: true
    content:
      application/json:
        schema:
          $ref: '#/components/schemas/ChangeStatusDTO'
  responses:
    204:
      description: Status of interested persons is changed

The endpoint is generated and tested and it's working. Now I am trying to call this endpoint from another microservice using the client generated code and for every other endpoint is ok except for this which is a PATCH.

This is the way that I am creating a client:

private ApiClient getMyApiClient() {
    ApiClient apiClient = ApiClient();
    apiClient.setDebugging(true);
    if (Boolean.TRUE.equals(addToken)) {
        final String token = tokenProvider.getTokenFromAzure();
        apiClient.setBearerToken(token);
    }
    apiClient.setBasePath(MyBaseUrl);
    return apiClient;
}

And after that just getting this client and calling the endpoint:

getInterestedPersonApi().changeStatusOnPersons(changeStatusDTO);

The exception that I am getting is:

Method threw 'org.springframework.web.client.ResourceAccessException' exception.
I/O error on PATCH request for "http://localhost:8083/my-endpoint": Invalid HTTP method: PATCH

Solution

  • Updating the ApiClient did the trick:

    private ApiClient getMyApiClient() {
        ApiClient apiClient = new ApiClient(getRestTemplate());
        apiClient.setDebugging(true);
        if (Boolean.TRUE.equals(addToken)) {
            final String token = tokenProvider.getTokenFromAzure();
            apiClient.setBearerToken(token);
        }
        apiClient.setBasePath(myBaseUrl);
        return apiClient;
    }
    
    private RestTemplate getRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        restTemplate.setRequestFactory(requestFactory);
        return restTemplate;
    }