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

[BUG][Python] Disallow additional properties if not present - missing Any and Dict import #16151

Closed
5 of 6 tasks
jakubno opened this issue Jul 21, 2023 · 1 comment · Fixed by #16208
Closed
5 of 6 tasks

Comments

@jakubno
Copy link

jakubno commented Jul 21, 2023

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

When setting disallowAdditionalPropertiesIfNotPresent tofalse it generates models with missing imports, disallowing generating the models without manual edits.

Here's snippet from the code

from __future__ import annotations
import pprint
import re  # noqa: F401
import json


# ------------------------------------------------------
# Here should be: from typing import Any, Dict, Optional
# ------------------------------------------------------
from typing import Optional
from pydantic import BaseModel, Field, StrictBool

class Object(BaseModel):
    """
    Minimal object
    """
    var_property: Optional[StrictBool] = Field(False, alias="property", description="Property")
    additional_properties: Dict[str, **Any] = {}
    __properties = ["property"]
...
openapi-generator version

Latest docker image

openapi-generator-cli 7.0.0-SNAPSHOT
  commit : 7252d1a
  built  : -999999999-01-01T00:00:00+18:00
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url

Minimal example

Generation Details

The code was generated by following command:

generate -i /local/openapi.yml -g python --additional-properties=disallowAdditionalPropertiesIfNotPresent=false

That should translate to

generatorName: python
outputDir: ./out/python/
inputSpec: https://gist.github.com/jakubno/fcc0709d18a4cc548e20d6499222fd14
templateDir: modules/openapi-generator/src/main/resources/python
additionalProperties:
  packageName: minimal_example
  disallowAdditionalPropertiesIfNotPresent: false 
Steps to reproduce

Just generate the code from the minimal example and the file ./out/python/openapi_client/models/object.py will be missing imports from Typing, explicitly from typing import Any, Dict

Related issues/PRs

Didn't find any

Suggest a fix

Add the imports if the property disallowAdditionalPropertiesIfNotPresent is set to false.

fa0311 added a commit to fa0311/openapi-generator that referenced this issue Jul 28, 2023
…s if not present - missing Any and Dict import

Signed-off-by: ふぁ <yuki@yuki0311.com>
wing328 pushed a commit that referenced this issue Jul 29, 2023
…sent - missing Any and Dict import (#16208)

* [python-nextgen] fix #16151 Disallow additional properties if not present - missing Any and Dict import

Signed-off-by: ふぁ <yuki@yuki0311.com>

* [python-nextgen] update samples

Signed-off-by: ふぁ <yuki@yuki0311.com>

* [python-nextgen] remove unwanted imports

Signed-off-by: ふぁ <yuki@yuki0311.com>

* [python-nextgen] update samples

Signed-off-by: ふぁ <yuki@yuki0311.com>

* [python-nextgen] remove unwanted imports

Signed-off-by: ふぁ <yuki@yuki0311.com>

* [python-nextgen] update samples

Signed-off-by: ふぁ <yuki@yuki0311.com>

---------

Signed-off-by: ふぁ <yuki@yuki0311.com>
@wing328
Copy link
Member

wing328 commented Jul 29, 2023

Thanks @fa0311 for the PR.

@jakubno please pull the latest master to give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants