fix: sdk label aliases#346
Conversation
Signed-off-by: kenwoodjw <blackxin55+@gmail.com>
Signed-off-by: kenwoodjw <blackxin55+@gmail.com>
There was a problem hiding this comment.
Code Review
This pull request refactors the I18nObject model to use standard camelCase/PascalCase-like attributes (en_US, zh_Hans, pt_BR, ja_JP) instead of lowercase ones, updating all references across the codebase and adding corresponding unit tests. A high-severity issue was identified in FormOption where overriding __init__ to set a default label fails under Pydantic v2 because validation occurs before the fallback logic is executed; using a @model_validator(mode="before") is recommended instead.
| def __init__(self, **data: object) -> None: | ||
| super().__init__(**data) | ||
| if not self.label: | ||
| self.label = I18nObject(en_us=self.value) | ||
| self.label = I18nObject(en_US=self.value) |
There was a problem hiding this comment.
In Pydantic v2, overriding __init__ to set default values for required fields (like label) does not work as expected. When super().__init__(**data) is called, Pydantic immediately validates the input data. If label is missing from data, a ValidationError is raised before the fallback logic if not self.label: can ever be reached. Conversely, if label is provided, self.label is already populated, making the fallback block dead code.\n\nTo fix this, we should use a @model_validator(mode="before") classmethod, similar to how it is done in ProviderModel and ParameterRule. This allows us to inject the fallback label into the input dictionary before Pydantic performs field validation.
@model_validator(mode="before")\n @classmethod\n def validate_label(cls, data: dict) -> dict:\n if isinstance(data, dict) and "value" in data and not data.get("label"):\n data["label"] = I18nObject(en_US=str(data["value"]))\n return dataThere was a problem hiding this comment.
Please review again
|
@fatelei @crazywoola Please help review this PR. |
Pull Request Checklist
Fixes ##345
Thank you for your contribution! Before submitting your PR, please make sure you have completed the following checks:
Compatibility Check
README.mdREADME.mdREADME.mdREADME.mdAvailable Checks
just buildhas passed