Skip to content

Allow global variables#507

Closed
gabrieltron wants to merge 5 commits intoscanapi:mainfrom
gabrieltron:global-variables
Closed

Allow global variables#507
gabrieltron wants to merge 5 commits intoscanapi:mainfrom
gabrieltron:global-variables

Conversation

@gabrieltron
Copy link
Copy Markdown
Member

@gabrieltron gabrieltron commented Sep 12, 2021

Description

Allow global variables. When spec_vars is updated after performing a call, instead of updating the spec_vars of just the endpoint node, recursively update all parent endpoint nodes. When evaluating an expression, instead of searching for the variable in spec_vars of only the request's endpoint node, search all parent request nodes recursively.

Motivation behind this PR?

#265

What type of change is this?

Feature

Checklist

  • I have added a changelog entry / my PR does not need a new changelog entry. Instructions.
  • I have added/updated unit tests. Instructions.
  • New and existing unit tests pass locally with my changes. Instructions
  • I have self-documented code my changes by adding docstring(s) and comment(s). Instructions
  • Current PR does not significantly decrease the code coverage and docstring coverage.
  • My code follows the style guidelines of this project.
  • I have run ScanAPI locally and manually tested my changes. Instructions.
  • I have squashed my commits. Instructions.

Issue

Closes #265

Stannislav and others added 2 commits August 27, 2021 20:20
* Fix the --browser option

The problem was that webbrowser.open expects a URI and doesn't
work with relative local paths.

Solution: use pathlib to convert reporter.output_path to a URI
using pathlib.Path(reporter.output_path).resolve().as_uri().

* Add a changelog entry
@gabrieltron gabrieltron changed the title Global variables Allow global variables Sep 12, 2021
@gabrieltron gabrieltron force-pushed the global-variables branch 4 times, most recently from f13ac53 to 1d9cabf Compare September 12, 2021 17:26
@gabrieltron gabrieltron marked this pull request as ready for review September 12, 2021 17:32
@gabrieltron gabrieltron requested review from a team as code owners September 12, 2021 17:32
@gabrieltron gabrieltron force-pushed the global-variables branch 2 times, most recently from 26511c2 to 775c04a Compare September 12, 2021 19:26
Copy link
Copy Markdown
Member

@camilamaia camilamaia left a comment

Choose a reason for hiding this comment

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

Awesome PR @gabrieltron! This was a super desired feature that we were trying to deliver for a long time. Thank you very much!

Also, pretty elegant solution 🌟

I left only one small change request, let me know what do you think :)

@mark.context("when key exists in endpoint node")
@mark.it("should search endpoint node")
@mark.it("should return the value in endpoint node")
def test_should_return_list(self, spec_evaluator):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
def test_should_return_list(self, spec_evaluator):
def test_should_return_list(self, mocker, spec_evaluator):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

def test_should_return_list(self, spec_evaluator):
key = "requests"
expected_value = "value"
with mock.patch(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
with mock.patch(
with mocker.patch(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

@@ -1,4 +1,5 @@
from pytest import fixture, mark, raises
from unittest import mock
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't need to import mock from unittest because we use the pytest-mock library. It already provides a mocker fixture which is a thin-wrapper around the patching API provided by the mock package:

Suggested change
from unittest import mock

The two next suggestions show how to do it using mocker fixture.

Copy link
Copy Markdown
Member Author

@gabrieltron gabrieltron Sep 25, 2021

Choose a reason for hiding this comment

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

Yup! Fixed it. I forgot that the scope of a fixture is a function by default, so now I just used the existing mock from the fixture instead 😅

@@ -0,0 +1,29 @@
from pytest import mark
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@gabrieltron can we have a test in which we have vars with the same name for both parent and child? To check that the inner value should be the one returned: the child's one.

For example:

endpoints:
  - name: snippets-api
    path: http://demo.scanapi.dev/api/v1/
    headers:
      Content-Type: application/json
    vars:
      my_var: one
    requests:
      - name: health
        path: health?${my_var}
    endpoints:
      - name: endpoint
        vars:
          my_var: two
        requests:
          - name: another health
            path: health?${my_var}

Request one should be: GET http://demo.scanapi.dev/api/v1/health?one
Request two should be: GET http://demo.scanapi.dev/api/v1/health?two

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nice, I hadn't thought of that scenario. I changed the method so it only propagates the new vars.

I also changed it so it only writes if the variable doesn't exist. On the downside, this implies that there's no way of update global variables. On the upside, it further protects the variables already declared in the parents. Do you think this is a good approach? Or should request nodes be able to update global variables?

@gabrieltron
Copy link
Copy Markdown
Member Author

Any news on that? I'd appreciate feedback, even if it's that we should hold this up for now hahaha

Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for supporting ScanAPI, and congratulations on your first contribution! A project committer will shortly review your contribution.

In the mean time, if you haven't had a chance please skim over the First Pull Request Guide which all pull requests must adhere to.

We hope to see you around!

@github-actions github-actions bot added the First Contribution First contribution to the project. label Aug 12, 2025
@Pradhvan Pradhvan self-assigned this Aug 21, 2025
@Pradhvan
Copy link
Copy Markdown
Member

Pradhvan commented Aug 21, 2025

Any news on that? I'd appreciate feedback, even if it's that we should hold this up for now hahaha

sorry for the super late response, taking a look into this. If you are still up for it let me know else I can take it forward from the work you have already done. Thank You 💜

Added a new PR alltogether and added you as a co-author.

#810

@Pradhvan Pradhvan closed this Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First Contribution First contribution to the project.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global Variables

4 participants