OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python] NameError: name 'AnyOf' is not defined

Open
#18,612 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The pyhton client generates an object called AnyOf which does not exist.

openapi-generator version

7.5.0 or this for me latest main branch

OpenAPI declaration file content or url

broken: https://gist.github.com/rei-ber/53a9c9bbf57ecd4d49e955f02fe33c46
works: https://gist.github.com/rei-ber/d93abc83f7dbabb5ff673517cfc4d4a6

Generation Details

Python 3.11.9
Podman 5.0.2

Steps to reproduce
mkdir any_of_test
cd any_of_test
curl https://gist.githubusercontent.com/rei-ber/53a9c9bbf57ecd4d49e955f02fe33c46/raw/86e6d9ce1e0af5c1f2212306603714c2468f0880/gistfile1.txt -o openapi.json
podman run --rm --volume ${PWD}:/src openapitools/openapi-generator-cli:latest generate --package-name any_of_test --input-spec /src/openapi.json --generator-name python --output /src/generated --enable-post-process-file
python3 -m venv venv
source venv/bin/activate
cd generated/
pip install -r requirements.txt

echo """
from any_of_test.models.usecase_data import (
    UsecaseData,
)

usecase = UsecaseData()
obj = {
    'reputation': {'fieldref': '', 'mark': '', 'ttl': 0},
}
usecase.from_dict(obj)
""" > any_of_test.py 

python any_of_test.py

Produces the following:

Traceback (most recent call last):
  File "/home/reiber/Downloads/any_of_test/generated/any_of_test.py", line 10, in <module>
    usecase.from_dict(obj)
  File "/home/reiber/Downloads/any_of_test/generated/any_of_test/models/usecase_data.py", line 91, in from_dict
    "reputation": AnyOf.from_dict(obj["reputation"]) if obj.get("reputation") is not None else None
                  ^^^^^
NameError: name 'AnyOf' is not defined
Related issues/PRs

#17415

Suggest a fix

If I change the title and remove UUID like:

42c42
<                 "title": "UsecaseDataUUID",
---
>                 "title": "UsecaseData",
103c103
<                                     "title": "UsecaseDataUUID",
---
>                                     "title": "UsecaseData",

it works:

mkdir any_of_test
cd any_of_test
curl https://gist.githubusercontent.com/rei-ber/d93abc83f7dbabb5ff673517cfc4d4a6/raw/a1a26f7259fe589add3086ae365a2e1950f79397/gistfile1.txt -o openapi.json
podman run --rm --volume ${PWD}:/src openapitools/openapi-generator-cli:latest generate --package-name any_of_test --input-spec /src/openapi.json --generator-name python --output /src/generated --enable-post-process-file
python3 -m venv venv
source venv/bin/activate
cd generated/
pip install -r requirements.txt

echo """
from any_of_test.models.usecase_data import (
    UsecaseData,
)

usecase = UsecaseData()
obj = {
    'reputation': {'fieldref': '', 'mark': '', 'ttl': 0},
}
usecase.from_dict(obj)
""" > any_of_test.py 

python any_of_test.py

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the broken OpenAPI declaration and the Python generator command, then inspect generated/any_of_test/models/usecase_data.py, especially UsecaseData.from_dict. Compare it with the working declaration and related issue #17415; done means the generated client imports AnyOf correctly and the provided Python reproduction completes without NameError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.