fix: Annotate free-form object as dict in Python (#17082)

This commit is contained in:
Robert Schweizer
2023-11-18 03:46:01 +01:00
committed by GitHub
parent 0098d56a6a
commit 5e7f2f274c
6 changed files with 34 additions and 31 deletions

View File

@@ -8,8 +8,10 @@ import time
import unittest
from pydantic import ValidationError
import pytest
import petstore_api
from petstore_api import InnerDictWithProperty
class ModelTests(unittest.TestCase):
@@ -508,6 +510,19 @@ class ModelTests(unittest.TestCase):
self.assertFalse(b is None)
self.assertEqual(b.optional_dict["key"].a_property["a"], "b")
def test_freeform_object(self):
# Allows dict[str, Any] and is nullable
a = InnerDictWithProperty.from_dict({"aProperty": {"a": 12}})
a = InnerDictWithProperty.from_dict({"aProperty": None})
# Allows no other values
with pytest.raises(ValidationError):
a = InnerDictWithProperty.from_dict({"aProperty": {123: 45}})
with pytest.raises(ValidationError):
a = InnerDictWithProperty.from_dict({"aProperty": "abc"})
with pytest.raises(ValidationError):
a = InnerDictWithProperty.from_dict({"aProperty": 12})
def test_object_with_dict_of_dict_of_object(self):
# for https://github.com/OpenAPITools/openapi-generator/issues/15135
d = {"optionalDict": {"a": {"b": {"aProperty": "value"}}}}