Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #6675
Closed
Open
Issue created Jun 15, 2020 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

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

Created by: 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
  • https://github.com/OpenAPITools/openapi-generator/issues/5582
Suggest a fix

I would like to fix this, if possible.

Assignee
Assign to
Time tracking