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

Exception when generating list properties in multipart forms #451

Closed
dpursehouse opened this issue Jul 7, 2021 · 11 comments · Fixed by #452
Closed

Exception when generating list properties in multipart forms #451

dpursehouse opened this issue Jul 7, 2021 · 11 comments · Fixed by #452
Labels
🐞bug Something isn't working
Milestone

Comments

@dpursehouse
Copy link

After upgrading from 0.9.2 to 0.10.0 the client generation fails with:

Traceback (most recent call last):
  File "REDACTED/.venv/bin/openapi-python-client", line 8, in <module>
    sys.exit(app())
  File "REDACTED/.venv/lib/python3.8/site-packages/typer/main.py", line 214, in __call__
    return get_command(self)(*args, **kwargs)
  File "REDACTED/.venv/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "REDACTED/.venv/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "REDACTED/.venv/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "REDACTED/.venv/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "REDACTED/.venv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "REDACTED/.venv/lib/python3.8/site-packages/typer/main.py", line 497, in wrapper
    return callback(**use_params)  # type: ignore
  File "REDACTED/.venv/lib/python3.8/site-packages/openapi_python_client/cli.py", line 141, in generate
    errors = create_new_client(
  File "REDACTED/.venv/lib/python3.8/site-packages/openapi_python_client/__init__.py", line 314, in create_new_client
    return project.build()
  File "REDACTED/.venv/lib/python3.8/site-packages/openapi_python_client/__init__.py", line 108, in build
    self._build_api()
  File "REDACTED/.venv/lib/python3.8/site-packages/openapi_python_client/__init__.py", line 263, in _build_api
    module_path.write_text(endpoint_template.render(endpoint=endpoint), encoding=self.file_encoding)
  File "REDACTED/.venv/lib/python3.8/site-packages/jinja2/environment.py", line 1289, in render
    self.environment.handle_exception()
  File "REDACTED/.venv/lib/python3.8/site-packages/jinja2/environment.py", line 924, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "REDACTED/.venv/lib/python3.8/site-packages/openapi_python_client/templates/endpoint_module.py.jinja", line 38, in top-level template code
    {{ multipart_body(endpoint) | indent(4) }}
  File "REDACTED/.venv/lib/python3.8/site-packages/jinja2/runtime.py", line 828, in _invoke
    rv = self._func(*arguments)
  File "REDACTED/.venv/lib/python3.8/site-packages/openapi_python_client/templates/endpoint_macros.py.jinja", line 80, in template
    {{ transform_multipart(property, property.python_name, destination) }}
  File "REDACTED/.venv/lib/python3.8/site-packages/jinja2/utils.py", line 81, in from_obj
    if hasattr(obj, "jinja_pass_arg"):
jinja2.exceptions.UndefinedError: the template 'property_templates/list_property.py.jinja' (imported on line 79 in 'endpoint_macros.py.jinja') does not export the requested name 'transform_multipart'
@dpursehouse dpursehouse added the 🐞bug Something isn't working label Jul 7, 2021
@slamora
Copy link

slamora commented Jul 8, 2021

It's failing to mee too with version 0.10.0 but working if I downgrade to 0.9.2

I have installed openapi-python-client on a clean virtual environment using pip.

$ python --version
Python 3.8.10
$ pip freeze
anyio==3.2.1
appdirs==1.4.4
attrs==21.2.0
autoflake==1.4
black==21.6b0
certifi==2021.5.30
click==7.1.2
h11==0.12.0
httpcore==0.13.6
httpx==0.18.2
idna==3.2
isort==5.9.2
Jinja2==3.0.1
MarkupSafe==2.0.1
mypy-extensions==0.4.3
openapi-python-client==0.9.2
pathspec==0.8.1
pydantic==1.8.2
pyflakes==2.3.1
python-dateutil==2.8.1
PyYAML==5.4.1
regex==2021.7.6
rfc3986==1.5.0
shellingham==1.4.0
six==1.16.0
sniffio==1.2.0
toml==0.10.2
typer==0.3.2
typing-extensions==3.10.0.0

@dbanty
Copy link
Collaborator

dbanty commented Jul 8, 2021

Thanks for the report and confirmation! Looks like this was due to #372. @csymeonides-mf if you get a chance could you look into a fix? If not, I can probably sort one out this weekend.

@dbanty dbanty changed the title jinja2.exceptions.UndefinedError: the template 'property_templates/list_property.py.jinja' (imported on line 79 in 'endpoint_macros.py.jinja') does not export the requested name 'transform_multipart' Exception when generating list properties in multipart forms Jul 8, 2021
@dbanty dbanty added this to the 0.10.1 milestone Jul 8, 2021
@csymeonides-mf
Copy link
Contributor

Apologies for this, will investigate asap

@csymeonides-mf
Copy link
Contributor

@dpursehouse @slamora could you please share your openapi JSON/yaml, or at least the part that causes the failure?

@csymeonides-mf
Copy link
Contributor

Presumably you have something like this?

requestBody:
  content:
    multipart/form-data:
      schema:
        type: array

but surely you want type: object? What effect should we expect from type: array?

@csymeonides-mf
Copy link
Contributor

The OpenAPI spec seems to be advising that, if we want to allow uploading multiple files, we should use something like this:

requestBody:
  content:
    multipart/form-data:
      schema:
        properties:
          # The property name 'file' will be used for all files.
          file:
            type: array
            items:
              type: string
              format: binary

@csymeonides-mf
Copy link
Contributor

I've pushed #452 to fix this, though I'm not sure it's the right fix. @dbanty let me know what you think

@dbanty
Copy link
Collaborator

dbanty commented Jul 8, 2021

Thanks for jumping on this so quick! Hopefully we can get some sample schemas and expected results from OPs. I'm not exactly sure what the "correct" behavior is in this case.

@dpursehouse
Copy link
Author

@csymeonides-mf thanks. In the case that exposed the issue, the yaml file contains a section that looks exactly as you imagined, i.e.

requestBody:
  content:
    multipart/form-data:
      schema:
        type: array

The yaml is being generated by drf-spectacular based on the views in our Django app.

@slamora
Copy link

slamora commented Jul 9, 2021

From my site, the yaml has being generated by management command generateschema of django-rest-framework. Here you have a part of the YML which raises the Exception:

openapi: 3.0.2
info:
  title: ''
  version: ''
paths:
  /api/databaseusers/{id}/set_password/:
    post:
      operationId: set_passwordDatabaseUser
      description: ''
      parameters:
      - name: id
        in: path
        required: true
        description: A unique integer value identifying this database user.
        schema:
          type: string
      requestBody:
        content:
          application/json:
            schema: &id059
              properties:
                password:
                  type: string
                  maxLength: 128
              required:
              - password
          application/x-www-form-urlencoded:
            schema: *id059
          multipart/form-data:
            schema: *id059
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  password:
                    type: string
                    maxLength: 128
                required:
                - password
          description: ''

@dpursehouse
Copy link
Author

With version 0.10.1 the client generation succeeds, thanks, however there is now a new bug #453 which prevents from using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants