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

[Question] Only Execute Examples from the OpenAPI Schema #381

Closed
mohit-sentieo opened this issue Jan 29, 2020 · 9 comments · Fixed by #382
Closed

[Question] Only Execute Examples from the OpenAPI Schema #381

mohit-sentieo opened this issue Jan 29, 2020 · 9 comments · Fixed by #382
Assignees
Labels
Priority: High Important but not urgent Type: Bug Errors or unexpected behavior

Comments

@mohit-sentieo
Copy link

I have read all the documentation and a big part of the code and I am unable to find a way to do this.

Only execute examples from the schema.

It keeps returning a FilteredStrategy and I am not sure how to filter out only the case examples.

It's quite surprising that such a simple case is not natively supported.

@Stranger6667 Stranger6667 self-assigned this Jan 29, 2020
@Stranger6667 Stranger6667 added Priority: Medium Planned for regular releases Type: Feature New functionalities or enhancements labels Jan 29, 2020
@Stranger6667
Copy link
Member

Hello @mohit-sentieo !
It sounds like a nice feature, indeed. Will work on it for the next release

@Stranger6667
Copy link
Member

It is actually possible at the moment with configuring Hypothesis in a certain way:

In code:

from hypothesis import Phase, settings

schema = ...

@schema.parametrize()
@settings(phases=[Phase.explicit])
def test_something(case):
    ...

For CLI, I'll add it soon as a shortcut, currently, it might work like this:

schemathesis run --hypothesis-phase=explicit ...

@Stranger6667
Copy link
Member

Let me know any of these options works for you

@mohit-sentieo
Copy link
Author

Thank You for the prompt reply. However this doesn't seem to be working.

I'll be back after some debugging :)

@mohit-sentieo
Copy link
Author

Hi @Stranger6667

After debugging, I can confirm that hypothesis is working as expected. However schemathesis is not properly adding the examples to the hypothesis strategy.

My OPENAPI3 YAML Schema is below:-

/documents/get:
    get:
      tags:
        - documents
      summary: Find document by ID
      description: >-
        Returns html for a document e.g. 5cff7ca9668114405a001036 ,
        5d40b7385cf17cc7f1000cf3
      operationId: getDocsById
      parameters:
        - name: id
          in: query
          description: ID of the document
          required: true
          schema:
            type: string
          example: 5cff7ca9668114405a001036
      responses:
        '200':
          description: successful operation
          content: {}

I have also tried with examples.

I have tracked the problem to the schemas.py file and these two functions:-

def add_parameter(self, container: Optional[Dict[str, Any]], parameter: Dict[str, Any]) -> Dict[str, Any]:
        """Add parameter object to the container."""
        name = parameter["name"]
        container = container or empty_object()
        container["properties"][name] = self.parameter_to_json_schema(parameter)
        if parameter.get("required", False):
            container["required"].append(name)
        return container

    def parameter_to_json_schema(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """Convert Parameter object to a JSON schema."""
        return {
            key: value
            for key, value in data.items()
            # Do not include keys not supported by JSON schema
            if not (key == "required" and not isinstance(value, list))
        }

They completely filter out the example instead of adding them to the container object.

Can you please help with this ?

@Stranger6667
Copy link
Member

Thank you very much for your investigation! Definitely a bug that I missed.

I'll fix it - it happened that the examples testing is done only for examples in "body", but they are processed slightly differently.

@Stranger6667 Stranger6667 added Priority: High Important but not urgent Type: Bug Errors or unexpected behavior and removed Priority: Medium Planned for regular releases Type: Feature New functionalities or enhancements labels Jan 30, 2020
@mohit-sentieo
Copy link
Author

No worries. I am forking it for my own use case meanwhile. Cheers for the wonderful library.

@Stranger6667
Copy link
Member

Thank you :) Let me know if we can do something to support your use case better :)

#382 should fix the issue - "example" field in Open API 3.0 ParameterObject was missed (https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-10)

@Stranger6667
Copy link
Member

And supporting "examples" field is probably worth another issue.

Feel free to reopen this issue, if it doesn't solve the problem. Released in 0.23.7 - https://pypi.org/project/schemathesis/0.23.7/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: High Important but not urgent Type: Bug Errors or unexpected behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants