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

Variables with JSON Strings Not Parsed Unless parse_response_as_json is Set #1

Open
vmariiechko opened this issue Nov 13, 2024 · 2 comments
Assignees

Comments

@vmariiechko
Copy link
Collaborator

vmariiechko commented Nov 13, 2024

Summary

I'm encountering an issue where variables defined in the YAML templates as JSON-formatted strings are not being parsed into JSON objects unless I set parse_response_as_json: true in the default_request or within the request step.

Steps to Reproduce

  1. Use the variable in a request that expects a JSON object:
config:
  ...
  default_request:
    product: '{{ product }}'

steps:
  - name: Test JSON Parsing in Variables
    request:
      product: '{ "id": 123 }'
  1. Run the scenario:
poetry run ccheck --output-type console --filename test_scenario.yaml

Expected Behavior

The variable product should be parsed as a JSON object and sent in the request.

Actual Behavior

The variable product remains a string, not a parsed JSON object.
An error occurs when making a request to an API that sends a string in a JSON format.

Analysis

I traced the issue to the replace_str_with_json function in the codebase.
The function only attempts to parse strings into JSON if the parse_response_as_json flag is present in the dictionary.
Relevant Code Snippet:

def replace_str_with_json(d: dict) -> dict:
    """Replace all strings JSON-parsable with corresponding python object."""

    if "parse_response_as_json" in d:
        for k, v in d.items():
            if isinstance(v, str) and check_beginning_bracket(v):
                try:
                    d[k] = from_json(v)
                except ValueError:
                    pass
            elif isinstance(v, dict):
                d[k] = replace_str_with_json(v)
    return d

I understand from the unit tests that the parse_response_as_json flag is intended to control when JSON parsing occurs, preventing unintended parsing of strings. However, in the case of variables, it seems cumbersome to set parse_response_as_json: true globally or within each request step.

Question

Variable Parsing Behavior: should variables containing JSON-formatted strings be parsed into JSON objects by default, or is there a recommended way to handle this?

Possible Solutions

Option 1: Modify the replace_str_with_json to automatically attempt to parse any variable value that is a string starting with a JSON opening character ({ or [), regardless of any flags.
Option 2: Introduce a new configuration option (e.g., parse_variables_as_json) that, when set to true, instructs the framework to parse all variables starting with { or [ as JSON.

I'm seeking guidance on the best way to handle variables that need to be parsed as JSON.
If introducing a new mechanism is acceptable, I'm happy to contribute by implementing it and submitting a Pull Request.

@aish1496
Copy link

aish1496 commented Dec 6, 2024

Can anyone assign me this issue

@vmariiechko
Copy link
Collaborator Author

@aish1496 thanks for volunteering to work on this issue!

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

No branches or pull requests

2 participants