Skip to content

Commit

Permalink
fix: json schema KafkaConnectorConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
sujuka99 committed Oct 24, 2023
1 parent f80d6e8 commit 0bd0395
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
14 changes: 11 additions & 3 deletions kpops/component_handlers/kafka_connect/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion kpops/pipeline_generator/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions kpops/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

0 comments on commit 0bd0395

Please sign in to comment.