openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open rei-ber opened this issue 9 months ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] 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

rei-ber avatar May 08 '24 14:05 rei-ber

I tried a bit more and got another, maybe more meaningful example. This one has a get and post path and breaks by producing an AnyOf object which does not exist.

While this one, almost the same as before, but does not contain a get path, works. This one does not produce an unknown AnyOf object.

And as in #17415 someone stated out to reduce the version: if I change openapi to 3.0.3 it works well with the broken and working example (get and post path).

Anyone has a suggestions?

rei-ber avatar May 10 '24 13:05 rei-ber

I am running into similar issue too.
I tried both openapi version 3.0.0, 3.0.3 and 3.1.0 on these docker images using the python generator and still seeing the issue,
openapitools/openapi-generator-cli:v7.8.0 openapitools/openapi-generator-cli:latest

The issue occurs when anyOf has an item with missing type.

Here is the OpenAPI Definition file and FastAPI code that generated it:

openapi.json fastapi_example.txt

joeyjsjs avatar Oct 04 '24 17:10 joeyjsjs