mocks-server/main

Paths with two parameters return message - 404 Not found

speculees opened this issue · 1 comments

Describe the bug
When path has more than one path params - mocks server returns Not found message.
If the /drivers/{driverId} is removed, leaving only one parameter the example is returned.

To Reproduce
Create openapi file with following path /cars/{carId}/drivers/{driverId} with some examples and attempt to access the resource.

Expected behavior
To return selected route variant example

Logs

Displaying logs. Press any key to display main menu again
12:38:14:20 [debug][server] Request received | GET => /api/cars/1/drivers/2 | Assigned id: 062546c7-568a-47f1-a2fe-76fab906a545
12:38:14:20 [debug][server] Sending Not found response | GET => /api/cars/1/drivers/2 | 062546c7-568a-47f1-a2fe-76fab906a545
12:38:14:20 [error][server] Sending Error 'Not Found' | 062546c7-568a-47f1-a2fe-76fab906a545

** Operating system, Node.js an npm versions, or browser version (please complete the following information):**

  • OS: [MacOs, Windows 10]
  • Node.js: [18.15.0]
  • npm: [9.5.0]

Additional context
Pseudo spec:

  /cars/{carId}/drivers/{driverId}:
    get:
      operationId: GetCarDriver
      security: []
      summary: Get driver
      parameters:
        - name: carId
          example: 1 
          in: path
          required: true
          schema:
            type: string
        - name: driverId
          in: path
          required: true
          example: 2
          schema:
            type: string
      responses:
        200:
          description: A
          content:
            application/json:
              ...
              examples:
                one:
                  value:
                    id: 2
                    employmentType: Full-time
                    name: Speculees
                    ...
                    location: Parise, France
                    jobDescription: lorem ipsum
                    responsibilities: lorem ipsum

Hi @speculees ,thanks for sharing!

The problem is caused by the regular expression at charge of converting OpenAPI paths into Express paths. After reproducing the issue with your data and enabling the debug log mode, I've seen that a path like the one you described is converted into /cars/:carId}/drivers/{driverId, because the regular expression replaces all the content from the start of the first parameter to the end of the last one as if it was a single parameter.

So, replacing the regular expression /{(\S*)}/gim by /{(\S*?)}/gim in the replaceTemplateInPath method of the plugin fixes the bug.

I'll try to publish a new version with the fix ASAP.