From c2080e237e2e95a7bcd0660fbda1ef8ea62a7a15 Mon Sep 17 00:00:00 2001 From: Joe Lynch Date: Wed, 4 Oct 2023 12:15:32 +0200 Subject: [PATCH] Allow extra fields in the config Right now we cannot add extra fields to the config without it not being backwards compatible. Should follow the rules of protobuf here where you can add new fields but they must be optional. --- astacus/common/utils.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/astacus/common/utils.py b/astacus/common/utils.py index 49b35caf..306621f7 100644 --- a/astacus/common/utils.py +++ b/astacus/common/utils.py @@ -14,6 +14,7 @@ from contextlib import contextmanager from multiprocessing.dummy import Pool # fastapi + fork = bad idea from pathlib import Path +from fastapi.encoders import jsonable_encoder from pydantic import BaseModel from typing import Any, AsyncIterable, Callable, Dict, Final, Iterable, Optional, TypeVar, Union @@ -39,12 +40,6 @@ class Config: # enumname.value (it is also slightly less safe but oh well) use_enum_values = True - # Extra values should be errors, as they are most likely typos - # which lead to grief when not detected. However, if we ever - # start deprecating some old fields and not wanting to parse - # them, this might need to be revisited. - extra = "forbid" - # Validate field default values too validate_all = True @@ -60,10 +55,8 @@ def jsondict(self, **kw): # .dict() returns Python dict, but it has things that are not # json serializable. # - # We provide json seralizable dict (super inefficiently) here. - # - # This is mostly used for test code so that should be fine - return _json.loads(self.json(**kw)) + # We provide json seralizable dict (super inefficiently). + return jsonable_encoder(self.dict(**kw)) def get_or_create_state(*, state: object, key: str, factory: Callable[[], Any]) -> Any: