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:
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!
Most helpful comment
Thanks again for the very fast fixes!