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

[BUG] Examples for request body ignored during testcase generation #1771

Closed
hoog1511 opened this issue Sep 26, 2023 · 1 comment · Fixed by #1960
Closed

[BUG] Examples for request body ignored during testcase generation #1771

hoog1511 opened this issue Sep 26, 2023 · 1 comment · Fixed by #1960
Assignees
Labels
Core: Data Generation Generating test data Difficulty: Hard Complex, needs deep understanding Priority: Medium Planned for regular releases Status: Needs Design Issue requires more design work Type: Bug Errors or unexpected behavior
Milestone

Comments

@hoog1511
Copy link

hoog1511 commented Sep 26, 2023

Describe the bug

Yesterday I was testing an OAS which had the following pattern ^(?=[[:ascii:]]+$)[^\/\\\s$]{1,36}$ and for some reason this caused schemathesis to ignore the examples I provided for my request body.

To Reproduce
Steps to reproduce the behavior:

  1. Add the pattern to a header parameter.
  2. Add an example request body to one of your requests which uses said header parameter.
  3. Run schemathesis (I used CLI version)

Expected behavior

Examples being included with my testcases.

Environment (please complete the following information):

  • OS: [Windows]
  • Python version: [3.11.0]
  • Schemathesis version: [3.19.5]
  • Spec version: [3.0.3]
@hoog1511 hoog1511 added Status: Needs Triage Requires initial assessment to categorize and prioritize Type: Bug Errors or unexpected behavior labels Sep 26, 2023
@Stranger6667 Stranger6667 added Priority: Medium Planned for regular releases Difficulty: Hard Complex, needs deep understanding Core: Data Generation Generating test data Status: Needs Design Issue requires more design work and removed Status: Needs Triage Requires initial assessment to categorize and prioritize labels Oct 12, 2023
@Stranger6667 Stranger6667 added this to the 3.22 milestone Oct 17, 2023
@Stranger6667
Copy link
Member

The reason is that the pattern contains parts that don't work as one might expect in Python, specifically :ascii:. This character class is specific to PCRE, but Python's regex engine treats [[:ascii:]] as matching a character in the [:asci list, similarly ECMAScript treats it the same.

We use Python's regex with some pre-processing so it supports some parts from ECMAScript, and in general, it is not possible to unambiguously detect which regex style is intended, because some patterns, such as the one you provided are valid in multiple regex engines but mean different things. The Open API spec implies ECMAScript, so Schemathesis supports it on a best-effort basis. The proper solution is possible with the use of greenery and transforming regular expressions to the target style. Before this happens, there will be counter-examples that won't work as expected.

So, from the Schemathesis POV, some schemas with regex are hard to generate because in many scenarios we fall back to the filtering-based approach and Hypothesis raises Unsatisfiable. In this scenario, we skip example generation at all as it is likely that the test itself will fail with Unsatisfiable (not always, but with a decent probability).

Recently, Hypothesis became better at generating strings from regexes, so there are fewer cases when we fall back into this.

However, it is still confusing that e.g. with --hypothesis-phase=explicit some endpoints with examples will be skipped without a note.

So, in this specific case, I think rewriting the pattern to the equivalent one in the ECMAScript / Python syntax should solve the issue, otherwise we can raise an error / warning to inform the user about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core: Data Generation Generating test data Difficulty: Hard Complex, needs deep understanding Priority: Medium Planned for regular releases Status: Needs Design Issue requires more design work Type: Bug Errors or unexpected behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants