From ba30bdc39d27f62cc468006fd7b9886b60613617 Mon Sep 17 00:00:00 2001 From: Tiemo Kieft Date: Fri, 16 Jan 2026 12:04:18 +0100 Subject: [PATCH] Improve aliases for Pydantic models The pydantic models generated include field aliases for fields that use camel case as opposed to snake case (customary in Python). The alias definitions are compatible with python's builtin dataclasses and are correctly detected by LSPs, however in the same models the `populate_by_name` option is set to True. This option allows the instantiotion using the actual field names (snake case) instead of the defined alias (camcel case). This options is not correctly recognized by most LSP and therefore results in two warnings per field. The first warning states that hte snake case version of the field is not a valid argument, and the second warning states that the camcel case version of the argument is missing. Exampel: ```python class WebSessionInfo(BaseModel): model_config = { "populate_by_name": True, } session_id: str = Field(alias="sessionID") ``` The above model can be instantiated like so: `WebSessionInfo(session_id='abc')`, however this results in two warnings (`session_id` is not a valid argument, and `sessionID` is missing). Settings `validation_alias` and `serialization_alias` results in the exact same behavior as far as validation and serialization are concerned, however they are not standard dataclass options and are therefore not recognized by LSPs. This removes the warnings. --- .../codegen/languages/AbstractPythonPydanticV1Codegen.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java index 7591c1df7c37..0fa3e5345f2a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java @@ -908,7 +908,8 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) { // field if (cp.baseName != null && !cp.baseName.equals(cp.name)) { // base name not the same as name - fields.add(String.format(Locale.ROOT, "alias=\"%s\"", cp.baseName)); + fields.add(String.format(Locale.ROOT, "validation_alias=\"%s\"", cp.baseName)); + fields.add(String.format(Locale.ROOT, "serialization_alias=\"%s\"", cp.baseName)); } if (!StringUtils.isEmpty(cp.description)) { // has description