SAP / cloud-sdk-js

Use the SAP Cloud SDK for JavaScript / TypeScript to reduce development effort when building applications on SAP Business Technology Platform that communicate with SAP solutions and services such as SAP S/4HANA Cloud, SAP SuccessFactors, and many others.
Apache License 2.0
160 stars 51 forks source link

Replace %2F from URL path with " / " #4460

Closed mcselvan closed 5 months ago

mcselvan commented 5 months ago

Describe the bug Replace %2F to / in URL path. 'https://example.com/api/documents/FpStoreApp%2Fpch';

To Reproduce Steps to reproduce the behavior:

  1. Set up ... npm i @sap-cloud-sdk

  2. Execute ... Under Folder @sap-cloud-sdk/ openapi /dist openapi-request-builder.js the requestconfig function is converting the below options parameter to "FpStoreApp%2Fpch" wrongly.

    I am passing parameter like options = "FpStoreApp/pch" . later this path is attaching with main url.

  3. See error getting invalid url because of %2F issue.

Expected behavior getpath() function get url like this 'https://example.com/api/documents/FpStoreApp%2Fpch'; Instead of / -> %2F expected is 'https://example.com/api/documents/FpStoreApp/pch';

Screenshots

Used Versions:

Code Examples The below function : this code need to be updated to replace special characters from the URL const decodedUrl = this.getPath().replace(/%2F/g, "/"); and URL property also to be update with decodedUrl.

  async requestConfig() {
        const decodedUrl = this.getPath().replace(/%2F/g, "/");
        const defaultConfig = {
            method: this.method,
            url: decodedUrl,   //this.getPath(),
            headers: this.getHeaders(),
            params: this.getParameters(),
            middleware: this._middlewares,
            parameterEncoder: internal_2.encodeTypedClientRequest,
            data: this.parameters?.body
        };
        return {
            ...defaultConfig,
            ...(0, internal_2.filterCustomRequestConfig)(this.customRequestConfiguration)
        };

Log file If applicable, add your log file or related error message. Again, please remove your sensitive information.

Impact / Priority

Affected development phase: Getting Started, Development

Impact: Blocked

Timeline: Go-Live is in 4 weeks.

Additional context Because of this problem, I have to spend more time to find out different approach

marikaner commented 5 months ago

Hey @mcselvan, thank you for reporting and investigating this. Could you please share the code that you are executing and point out the exact issue? I understand that you would like to skip the encoding, but in other cases encoding might be required, so I would like to understand how you are using the API and whether this is in fact a bug. It seems that you are using the @sap-cloud-sdk/openapi lib, which you should only use with a generated client. Please share the steps to reproduce this issue.

mcselvan commented 5 months ago

Hi @marikaner, Thanks for your response. Here I am sharing the steps to reproduce this issue. We are using 'sap-cap-sdm-plugin" library for SAP DMS curd operations. While using document creation function with parameters like which folder I want to create a file example

           let destination = await loadDestination();
            const client = await cds.connect.to('cmis-client');
            const options = {};
            options.folderPath = "FpStoreApp/pch";       (This string / is changing in opeapi)
            const readStream = fs.createReadStream('./download/A4-16012024.pptx');
            try {
                response = await client.createDocument(repositoryId, 'A4-16012024-52.pptx', readStream, options)//'${Date.now()}-A4L-v1.pptx','A4L-16012024.pptx')
                    .execute(destination); 

It will go here:-

const { OpenApiRequestBuilder } = require('@sap-cloud-sdk/openapi');
const createBrowserRootByRepositoryIdAndDirectoryPath = (
  repositoryId,
  directoryPath,
  body,
) =>
  new OpenApiRequestBuilder(
    'post',
    '/browser/{repositoryId}/root/{directoryPath}',
    {
      pathParameters: { repositoryId, directoryPath },
      body,
    },
  );

After executing this code. getting this error -02-05T15:11:03.904Z","written_ts":1707145863904} [2024-02-05T15:11:03.918Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /browser/cd2ea5d5-f260-4309-9198-105de0479141/root/FpStoreApp%2Fpch/. [2024-02-05T15:11:03.923Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /browser/cd2ea5d5-f260-4309-9198-105de0479141/root/FpStoreApp%2Fpch.

see end of the URL.

marikaner commented 5 months ago

Hey @mcselvan,

I think I now understand what you are trying to achieve. Unfortunately we cannot simply turn off encoding for path parts because for most cases this is required. Also, according to the OpenAPI specification it is not possible to have path parameters that are not encoded (see allowReserved here). This makes sense because that would allow you to change the path of your resources depending on what path parameter you're sending. Instead I would rather recommend sending this as a query parameter, if you can change the API of the underlying service.

Please note, that I found an error in the encoding of query parameters, that currently leaves them unencoded. We will fix this as soon as possible, so that by default query parameters will also be encoded. I would recommend to then decode them in the backend service.

If you still need the functionality for path parameters, you could add a middleware that subsequently changes the URL.

marikaner commented 5 months ago

The encoding of query parameters is not fixed. I hope you are able to switch to query parameters. If you need a different way of encoding your parameters, you can overwrite the parameterEncoder in the request configuration.