[BUG] relative path is prefixed with spec-url
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [ ] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When you generate with specs from URL the url is prefixed to the relative path from spec. From #10697 i would assume that a relative server-url is used as basePath in generated code.
openapi-generator version
5.4.0 5.3.1
OpenAPI declaration file content or url
https://petstore3.swagger.io/api/v3/openapi.json
Generation Details
The generator is used from npms @openapitools/openapi-generator-cli version 2.4.26
Via openapi-generator-cli version-manager list I choosed the versions mentioned above - same result.
Steps to reproduce
openapi-generator-cli generate -g typescript-angular -o /tmp/api -i https://petstore3.swagger.io/api/v3/openapi.json
The file /tmp/api/api/pet.service.ts looks as following:
...
export class PetService {
protected basePath = 'https://petstore3.swagger.io/api/v3';
...
expected is
...
export class PetService {
protected basePath = '/api/v3';
...
as "servers":[{"url":"/api/v3"} suggests
Related issues/PRs
#10057
Suggest a fix
Take the servers-url as-is for generating the basePath
As workaround one can do the following:
wget -qO /tmp/swagger.json https://petstore3.swagger.io/api/v3/openapi.json && openapi-generator-cli generate -g typescript-angular -o tmp/api -i /tmp/swagger.json
/tmp/api/api/pet.service.ts looks like
...
export class PetService {
protected basePath = '/api/v3';
...
@smoebody This workaround does not work for me.
The issue in my case is that the server URL is /, i.e. a single slash.
@smoebody This workaround does not work for me.
The issue in my case is that the server URL is
/, i.e. a single slash.
Same issue for me. I have fixed it by using an empty string as value:
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideHttpClient(),
{
provide: BASE_PATH,
useValue: '',
},
],
};
Error is in the class org.openapitools.codegen.utils.URLPathUtils in the method isRelativeUrl added in last release.
I suggest to change it:
from:
public static boolean isRelativeUrl(List<Server> servers) {
if (servers != null && servers.size() > 0) {
final Server firstServer = servers.get(0);
return Pattern.matches("^(\\/[\\w\\d]+)+", firstServer.getUrl());
}
return false;
}
to
public static boolean isRelativeUrl(List<Server> servers) {
if (servers != null && ! servers.isEmpty()) {
final Server firstServer = servers.get(0);
return StringUtils.isNoneBlank(firstServer.getUrl()) && firstServer.getUrl().startsWith("/");
}
return false;
}
Actually this method works wrong for path with example '-' character. IMHO regular expression is to complex.