Skip to content

Commit

Permalink
feat: Support basePath in options-per-service for openapi generator…
Browse files Browse the repository at this point in the history
… - Option 2 (#5247) (#5311)
  • Loading branch information
KavithaSiva authored Dec 27, 2024
1 parent c0bbb50 commit 936a6eb
Show file tree
Hide file tree
Showing 54 changed files with 561 additions and 215 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-flowers-freeze.md
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 single 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"
"/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}",
{
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'
)
).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

0 comments on commit 936a6eb

Please sign in to comment.