Skip to content

Commit

Permalink
fix: Fix quote issue in path pattern (#4634)
Browse files Browse the repository at this point in the history
* fix quote issue in path

* Changes from lint:fix

* changelog

* fix test

* fix test 2

* fix test 3

---------

Co-authored-by: cloud-sdk-js <cloud-sdk-js@github.com>
  • Loading branch information
deekshas8 and cloud-sdk-js committed Apr 15, 2024
1 parent 8c37487 commit b4bc9ad
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-cats-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sap-cloud-sdk/openapi-generator": minor
---

[Fixed Issue] Fix serialization of path params that contain quotes to avoid syntax error and failing client generation
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`serializeOperation serializes operation with path parameters inside () 1`] = `
"/**
* 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}')",
{
pathParameters: { id }
}
)"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('apiFile', () => {
*/
getFn: (id: string) => new OpenApiRequestBuilder<any>(
'get',
'test/{id}',
"test/{id}",
{
pathParameters: { id }
}
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('apiFile', () => {
*/
getFn: (id: string, queryParameters: {'queryParam': QueryParameterType}) => new OpenApiRequestBuilder<string>(
'get',
'test/{id}',
"test/{id}",
{
pathParameters: { id },
queryParameters
Expand All @@ -139,7 +139,7 @@ describe('apiFile', () => {
*/
createFn: (body: RefType) => new OpenApiRequestBuilder<ResponseType>(
'post',
'test',
"test",
{
body
}
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('apiFile', () => {
*/
getFn: () => new OpenApiRequestBuilder<any>(
'get',
'test'
"test"
)
};"
`);
Expand Down
37 changes: 32 additions & 5 deletions packages/openapi-generator/src/file-serializer/operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('serializeOperation', () => {
*/
getFn: (id: string, subId: string, queryParameters?: {'limit'?: number}) => new OpenApiRequestBuilder<string>(
'get',
'test/{id}/{subId}',
"test/{id}/{subId}",
{
pathParameters: { id, subId },
queryParameters
Expand Down Expand Up @@ -93,14 +93,41 @@ describe('serializeOperation', () => {
*/
deleteFn: (id: string) => new OpenApiRequestBuilder<Record<string, any>>(
'delete',
'test/{id}',
"test/{id}",
{
pathParameters: { id }
}
)"
`);
});

it('serializes operation with path parameters inside ()', () => {
const operation: OpenApiOperation = {
operationId: 'getFn',
method: 'get',
tags: [],
pathParameters: [
{
in: 'path',
name: 'id',
originalName: 'id',
schema: { type: 'string' },
required: true,
schemaProperties: {}
}
],
queryParameters: [],
responses: { 200: { description: 'some response description' } },
response: {
additionalProperties: { type: 'any' },
properties: []
},
pathPattern: "test('{id}')"
};

expect(serializeOperation(operation)).toMatchSnapshot();
});

it('serializes operation with only query parameters', () => {
const operation: OpenApiOperation = {
operationId: 'getFn',
Expand Down Expand Up @@ -129,7 +156,7 @@ describe('serializeOperation', () => {
*/
getFn: (queryParameters?: {'limit'?: number}) => new OpenApiRequestBuilder<any>(
'get',
'test',
"test",
{
queryParameters
}
Expand Down Expand Up @@ -173,7 +200,7 @@ describe('serializeOperation', () => {
*/
createFn: (id: string, body: Record<string, any>) => new OpenApiRequestBuilder<any>(
'post',
'test/{id}',
"test/{id}",
{
pathParameters: { id },
body
Expand Down Expand Up @@ -209,7 +236,7 @@ describe('serializeOperation', () => {
*/
fnWithRefBody: (body: RefType | undefined) => new OpenApiRequestBuilder<string>(
'post',
'test',
"test",
{
body
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { serializeSchema } from './schema';
export function serializeOperation(operation: OpenApiOperation): string {
const requestBuilderParams = [
`'${operation.method}'`,
`'${operation.pathPattern}'`
`"${operation.pathPattern}"`
];

const bodyAndQueryParams = serializeParamsForRequestBuilder(operation);
Expand Down
12 changes: 8 additions & 4 deletions packages/openapi/src/openapi-request-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,20 @@ describe('openapi-request-builder', () => {
});

it('encodes path parameters', async () => {
const requestBuilder = new OpenApiRequestBuilder('get', '/test/{id}', {
pathParameters: { id: '^test' }
});
const requestBuilder = new OpenApiRequestBuilder(
'get',
"/test('{someId}')/{id}",
{
pathParameters: { someId: 'value', id: '^test' }
}
);
const response = await requestBuilder.executeRaw(destination);
expect(httpSpy).toHaveBeenCalledWith(
sanitizeDestination(destination),
{
method: 'get',
middleware: [],
url: '/test/%5Etest',
url: "/test('value')/%5Etest",
headers: { requestConfig: {} },
params: { requestConfig: {} },
data: undefined
Expand Down

0 comments on commit b4bc9ad

Please sign in to comment.