Skip to content

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

@RedRoserade

Description

@RedRoserade

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions