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

Python client code improperly escaping regex strings #1517

Closed
Raznic opened this issue Nov 21, 2018 · 1 comment
Closed

Python client code improperly escaping regex strings #1517

Raznic opened this issue Nov 21, 2018 · 1 comment

Comments

@Raznic
Copy link
Contributor

Raznic commented Nov 21, 2018

Description

I've got a web server running Django + REST Framework that I am trying to generate Python client code for. One of my Serializers defines a RegexField that includes \d as a part of the regular expression. The generator appears to be escaping these characters in the generated client code.

Serializer regex: r'^\d$'
Actual Python client code: re.search(r'^\\d$', value)
Expected Python client code: re.search(r'^\d$', value)

openapi-generator version

I've pulled the latest from master and the version shows as 3.3.4-SNAPSHOT.

OpenAPI declaration file content or url
Command line used for generation
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g python -i <development Swagger URL> -o python
Steps to reproduce
  1. Define a Serializer class with a RegexField in your Django + REST Framework app.
from rest_framework.serializers import ModelSerializer, RegexField
from .model import MyModel
class MyModelSeralizer(ModelSerializer):
    value = RegexField(regex=r'^\d$')

    class Meta:
        model = MyModel
        fields = '__all__'
  1. Generate Python client code.

  2. Try to create a model.

import openapi_client

config = openapi_client.Configuration()
client = openapi_client.ApiClient(client)
api_instance = openapi_client.MyModelApi(client)

data = openapi_client.MyModel(value='9')
api_instance.my_model_create(data)
Related issues/PRs

#1392

Suggest a fix/enhancement

My Java is pretty rusty, but I would assume the fix for the Python client would be similar to the one for the Ruby client from the related issue/PR.

@Raznic
Copy link
Contributor Author

Raznic commented Nov 21, 2018

After a little more testing, it appears the problem is not just limited to \d. Any backslash character in the regex string is getting escaped.

Raznic added a commit to Raznic/openapi-generator that referenced this issue Nov 24, 2018
* Override PythonClientCodegen toRegularExpression() method to correct
  issue with backslashes being escaped. This issue was a result of
  calling escapeText() in the parent DefaultCodegen class.
cbornet added a commit to cbornet/openapi-generator that referenced this issue Nov 29, 2018
wing328 pushed a commit that referenced this issue Dec 8, 2018
* Fix escaped regex values in Python client (#1517).

* Override PythonClientCodegen toRegularExpression() method to correct
  issue with backslashes being escaped. This issue was a result of
  calling escapeText() in the parent DefaultCodegen class.

* Only escape unescaped forward slashes in PythonClientCodegen.

* Override addRegularExpressionDelimiter in PythonClientCodegen.java
  such that only unescaped forward slashes in the pattern get escaped.
* Adds a new test resource .yaml file for specifically testing this
  issue.

* Check for regular expression modifiers in PythonClientCodegen.

* Adds check in postProcessPattern() in PythonClientCodegen.java to
  check if regular expression has modifiers defined. If so, it throws an
  exception as modifiers are not currently supported in the Python
  client.

* PythonClientCodegen warns that regex modifiers are not supported.

* Changes behavior in PythonClientCodegen.java to no longer throw an
  IllegalArgumentException in the case that a pattern contains
  modifiers. A warning message will be logged instead stating that
  modifiers are not currently supported by the codegen class.

* Remove warning for PythonClientCodegen regex modifier support.

* Removes warning message from PythonClientCodegen.java stating that
  regular expression modifiers are not currently supported. Further
  code review and testing revealed that this feature is already
  supported and working.

* Add updated Python client sample files.
A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this issue Feb 27, 2019
A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this issue Feb 27, 2019
…PITools#1539)

* Fix escaped regex values in Python client (OpenAPITools#1517).

* Override PythonClientCodegen toRegularExpression() method to correct
  issue with backslashes being escaped. This issue was a result of
  calling escapeText() in the parent DefaultCodegen class.

* Only escape unescaped forward slashes in PythonClientCodegen.

* Override addRegularExpressionDelimiter in PythonClientCodegen.java
  such that only unescaped forward slashes in the pattern get escaped.
* Adds a new test resource .yaml file for specifically testing this
  issue.

* Check for regular expression modifiers in PythonClientCodegen.

* Adds check in postProcessPattern() in PythonClientCodegen.java to
  check if regular expression has modifiers defined. If so, it throws an
  exception as modifiers are not currently supported in the Python
  client.

* PythonClientCodegen warns that regex modifiers are not supported.

* Changes behavior in PythonClientCodegen.java to no longer throw an
  IllegalArgumentException in the case that a pattern contains
  modifiers. A warning message will be logged instead stating that
  modifiers are not currently supported by the codegen class.

* Remove warning for PythonClientCodegen regex modifier support.

* Removes warning message from PythonClientCodegen.java stating that
  regular expression modifiers are not currently supported. Further
  code review and testing revealed that this feature is already
  supported and working.

* Add updated Python client sample files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants