0

I am using openapi-generator-maven-plugin to generate an api client from an existing yaml specification file in a Java and SpringBoot project .

The API endpoint are protected by Basic HTTP Security scheme (username and password) this way :

securitySchemes:
    BasicAuth:
        type: http
        scheme: basic

The generated client (UsersApi in my case) comes with an ApiClient class which will use a RestTemplate to perform all the REST calls.

Is there a way to pass the credentials to ApiClient so I will be able to reach the external API.

Ghassen
  • 591
  • 1
  • 15
  • 33
  • Please describe which language and generator you are using. That info is necessary for someone to be able to answer you. – spacether Feb 27 '23 at 02:13

1 Answers1

0

The solution that I found is to inject 2 beans of type UsersApi and ApiClient this way :

@Configuration
public class ApiClientCustomConfig {

    @Value("${api.credentials.username}")
    private String username;

    @Value("${api.credentials.password}")
    private String password;


    @Bean
    @Qualifier("usersApi")
    public UsersApi usersApi(RestTemplate restTemplate) {
        return new UsersApi(apiClient(restTemplate));
    }

    @Bean
    public ApiClient apiClient(RestTemplate restTemplate) {
        ApiClient apiClient = new ApiClient(restTemplate);
        apiClient.setUsername(username);
        apiClient.setPassword(password);
        return apiClient;
    }
}
Ghassen
  • 591
  • 1
  • 15
  • 33