Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support basePath in options-per-service for openapi generator - Option 2 #5247

Merged
merged 73 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 69 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
b83be1d
chore: append basePath to path pattern in openapi generator
KavithaSiva Dec 2, 2024
43576b8
chore: switch to using removeSlashes from util
KavithaSiva Dec 2, 2024
497f40c
chore: add additional test
KavithaSiva Dec 2, 2024
c203aa7
chore: check-public-api change
KavithaSiva Dec 2, 2024
4d72e6a
Changes from lint:fix
Dec 3, 2024
de355eb
chore: update test
KavithaSiva Dec 3, 2024
91c69f8
chore: adjust all tests
KavithaSiva Dec 3, 2024
84a7af8
chore: fix codeQL warning
KavithaSiva Dec 4, 2024
43142a9
Changes from lint:fix
Dec 4, 2024
e934945
chore: add changeset
KavithaSiva Dec 4, 2024
0764c6f
Merge branch 'feat/openapi-support-basePath-optionsPerService' of git…
KavithaSiva Dec 4, 2024
8e2dc85
chore: cleanup tests
KavithaSiva Dec 4, 2024
7773786
feat: extend openapi request builder for basePath
KavithaSiva Dec 6, 2024
cad7364
chore: introduce setBasePath
KavithaSiva Dec 6, 2024
31da305
feat: introduce _defaultBasePath to the generated files
KavithaSiva Dec 6, 2024
8917c18
chore: fix order of parameters with intro of basepath
KavithaSiva Dec 6, 2024
aa91a33
chore: fix tests
KavithaSiva Dec 6, 2024
e129c10
chore: add setBasePath
KavithaSiva Dec 6, 2024
e366dd7
chore: fix failing e2e test
KavithaSiva Dec 9, 2024
90fcb78
Changes from lint:fix
Dec 9, 2024
bd78a6a
chore: cleanup path
KavithaSiva Dec 9, 2024
82a1a51
Changes from lint:fix
Dec 9, 2024
4a0822b
chore: adjust test
KavithaSiva Dec 9, 2024
0d98293
Merge branch 'feat/openapi-support-setBasePath' of github.com:SAP/clo…
KavithaSiva Dec 9, 2024
52ca448
Changes from lint:fix
Dec 9, 2024
e66bf02
chore: switch to using undefined for default basePath value
KavithaSiva Dec 9, 2024
a37721d
Merge branch 'feat/openapi-support-setBasePath' of github.com:SAP/clo…
KavithaSiva Dec 9, 2024
1232651
chore: cleanup test
KavithaSiva Dec 9, 2024
333c21b
chore: update changeset
KavithaSiva Dec 9, 2024
f185dfa
chore: adjust changeset
KavithaSiva Dec 9, 2024
28b25cd
chore: fix unit test
KavithaSiva Dec 9, 2024
6ce5e67
chore:cleanup
KavithaSiva Dec 9, 2024
78dec6a
chore: fix test
KavithaSiva Dec 9, 2024
b77dca7
chore: cleanup changesets
KavithaSiva Dec 9, 2024
2df022c
Merge branch 'v3-main' into feat/openapi-support-setBasePath
jjtang1985 Dec 10, 2024
d46219b
fix: integration test
KavithaSiva Dec 10, 2024
52085ec
Merge branch 'feat/openapi-support-setBasePath' of github.com:SAP/clo…
KavithaSiva Dec 10, 2024
a5146d7
chore: fix path
KavithaSiva Dec 10, 2024
d4eac1f
chore:fix path
KavithaSiva Dec 10, 2024
f7e8de3
chore: remove dead code
KavithaSiva Dec 10, 2024
4d99146
Merge branch 'v3-main' into feat/openapi-support-setBasePath
KavithaSiva Dec 10, 2024
2245695
Merge branch 'v3-main' into feat/openapi-support-setBasePath
KavithaSiva Dec 11, 2024
b9242b4
chore: review comment
KavithaSiva Dec 12, 2024
620f793
Update .changeset/chilled-flowers-hide.md
KavithaSiva Dec 12, 2024
ac017c2
Update packages/openapi/src/openapi-request-builder.ts
KavithaSiva Dec 12, 2024
df3be1e
chore: address review comments
KavithaSiva Dec 12, 2024
804a763
chore: revert update to removeSlashes utility
KavithaSiva Dec 12, 2024
c5b6a60
Changes from lint:fix
Dec 12, 2024
2134353
chore: address review comments
KavithaSiva Dec 12, 2024
a5e74a3
chore: tests fix
KavithaSiva Dec 12, 2024
a710c7a
Merge branch 'feat/openapi-support-setBasePath' of github.com:SAP/clo…
KavithaSiva Dec 12, 2024
9e697fb
chore: adjust tests
KavithaSiva Dec 12, 2024
6a84e46
Merge branch 'v3-main' into feat/openapi-support-setBasePath
deekshas8 Dec 12, 2024
076f971
chore: address review comments
KavithaSiva Dec 13, 2024
ed82ff3
Changes from lint:fix
Dec 13, 2024
9780c93
Merge branch 'v3-main' into feat/openapi-support-setBasePath
KavithaSiva Dec 13, 2024
a6744ce
fix: begin all paths using '/'
KavithaSiva Dec 13, 2024
99ee4f2
chore: address review comments
KavithaSiva Dec 13, 2024
e29f3ab
Merge branch 'v3-main' into feat/openapi-support-setBasePath
KavithaSiva Dec 16, 2024
61597a7
Update .changeset/chilled-flowers-hide.md
KavithaSiva Dec 17, 2024
8261772
Update .changeset/chilled-flowers-freeze.md
KavithaSiva Dec 17, 2024
d5dbf49
Update packages/util/src/remove-slashes.spec.ts
KavithaSiva Dec 17, 2024
2ac2abe
Update packages/util/src/remove-slashes.spec.ts
KavithaSiva Dec 17, 2024
e9d0e03
chore: address review comments
KavithaSiva Dec 17, 2024
5f1e6da
Changes from lint:fix
Dec 17, 2024
252781b
chore: add review changes
KavithaSiva Dec 17, 2024
5ee129d
Merge branch 'feat/openapi-support-setBasePath' of github.com:SAP/clo…
KavithaSiva Dec 17, 2024
0ee7e6b
chore: address review comments
KavithaSiva Dec 17, 2024
1d35573
Merge branch 'v3-main' into feat/openapi-support-setBasePath
KavithaSiva Dec 17, 2024
fab2864
chore:address review comments
KavithaSiva Dec 19, 2024
14c20b7
Merge branch 'v3-main' into feat/openapi-support-setBasePath
KavithaSiva Dec 19, 2024
29b0abd
chore: address review comments
KavithaSiva Dec 19, 2024
b7af1d0
chore: release note fix
KavithaSiva Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-flowers-freeze.md
ZhongpinWang marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-cloud-sdk/openapi': minor
---

[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 request.
7 changes: 7 additions & 0 deletions .changeset/chilled-flowers-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sap-cloud-sdk/openapi-generator': minor
'@sap-cloud-sdk/openapi': minor
'@sap-cloud-sdk/util': minor
---

[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.
3 changes: 3 additions & 0 deletions .github/actions/check-public-api/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,36 @@ import type { QueryParameterType, RefType, ResponseType } from './schema/index.j
* This API is part of the 'MyServiceName' service.
*/
export const TestApi = {
_defaultBasePath: undefined,
/**
* Create a request builder for execution of get requests to the 'test/{id}' endpoint.
* Create a request builder for execution of get requests to the '/test/{id}' endpoint.
* @param id - Path parameter.
* @param queryParameters - Object containing the following keys: queryParam.
* @param headerParameters - Object containing the following keys: headerParam.
* @returns The request builder, use the \`execute()\` method to trigger the request.
*/
getFn: (id: string, queryParameters: {'queryParam': QueryParameterType}, headerParameters?: {'headerParam'?: string}) => new OpenApiRequestBuilder<string>(
'get',
"test/{id}",
"/test/{id}",
{
pathParameters: { id },
queryParameters,
headerParameters
}
},
TestApi._defaultBasePath
),
/**
* Create a request builder for execution of post requests to the 'test' endpoint.
* Create a request builder for execution of post requests to the '/test' endpoint.
* @param body - Request body.
* @returns The request builder, use the \`execute()\` method to trigger the request.
*/
createFn: (body: RefType) => new OpenApiRequestBuilder<ResponseType>(
'post',
"test",
"/test",
{
body
}
},
TestApi._defaultBasePath
)
};"
`;
Expand All @@ -46,13 +49,16 @@ exports[`api-file creates an api file with documentation 1`] = `
* This API is part of the 'TestService' service.
*/
export const TestApi = {
_defaultBasePath: undefined,
/**
* Create a request builder for execution of get requests to the 'test' endpoint.
* Create a request builder for execution of get requests to the '/test' endpoint.
* @returns The request builder, use the \`execute()\` method to trigger the request.
*/
getFn: () => new OpenApiRequestBuilder<any>(
'get',
"test"
KavithaSiva marked this conversation as resolved.
Show resolved Hide resolved
"/test",
{},
TestApi._defaultBasePath
)
};"
`;
Expand All @@ -72,33 +78,78 @@ import type { QueryParameterType, RefType, ResponseType } from './schema';
* This API is part of the 'MyServiceName' service.
*/
export const TestApi = {
_defaultBasePath: undefined,
/**
* Create a request builder for execution of get requests to the 'test/{id}' endpoint.
* Create a request builder for execution of get requests to the '/test/{id}' endpoint.
* @param id - Path parameter.
* @param queryParameters - Object containing the following keys: queryParam.
* @param headerParameters - Object containing the following keys: headerParam.
* @returns The request builder, use the \`execute()\` method to trigger the request.
*/
getFn: (id: string, queryParameters: {'queryParam': QueryParameterType}, headerParameters?: {'headerParam'?: string}) => new OpenApiRequestBuilder<string>(
'get',
"test/{id}",
"/test/{id}",
{
pathParameters: { id },
queryParameters,
headerParameters
}
},
TestApi._defaultBasePath
),
/**
* Create a request builder for execution of post requests to the 'test' endpoint.
* Create a request builder for execution of post requests to the '/test' endpoint.
* @param body - Request body.
* @returns The request builder, use the \`execute()\` method to trigger the request.
*/
createFn: (body: RefType) => new OpenApiRequestBuilder<ResponseType>(
'post',
"test",
"/test",
{
body
}
},
TestApi._defaultBasePath
)
};"
`;

exports[`api-file serializes api file with multiple operations and references and base path 1`] = `
"import { OpenApiRequestBuilder } from '@sap-cloud-sdk/openapi';
import type { QueryParameterType, RefType, ResponseType } from './schema';
/**
* Representation of the 'TestApi'.
* This API is part of the 'MyServiceName' service.
*/
export const TestApi = {
_defaultBasePath: '/base/path/to/service/',
/**
* Create a request builder for execution of get requests to the '/test/{id}' endpoint.
* @param id - Path parameter.
* @param queryParameters - Object containing the following keys: queryParam.
* @param headerParameters - Object containing the following keys: headerParam.
* @returns The request builder, use the \`execute()\` method to trigger the request.
*/
getFn: (id: string, queryParameters: {'queryParam': QueryParameterType}, headerParameters?: {'headerParam'?: string}) => new OpenApiRequestBuilder<string>(
'get',
"/test/{id}",
KavithaSiva marked this conversation as resolved.
Show resolved Hide resolved
{
pathParameters: { id },
queryParameters,
headerParameters
},
TestApi._defaultBasePath
),
/**
* Create a request builder for execution of post requests to the '/test' endpoint.
* @param body - Request body.
* @returns The request builder, use the \`execute()\` method to trigger the request.
*/
createFn: (body: RefType) => new OpenApiRequestBuilder<ResponseType>(
'post',
"/test",
{
body
},
TestApi._defaultBasePath
)
};"
`;
Expand All @@ -110,17 +161,19 @@ exports[`api-file serializes api file with one operation and no references 1`] =
* This API is part of the 'MyServiceName' service.
*/
export const TestApi = {
_defaultBasePath: undefined,
/**
* Create a request builder for execution of get requests to the 'test/{id}' endpoint.
* Create a request builder for execution of get requests to the '/test/{id}' endpoint.
* @param id - Path parameter.
* @returns The request builder, use the \`execute()\` method to trigger the request.
*/
getFn: (id: string) => new OpenApiRequestBuilder<any>(
'get',
"test/{id}",
"/test/{id}",
{
pathParameters: { id }
}
},
TestApi._defaultBasePath
)
};"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

exports[`serializeOperation serializes operation with path parameters inside () 1`] = `
"/**
* Create a request builder for execution of get requests to the 'test('{id}')' endpoint.
* Create a request builder for execution of get requests to the '/test('{id}')' endpoint.
* @param id - Path parameter.
* @returns The request builder, use the \`execute()\` method to trigger the request.
*/
getFn: (id: string) => new OpenApiRequestBuilder<Record<string, any>>(
'get',
"test('{id}')",
"/test('{id}')",
{
pathParameters: { id }
}
},
TestApi._defaultBasePath
)"
`;
19 changes: 15 additions & 4 deletions packages/openapi-generator/src/file-serializer/api-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const singleOperationApi: OpenApiApi = {
headerParameters: [],
response: { type: 'any' },
responses: { 200: { description: 'some response description' } },
pathPattern: 'test/{id}'
pathPattern: '/test/{id}'
}
]
};
Expand Down Expand Up @@ -71,7 +71,7 @@ const multipleOperationApi: OpenApiApi = {
schemaProperties: {}
}
],
pathPattern: 'test/{id}',
pathPattern: '/test/{id}',
response: { type: 'string' }
},
{
Expand All @@ -89,7 +89,7 @@ const multipleOperationApi: OpenApiApi = {
schemaName: 'RefType'
} as OpenApiReferenceSchema
},
pathPattern: 'test',
pathPattern: '/test',
response: {
$ref: '#/components/schemas/ResponseType',
schemaName: 'ResponseType'
Expand All @@ -110,7 +110,7 @@ const docsApi: OpenApiApi = {
queryParameters: [],
headerParameters: [],
response: { type: 'any' },
pathPattern: 'test'
pathPattern: '/test'
}
]
};
Expand All @@ -124,6 +124,17 @@ describe('api-file', () => {
expect(apiFile(multipleOperationApi, 'MyServiceName')).toMatchSnapshot();
});

it('serializes api file with multiple operations and references and base path', () => {
expect(
apiFile(
multipleOperationApi,
'MyServiceName',
undefined,
'/base/path/to/service/'
KavithaSiva marked this conversation as resolved.
Show resolved Hide resolved
)
).toMatchSnapshot();
});

it('creates an api file with documentation', () => {
expect(apiFile(docsApi, 'TestService')).toMatchSnapshot();
});
Expand Down
9 changes: 6 additions & 3 deletions packages/openapi-generator/src/file-serializer/api-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@ import type {
* Serialize an API representation to a string representing the resulting API file.
* @param api - Representation of an API.
* @param serviceName - Service name for which the API is created.
* @param options - Options to configure the file creation.
* @param basePath - Custom base path for the API.
* @returns The serialized API file contents.
* @internal
*/
export function apiFile(
api: OpenApiApi,
serviceName: string,
options?: CreateFileOptions
options?: CreateFileOptions,
basePath?: string
): string {
const imports = serializeImports(getImports(api, options));
const apiDoc = apiDocumentation(api, serviceName);
const apiContent = codeBlock`
export const ${api.name} = {
${api.operations.map(operation => serializeOperation(operation)).join(',\n')}
_defaultBasePath: ${basePath ? `'${basePath}'` : undefined},
${api.operations.map(operation => serializeOperation(operation, api.name)).join(',\n')}
};
`;

return [imports, apiDoc, apiContent].join(unixEOL);
}

/**
* Get the unique reference schemas for all request body types in the given operation list.
* @param operations - The given operation list.
Expand Down
Loading
Loading