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

fix: mock issue resolve for similar templated requests #2564

Merged
merged 2 commits into from
Jul 19, 2024

Conversation

SB-akshaythakar
Copy link
Contributor

Addresses STOP-448

Summary

We observe that the issue is occurring in mock server when OAS is having similar templated requests as below example. Request was getting mapped with wrong endpoint and mock server was returning 400 error code.

Consider following API endpoints defined in project spec.

  1. https://stoplight-local.com:8443/mocks/seeded-enterprise/mock/174/{initgPtyIdOrgId}/payments/{paymentId}
  2. https://stoplight-local.com:8443/mocks/seeded-enterprise/mock/174/{initgPtyIdOrgId}/payments/search

we can find two paths here -

  1. /{initgPtyIdOrgId}/payments/{paymentId}
  2. /{initgPtyIdOrgId}/payments/search

If we call search Api from mock server our request will be encoded to "/SMARTBEAR/payments/search". here "SMARTBEAR" is initgPtyIdOrgId. And if call is made to payment API, request will be encoded to - SMARTBEAR/payments/paytm123. Here SMARTBEAR is initgPtyIdOrgId and paytm123 is paymentId. But if you compare the both requests looks similar.

There is matchPath function in prism-http which is handling request and endpoint mapping functionality and for above scenario it is returning two endpoint objects for search API. Ideally it should return single object array. And make-payment API is placed before search Api in spec, make-Api object will be the first item in array and matchPath is using first item from array to call mock Api.

To solve this, I created the sorting function in http-spec where all these endpoints will be sorted on the basis their match with request path. In case we get multiple matched endpoints for any request, this sorting will make sure that first endpoint in array is close match to request.

Checklist

  • The basics
    • I tested these changes manually in my local or dev environment
  • Tests
    • Added or updated
    • N/A
  • Event Tracking
    • I added event tracking and followed the event tracking guidelines
    • N/A
  • Error Reporting
    • I reported errors and followed the error reporting guidelines
    • N/A

Screenshots

If applicable, add screenshots or gifs to help demonstrate the changes. If not applicable, remove this screenshots
section before creating the PR.

Additional context

Add any other context about the pull request here. Remove this section if there is no additional context.

@@ -1,6 +1,6 @@
{
"name": "@stoplight/prism-http",
"version": "5.8.2",
"version": "5.8.3",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required? just check if it gets incremented bu build system during release or deployment ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. we need to change the version as per the change.

if (endpointParts[i] === requestParts[i]) {
score++;
} else if (isTemplated(endpointParts[i])) {
score += 0.5;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the purpose of this score += 0.5; ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added score to differentiate match weightage. When templated path is found, 0.5 score is added to it and if path is not templated and it is matching with request, then score is incremented by 1. So non-template path will have more weightage compare to templated path while performing sorting.

Copy link

@prafullaAtSB prafullaAtSB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are good to go ahead

@SB-akshaythakar SB-akshaythakar merged commit b8e9fd8 into master Jul 19, 2024
7 checks passed
@SB-akshaythakar SB-akshaythakar deleted the STOP-448-mock-server-400-issue branch July 19, 2024 08:49
This was referenced Jul 19, 2024
This was referenced Aug 8, 2024
ilanashapiro pushed a commit to ilanashapiro/prism that referenced this pull request Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants