From 0bd0395caec12aec1c318425a03a7dfc36de08eb Mon Sep 17 00:00:00 2001 From: Ivan Yordanov Date: Tue, 24 Oct 2023 22:01:15 +0300 Subject: [PATCH] fix: json schema KafkaConnectorConfig --- kpops/component_handlers/kafka_connect/model.py | 14 +++++++++++--- kpops/pipeline_generator/pipeline.py | 2 +- kpops/utils/pydantic.py | 11 ++++++----- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/kpops/component_handlers/kafka_connect/model.py b/kpops/component_handlers/kafka_connect/model.py index c79dd8dc6..d40f2f1c5 100644 --- a/kpops/component_handlers/kafka_connect/model.py +++ b/kpops/component_handlers/kafka_connect/model.py @@ -4,7 +4,7 @@ from pydantic import BaseModel, ConfigDict, Field, field_validator from typing_extensions import override -from kpops.utils.pydantic import CamelCaseConfigModel, DescConfigModel, to_dot +from kpops.utils.pydantic import CamelCaseConfigModel, to_dot, DescConfigModel class KafkaConnectorType(str, Enum): @@ -17,11 +17,17 @@ class KafkaConnectorConfig(DescConfigModel): connector_class: str name: str | None = Field(default=None) + + @override + @staticmethod + def json_schema_extra(schema: dict[str, Any], model: type[BaseModel]) -> None: + super(KafkaConnectorConfig, KafkaConnectorConfig).json_schema_extra(schema, model) + schema["additional_properties"] = {"type": "string"} + model_config = ConfigDict( extra="allow", alias_generator=to_dot, - # TODO(sujuka99): combine with ``json_schema_extra`` of ``DescCohnfigModel`` - json_schema_extra={"additional_properties": {"type": "string"}}, + json_schema_extra=json_schema_extra, ) @field_validator("connector_class") @@ -35,6 +41,7 @@ def connector_class_must_contain_dot(cls, connector_class: str) -> str: def class_name(self) -> str: return self.connector_class.split(".")[-1] + # TODO(Ivan Yordanov): replace with a function decorated with `@model_serializer` @override def model_dump(self, **_) -> dict[str, Any]: return super().model_dump(by_alias=True, exclude_none=True) @@ -81,6 +88,7 @@ class KafkaConnectResetterValues(CamelCaseConfigModel): config: KafkaConnectResetterConfig name_override: str + # TODO(Ivan Yordanov): replace with a function decorated with `@model_serializer` @override def model_dump(self, **_) -> dict[str, Any]: return super().model_dump(by_alias=True, exclude_none=True) diff --git a/kpops/pipeline_generator/pipeline.py b/kpops/pipeline_generator/pipeline.py index c1b3488ae..f4676105c 100644 --- a/kpops/pipeline_generator/pipeline.py +++ b/kpops/pipeline_generator/pipeline.py @@ -279,7 +279,7 @@ def substitute_in_component(self, component_as_dict: dict) -> dict: config = self.config # Leftover variables that were previously introduced in the component by the substitution # functions, still hardcoded, because of their names. - # TODO: Get rid of them + # TODO(Ivan Yordanov): Get rid of them substitution_hardcoded = { "error_topic_name": config.topic_name_config.default_error_topic_name, "output_topic_name": config.topic_name_config.default_output_topic_name, diff --git a/kpops/utils/pydantic.py b/kpops/utils/pydantic.py index 1dc5063cd..97fcccdaa 100644 --- a/kpops/utils/pydantic.py +++ b/kpops/utils/pydantic.py @@ -21,10 +21,6 @@ def to_dot(s: str) -> str: return s.replace("_", ".") -def schema_extra(schema: dict[str, Any], model: type[BaseModel]) -> None: - schema["description"] = describe_object(model.__doc__) - - class CamelCaseConfigModel(BaseModel): model_config = ConfigDict( alias_generator=to_camel, @@ -33,4 +29,9 @@ class CamelCaseConfigModel(BaseModel): class DescConfigModel(BaseModel): - model_config = ConfigDict(json_schema_extra=schema_extra) + + @staticmethod + def json_schema_extra(schema: dict[str, Any], model: type[BaseModel]) -> None: + schema["description"] = describe_object(model.__doc__) + + model_config = ConfigDict(json_schema_extra=json_schema_extra)