OpenAPITools / OpenAPITools/openapi-generator
RestTemplate generator should not require absolute base URL
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
I have found an issue when working with Spring's RestTemplate generator. I can't create a fully working example at the moment because copy&paste is forbidden outside my customer's virtual development environment.
I have generated RestTemplate clients after Red Hat PAM's JSON swagger file. This allows me to generate a bunch of APIs.
The Swagger file contains a base path that is a relative URL: /services/rest and not the full URL, which is something great
I had created a @Configuration bean that defines the RestTemplate used by ApiClient. It must work multi-environment, as the URL is injected by environment properties.
I have found an issue when setting the base URL
@Configuration
public class PamClientConfiguration{
@Value(....)
private String baseUrl;
@Bean public RestTemplate pamRestTemplate(RestTemplateBuilder restTemplateBuilder){
return restTemplateBuilder
.rootUri(baseUrl)
......
// set authentication, logging, etc
.build();
}
@Bean public ApiClient apiClient(RestTemplate pamRestTemplate){
ApiClient ret = new ApiClient(pamRestTemplate);
//ret.setBasePath(baseUrl + apiClient.getBasePath())
return ret;
}
}
The problem is that I can't use RestTemplate's great feature rootUri with OAG beucase on every API call it will throw an exception that the URI is not a valid URI.
I have identified the possible issue in the following line
In my opinion, it should be fromUriString, so that it returns the URI relative string. After that, RestTemplate is likely to add the prefix.
There is a workaround, and in this case is commenting the rootUri of the RestTemplate (with side effects when invoking PAM APIs dynamically), and adding the absolute hostname with ApiClient.setBasePath(baseUrl + apiClient.getBasePath()).
I would like to discuss the opportunity to change that piece of code without creating regressions.
Many thanks
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache around line 690 and inspect how the generated client parses its base path. Reproduce generation with a relative /services/rest base path and a RestTemplate configured with rootUri; done means the generated client accepts the relative path without invalid-URI errors while preserving absolute-URL behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100