pierrejeambrun commented on code in PR #45089:
URL: https://github.com/apache/airflow/pull/45089#discussion_r1893172521


##########
airflow/api_fastapi/core_api/routes/public/variables.py:
##########
@@ -166,6 +167,21 @@ def post_variable(
     session: SessionDep,
 ) -> VariableResponse:
     """Create a variable."""
+    # Check for key length limit
+    if len(post_body.key) > ID_LEN:
+        raise HTTPException(
+            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
+            detail=f"Variable key exceeds maximum length of {ID_LEN} 
characters.",
+        )
+
+    # Check if the key already exists
+    existing_variable = session.scalar(select(Variable).where(Variable.key == 
post_body.key).limit(1))
+    if existing_variable:
+        raise HTTPException(
+            status_code=status.HTTP_409_CONFLICT,

Review Comment:
   Exactly



##########
airflow/api_fastapi/core_api/routes/public/variables.py:
##########
@@ -166,6 +167,21 @@ def post_variable(
     session: SessionDep,
 ) -> VariableResponse:
     """Create a variable."""
+    # Check for key length limit
+    if len(post_body.key) > ID_LEN:
+        raise HTTPException(
+            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
+            detail=f"Variable key exceeds maximum length of {ID_LEN} 
characters.",
+        )

Review Comment:
   This shouldn't be done manually like that. Pydantic have native validators 
for that.
   Something like that in the datamodel should do the exact same.
   
   ```
   Field(max_length=256)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to