Skip to content

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

@jakubno

Description

@jakubno

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions