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.
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;
}
}