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][Python] Some regex patterns can generate invalid code. #6675

Open
5 of 6 tasks
RedRoserade opened this issue Jun 15, 2020 · 2 comments
Open
5 of 6 tasks

[BUG][Python] Some regex patterns can generate invalid code. #6675

RedRoserade opened this issue Jun 15, 2020 · 2 comments

Comments

@RedRoserade
Copy link

@RedRoserade RedRoserade commented Jun 15, 2020

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

If a pattern such as ^['"\-\w\s]+$ is specified, the generated python code for validation produces a syntax error:

        if self.api_client.client_side_validation and 'search_string' in local_var_params and not re.search(r'^[\'"\-\w\s]+$', local_var_params['search_string']):  # noqa: E501
            raise ApiValueError("Invalid value for parameter `search_string` when calling `example`, must conform to the pattern `/^['"\-\w\s]+$/`")  # noqa: E501
                                                                                                                            #       --^

However, if it is changed to ^[\'\"\-\w\s]+$, the error then becomes:

        if self.api_client.client_side_validation and 'search_string' in local_var_params and not re.search(r'^[\\'\"\-\w\s]+$', local_var_params['search_string']):  # noqa: E501
                                                                                                            #  ---^
            raise ApiValueError("Invalid value for parameter `search_string` when calling `example`, must conform to the pattern `/^[\'\"\-\w\s]+$/`")  # noqa: E501

The pattern ^['\"\-\w\s]+$ works, since the generated code has the correct escape sequences, but as far as I can tell, it is equivalent to the others (despite the redundant escapes).

openapi-generator version

I'm using version v4.3.1.

OpenAPI declaration file content or url
swagger: '2.0'
info:
  version: 1.0.0
  title: Example
consumes:
  - application/json
produces:
  - application/json
paths:
  /example:
    get:
      operationId: example
      parameters:
        - name: search_string
          in: query
          required: true
          type: string
          minLength: 1
          maxLength: 256
          pattern: ^['"\-\w\s]+$
      responses:
        '200':
          description: OK

##### Command line used for generation

docker run --rm
-v "$(pwd):/local"
--user="$(id -u):$(id -g)"
-w /local
openapitools/openapi-generator-cli:v4.3.1 generate
-i "swagger.yaml"
-g python
-o .
-c example.json


```json
{
  "packageName": "example",
  "projectName": "example",
  "packageVersion": "1.0.0"
}
Related issues/PRs
Suggest a fix

I would like to fix this, if possible.

@hbusul
Copy link

@hbusul hbusul commented Dec 21, 2020

Is there any development for this issue?

@spacether
Copy link
Contributor

@spacether spacether commented Mar 24, 2021

@hbusul and @RedRoserade do either of you or does anyone else want to fix this?
One would probably need to edit the existing postProcessPattern method here
To build in the proper escaping when going from openapi regex to a python regex string with escaping.

And here is where our regex patterns are placed in the generated python code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants