Datamodel-code-generator: Nested additionalProperties result in empty value type being generated

Created on 23 Sep 2020  路  3Comments  路  Source: koxudaxi/datamodel-code-generator

Describe the bug

Nested additionalProperties with specified type result in empty value type being generated.

To Reproduce

Example schema:

openapi: "3.0.0"
info:
  title: Sample API
  description: API description in Markdown.
  version: 1.0.0
servers: []
paths: {}
components:
  schemas:
    test:
      type: object
      properties:
        failing:
          type: object
          additionalProperties:
            type: string
          default: {}

Used commandline:

$ datamodel-codegen --input openapi.yaml --output test.py

Resulting generated code:

from __future__ import annotations

from typing import Dict, Optional

from pydantic import BaseModel


class Failing(BaseModel):
    pass


class Test(BaseModel):
    failing: Optional[Dict[str, Failing]] = None

The following error occurs when trying to use these generated classes:

print(Test(failing={"foo": "bar"}).to_json())
Traceback (most recent call last):
  File "test.py", line 20, in <module>
    print(Test(failing={"foo": "bar"}).to_json())
  File "pydantic/main.py", line 346, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Test
failing -> foo
  value is not a valid dict (type=type_error.dict)

Expected behavior

There is no type Failing or it is correctly set to be of value type str. Right now, data cannot be generated with this output.

Version:

  • OS: Linux
  • Python Verison: 3.8
  • datamodel-code-generator Version: 0.5.34
bug released

Most helpful comment

Thanks again for the very fast fixes!

All 3 comments

Interestingly, this doesn't happen if additionalProperties is used on the top-level. This result then in:

class Test(BaseModel):
    pass

    class Config:
        extra = Extra.allow

@languitar
I have released a new version as 0.5.35

Thanks again for the very fast fixes!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

philipbjorge picture philipbjorge  路  4Comments

Gowee picture Gowee  路  3Comments

fzirker picture fzirker  路  6Comments

HuiiBuh picture HuiiBuh  路  4Comments

mboutet picture mboutet  路  3Comments