Skip to content

Commit 936a6eb

Browse files
authored
feat: Support basePath in options-per-service for openapi generator - Option 2 (#5247) (#5311)
1 parent c0bbb50 commit 936a6eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+561
-215
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sap-cloud-sdk/openapi': minor
3+
---
4+
5+
[New Functionality] Introduce `setBasePath()` method on the OpenAPI request builder, allowing a custom base path URL to be set for a single request. This base path is prepended to the API path parameter for that single request.

.changeset/chilled-flowers-hide.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@sap-cloud-sdk/openapi-generator': minor
3+
'@sap-cloud-sdk/openapi': minor
4+
'@sap-cloud-sdk/util': minor
5+
---
6+
7+
[New Functionality] Add `basePath` option in the `options-per-service.json` file in the OpenAPI generator. This option prepends the base URL path to the API path parameter for every request.

.github/actions/check-public-api/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-generator/src/file-serializer/__snapshots__/api-file.spec.ts.snap

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,36 @@ import type { QueryParameterType, RefType, ResponseType } from './schema/index.j
88
* This API is part of the 'MyServiceName' service.
99
*/
1010
export const TestApi = {
11+
_defaultBasePath: undefined,
1112
/**
12-
* Create a request builder for execution of get requests to the 'test/{id}' endpoint.
13+
* Create a request builder for execution of get requests to the '/test/{id}' endpoint.
1314
* @param id - Path parameter.
1415
* @param queryParameters - Object containing the following keys: queryParam.
1516
* @param headerParameters - Object containing the following keys: headerParam.
1617
* @returns The request builder, use the \`execute()\` method to trigger the request.
1718
*/
1819
getFn: (id: string, queryParameters: {'queryParam': QueryParameterType}, headerParameters?: {'headerParam'?: string}) => new OpenApiRequestBuilder<string>(
1920
'get',
20-
"test/{id}",
21+
"/test/{id}",
2122
{
2223
pathParameters: { id },
2324
queryParameters,
2425
headerParameters
25-
}
26+
},
27+
TestApi._defaultBasePath
2628
),
2729
/**
28-
* Create a request builder for execution of post requests to the 'test' endpoint.
30+
* Create a request builder for execution of post requests to the '/test' endpoint.
2931
* @param body - Request body.
3032
* @returns The request builder, use the \`execute()\` method to trigger the request.
3133
*/
3234
createFn: (body: RefType) => new OpenApiRequestBuilder<ResponseType>(
3335
'post',
34-
"test",
36+
"/test",
3537
{
3638
body
37-
}
39+
},
40+
TestApi._defaultBasePath
3841
)
3942
};"
4043
`;
@@ -46,13 +49,16 @@ exports[`api-file creates an api file with documentation 1`] = `
4649
* This API is part of the 'TestService' service.
4750
*/
4851
export const TestApi = {
52+
_defaultBasePath: undefined,
4953
/**
50-
* Create a request builder for execution of get requests to the 'test' endpoint.
54+
* Create a request builder for execution of get requests to the '/test' endpoint.
5155
* @returns The request builder, use the \`execute()\` method to trigger the request.
5256
*/
5357
getFn: () => new OpenApiRequestBuilder<any>(
5458
'get',
55-
"test"
59+
"/test",
60+
{},
61+
TestApi._defaultBasePath
5662
)
5763
};"
5864
`;
@@ -72,33 +78,78 @@ import type { QueryParameterType, RefType, ResponseType } from './schema';
7278
* This API is part of the 'MyServiceName' service.
7379
*/
7480
export const TestApi = {
81+
_defaultBasePath: undefined,
7582
/**
76-
* Create a request builder for execution of get requests to the 'test/{id}' endpoint.
83+
* Create a request builder for execution of get requests to the '/test/{id}' endpoint.
7784
* @param id - Path parameter.
7885
* @param queryParameters - Object containing the following keys: queryParam.
7986
* @param headerParameters - Object containing the following keys: headerParam.
8087
* @returns The request builder, use the \`execute()\` method to trigger the request.
8188
*/
8289
getFn: (id: string, queryParameters: {'queryParam': QueryParameterType}, headerParameters?: {'headerParam'?: string}) => new OpenApiRequestBuilder<string>(
8390
'get',
84-
"test/{id}",
91+
"/test/{id}",
8592
{
8693
pathParameters: { id },
8794
queryParameters,
8895
headerParameters
89-
}
96+
},
97+
TestApi._defaultBasePath
9098
),
9199
/**
92-
* Create a request builder for execution of post requests to the 'test' endpoint.
100+
* Create a request builder for execution of post requests to the '/test' endpoint.
93101
* @param body - Request body.
94102
* @returns The request builder, use the \`execute()\` method to trigger the request.
95103
*/
96104
createFn: (body: RefType) => new OpenApiRequestBuilder<ResponseType>(
97105
'post',
98-
"test",
106+
"/test",
99107
{
100108
body
101-
}
109+
},
110+
TestApi._defaultBasePath
111+
)
112+
};"
113+
`;
114+
115+
exports[`api-file serializes api file with multiple operations and references and base path 1`] = `
116+
"import { OpenApiRequestBuilder } from '@sap-cloud-sdk/openapi';
117+
import type { QueryParameterType, RefType, ResponseType } from './schema';
118+
/**
119+
* Representation of the 'TestApi'.
120+
* This API is part of the 'MyServiceName' service.
121+
*/
122+
export const TestApi = {
123+
_defaultBasePath: '/base/path/to/service',
124+
/**
125+
* Create a request builder for execution of get requests to the '/test/{id}' endpoint.
126+
* @param id - Path parameter.
127+
* @param queryParameters - Object containing the following keys: queryParam.
128+
* @param headerParameters - Object containing the following keys: headerParam.
129+
* @returns The request builder, use the \`execute()\` method to trigger the request.
130+
*/
131+
getFn: (id: string, queryParameters: {'queryParam': QueryParameterType}, headerParameters?: {'headerParam'?: string}) => new OpenApiRequestBuilder<string>(
132+
'get',
133+
"/test/{id}",
134+
{
135+
pathParameters: { id },
136+
queryParameters,
137+
headerParameters
138+
},
139+
TestApi._defaultBasePath
140+
),
141+
/**
142+
* Create a request builder for execution of post requests to the '/test' endpoint.
143+
* @param body - Request body.
144+
* @returns The request builder, use the \`execute()\` method to trigger the request.
145+
*/
146+
createFn: (body: RefType) => new OpenApiRequestBuilder<ResponseType>(
147+
'post',
148+
"/test",
149+
{
150+
body
151+
},
152+
TestApi._defaultBasePath
102153
)
103154
};"
104155
`;
@@ -110,17 +161,19 @@ exports[`api-file serializes api file with one operation and no references 1`] =
110161
* This API is part of the 'MyServiceName' service.
111162
*/
112163
export const TestApi = {
164+
_defaultBasePath: undefined,
113165
/**
114-
* Create a request builder for execution of get requests to the 'test/{id}' endpoint.
166+
* Create a request builder for execution of get requests to the '/test/{id}' endpoint.
115167
* @param id - Path parameter.
116168
* @returns The request builder, use the \`execute()\` method to trigger the request.
117169
*/
118170
getFn: (id: string) => new OpenApiRequestBuilder<any>(
119171
'get',
120-
"test/{id}",
172+
"/test/{id}",
121173
{
122174
pathParameters: { id }
123-
}
175+
},
176+
TestApi._defaultBasePath
124177
)
125178
};"
126179
`;

packages/openapi-generator/src/file-serializer/__snapshots__/operation.spec.ts.snap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
exports[`serializeOperation serializes operation with path parameters inside () 1`] = `
44
"/**
5-
* Create a request builder for execution of get requests to the 'test('{id}')' endpoint.
5+
* Create a request builder for execution of get requests to the '/test('{id}')' endpoint.
66
* @param id - Path parameter.
77
* @returns The request builder, use the \`execute()\` method to trigger the request.
88
*/
99
getFn: (id: string) => new OpenApiRequestBuilder<Record<string, any>>(
1010
'get',
11-
"test('{id}')",
11+
"/test('{id}')",
1212
{
1313
pathParameters: { id }
14-
}
14+
},
15+
TestApi._defaultBasePath
1516
)"
1617
`;

packages/openapi-generator/src/file-serializer/api-file.spec.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const singleOperationApi: OpenApiApi = {
2323
headerParameters: [],
2424
response: { type: 'any' },
2525
responses: { 200: { description: 'some response description' } },
26-
pathPattern: 'test/{id}'
26+
pathPattern: '/test/{id}'
2727
}
2828
]
2929
};
@@ -71,7 +71,7 @@ const multipleOperationApi: OpenApiApi = {
7171
schemaProperties: {}
7272
}
7373
],
74-
pathPattern: 'test/{id}',
74+
pathPattern: '/test/{id}',
7575
response: { type: 'string' }
7676
},
7777
{
@@ -89,7 +89,7 @@ const multipleOperationApi: OpenApiApi = {
8989
schemaName: 'RefType'
9090
} as OpenApiReferenceSchema
9191
},
92-
pathPattern: 'test',
92+
pathPattern: '/test',
9393
response: {
9494
$ref: '#/components/schemas/ResponseType',
9595
schemaName: 'ResponseType'
@@ -110,7 +110,7 @@ const docsApi: OpenApiApi = {
110110
queryParameters: [],
111111
headerParameters: [],
112112
response: { type: 'any' },
113-
pathPattern: 'test'
113+
pathPattern: '/test'
114114
}
115115
]
116116
};
@@ -124,6 +124,17 @@ describe('api-file', () => {
124124
expect(apiFile(multipleOperationApi, 'MyServiceName')).toMatchSnapshot();
125125
});
126126

127+
it('serializes api file with multiple operations and references and base path', () => {
128+
expect(
129+
apiFile(
130+
multipleOperationApi,
131+
'MyServiceName',
132+
undefined,
133+
'/base/path/to/service'
134+
)
135+
).toMatchSnapshot();
136+
});
137+
127138
it('creates an api file with documentation', () => {
128139
expect(apiFile(docsApi, 'TestService')).toMatchSnapshot();
129140
});

packages/openapi-generator/src/file-serializer/api-file.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,28 @@ import type {
1616
* Serialize an API representation to a string representing the resulting API file.
1717
* @param api - Representation of an API.
1818
* @param serviceName - Service name for which the API is created.
19+
* @param options - Options to configure the file creation.
20+
* @param basePath - Custom base path for the API.
1921
* @returns The serialized API file contents.
2022
* @internal
2123
*/
2224
export function apiFile(
2325
api: OpenApiApi,
2426
serviceName: string,
25-
options?: CreateFileOptions
27+
options?: CreateFileOptions,
28+
basePath?: string
2629
): string {
2730
const imports = serializeImports(getImports(api, options));
2831
const apiDoc = apiDocumentation(api, serviceName);
2932
const apiContent = codeBlock`
3033
export const ${api.name} = {
31-
${api.operations.map(operation => serializeOperation(operation)).join(',\n')}
34+
_defaultBasePath: ${basePath ? `'${basePath}'` : undefined},
35+
${api.operations.map(operation => serializeOperation(operation, api.name)).join(',\n')}
3236
};
3337
`;
3438

3539
return [imports, apiDoc, apiContent].join(unixEOL);
3640
}
37-
3841
/**
3942
* Get the unique reference schemas for all request body types in the given operation list.
4043
* @param operations - The given operation list.

0 commit comments

Comments
 (0)