Datamodel-code-generator: additionalProperties always result in Any Python type

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

Describe the bug

When using an object with additionalProperties and an item type, the resulting code doesn't reference the generated item type.

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:
        broken:
          type: object
          additionalProperties:
            $ref: '#/components/schemas/broken'
    broken:
      type: object
      properties:
        foo:
          type: string
        bar:
          type: integer

Results in the following generated file:

# generated by datamodel-codegen:
#   filename:  openapi.yaml
#   timestamp: 2020-09-09T10:17:59+00:00

from __future__ import annotations

from typing import Any, Dict, Optional

from pydantic import BaseModel


class Test(BaseModel):
    broken: Optional[Dict[str, Any]] = None


class Broken(BaseModel):
    foo: Optional[str] = None
    bar: Optional[int] = None

Used commandline:

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

Expected behavior
The definition for broken should be:

broken: Optional[Dict[str, Broken]] = None

Version:

  • OS: Linux
  • Python Verison: 3.8.5
  • datamodel-code-generator Version: 0.5.30
bug released

All 3 comments

This already happens without a type reference. Setting type: string exhibits the same behavior.

@languitar
Thank you for your reporting.
I detect the problem. I will fix it.

@languitar
I have released a fixed version as 0.5.31

Was this page helpful?
0 / 5 - 0 ratings