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(oas): user data + server variable quirks #848

Merged
merged 3 commits into from
Apr 5, 2024

Conversation

kanadgupta
Copy link
Member

@kanadgupta kanadgupta commented Mar 21, 2024

🚥 Resolves RM-9110

🧰 Changes

This PR fixes two interwoven issues related to construction of server variable data.

Issue 1: User Data Payload Retrieval

If a user data payload has a keys array, our oas.defaultVariables() function will look in there but fail to properly retrieve any data from the top level of that payload.

For example, take the following the server variables definition:

{
  "url": "https://{name}.example.com:{port}/{basePath}",
  "variables": {
    "name": { "default": "demo" },
    "port": { "default": "443" },
    "basePath": { "default": "v2" }
  }
}

And say the user payload looks like this:

{
  "name": "owlbert",
  "keys": [
    {
      "apiKey": "test1"
    }
  ],
  "port": "8000"
}

Because of a quirk in our getUserVariable helper, the presence of that keys array in the user data prevents us from using that top-level data at all. The resulting URL is https://demo.example.com:443/v2 when it should be https://owlbert.example.com:8000/v2.

This was a one-liner fix in 6e89c47 and I also added some test coverage in 366ffed 😌

Issue 2: Server Variable Data Precedence in oas.url()

Quick background

Once I figured out the solution to issue 1 above, I noticed the following bug — when you're logged in and you have user data populating your server variables, editing the server variables won't update the code sample:

CleanShot.2024-03-21.at.18.38.31-trimmed.mp4

Isolating the issue

After going down a crazy rabbit hole, I discovered the root issue was with how we use oas.url() to construct the URL in the auto-generated code snippet in our reference section.

For starters, we call oas-to-snippet to generate the snippet, which then invokes oas-to-har:

const har = opts.harOverride || generateHar(oas, operation, values, auth);

Once we're in oas-to-har, we invoke oas.url() to construct the resulting URL:

url: `${oas.url(formData.server.selected, formData.server.variables)}${operation.path}`.replace(/\s/g, '%20'),

The issue itself

There are essentially three sources for these variable values that we need to deal with (in order of lowest to highest precedence):

  1. Default values, as defined in the OpenAPI definition itself
  2. User data, as defined via JWT
  3. Current state data, a.k.a. whatever the end user manually writes into their API reference section

The problem is we were prioritizing 2 over 3. This PR reworks some logic related to oas.url() to fix that, see 9fbea61 😮‍💨

🧬 QA & Testing

For all of this, the new test cases provide a great visualization of the changes and expected behaviors. That and all the existing test coverage passes, so hopefully we should be in good shape!

@kanadgupta kanadgupta added the bug Something isn't working label Mar 21, 2024
@kanadgupta kanadgupta changed the title fix(oas): fallback to user object for default vars fix(oas): user data + server variable quirks Mar 21, 2024
@kanadgupta kanadgupta marked this pull request as ready for review March 21, 2024 23:49
@erunion
Copy link
Member

erunion commented Mar 26, 2024

assuming this fix is for something pretty serious, did you want to try to backport it to the current version of oas we're running in prod?

@kanadgupta
Copy link
Member Author

@erunion I'm open to it — maybe #842 also? anything else? cc: @darrenyong

@darrenyong
Copy link
Contributor

yep, sounds good!

@darrenyong darrenyong requested review from darrenyong and removed request for darrenyong March 27, 2024 17:41
@kanadgupta kanadgupta merged commit 9a630a4 into main Apr 5, 2024
18 checks passed
@kanadgupta kanadgupta deleted the kanad-2024-03-21/fallback-to-user-object branch April 5, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants