Skip to content

Commit

Permalink
Allow empty value while variable creation (apache#45402)
Browse files Browse the repository at this point in the history
* fix

* fix backend

* added tests
  • Loading branch information
shubhamraj-git authored Jan 4, 2025
1 parent de6d83a commit 8a3d0f4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 34 deletions.
4 changes: 2 additions & 2 deletions airflow/api_fastapi/core_api/datamodels/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class VariableResponse(BaseModel):
model_config = ConfigDict(populate_by_name=True, from_attributes=True)

key: str
val: str | None = Field(alias="value")
val: str = Field(alias="value")
description: str | None
is_encrypted: bool

Expand All @@ -56,7 +56,7 @@ class VariableBody(BaseModel):
"""Variable serializer for bodies."""

key: str = Field(max_length=ID_LEN)
value: str | None = Field(serialization_alias="val")
value: str = Field(serialization_alias="val")
description: str | None = Field(default=None)


Expand Down
8 changes: 2 additions & 6 deletions airflow/api_fastapi/core_api/openapi/v1-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9736,9 +9736,7 @@ components:
maxLength: 250
title: Key
value:
anyOf:
- type: string
- type: 'null'
type: string
title: Value
description:
anyOf:
Expand Down Expand Up @@ -9773,9 +9771,7 @@ components:
type: string
title: Key
value:
anyOf:
- type: string
- type: 'null'
type: string
title: Value
description:
anyOf:
Expand Down
18 changes: 2 additions & 16 deletions airflow/ui/openapi-gen/requests/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5485,14 +5485,7 @@ export const $VariableBody = {
title: "Key",
},
value: {
anyOf: [
{
type: "string",
},
{
type: "null",
},
],
type: "string",
title: "Value",
},
description: {
Expand Down Expand Up @@ -5540,14 +5533,7 @@ export const $VariableResponse = {
title: "Key",
},
value: {
anyOf: [
{
type: "string",
},
{
type: "null",
},
],
type: "string",
title: "Value",
},
description: {
Expand Down
4 changes: 2 additions & 2 deletions airflow/ui/openapi-gen/requests/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ export type ValidationError = {
*/
export type VariableBody = {
key: string;
value: string | null;
value: string;
description?: string | null;
};

Expand All @@ -1293,7 +1293,7 @@ export type VariableCollectionResponse = {
*/
export type VariableResponse = {
key: string;
value: string | null;
value: string;
description: string | null;
is_encrypted: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const EditVariableButton = ({ variable }: Props) => {
const initialVariableValue: VariableBody = {
description: variable.description ?? "",
key: variable.key,
value: variable.value ?? "",
value: variable.value,
};
const { editVariable, error, isPending, setError } = useEditVariable(initialVariableValue, {
onSuccessConfirm: onClose,
Expand Down
10 changes: 3 additions & 7 deletions airflow/ui/src/pages/Variables/ManageVariable/VariableForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,14 @@ const VariableForm = ({ error, initialVariable, isPending, manageMutate, setErro
<Controller
control={control}
name="value"
render={({ field, fieldState }) => (
<Field.Root invalid={Boolean(fieldState.error)} mt={4} required>
render={({ field }) => (
<Field.Root mt={4}>
<Field.Label fontSize="md">
Value <Field.RequiredIndicator />
</Field.Label>
<Textarea {...field} required size="sm" />
{fieldState.error ? <Field.ErrorText>{fieldState.error.message}</Field.ErrorText> : undefined}
<Textarea {...field} size="sm" />
</Field.Root>
)}
rules={{
required: "Value is required",
}}
/>

<Controller
Expand Down
32 changes: 32 additions & 0 deletions tests/api_fastapi/core_api/routes/public/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,19 @@ class TestPostVariable(TestVariableEndpoint):
"is_encrypted": True,
},
),
(
{
"key": "empty value variable",
"value": "",
"description": "some description",
},
{
"key": "empty value variable",
"value": "",
"description": "some description",
"is_encrypted": True,
},
),
],
)
def test_post_should_respond_201(self, test_client, session, body, expected_response):
Expand Down Expand Up @@ -400,6 +413,25 @@ def test_post_should_respond_422_when_key_too_large(self, test_client):
]
}

def test_post_should_respond_422_when_value_is_null(self, test_client):
body = {
"key": "null value key",
"value": None,
"description": "key too large",
}
response = test_client.post("/public/variables", json=body)
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"type": "string_type",
"loc": ["body", "value"],
"msg": "Input should be a valid string",
"input": None,
}
]
}


class TestImportVariables(TestVariableEndpoint):
@pytest.mark.enable_redact
Expand Down

0 comments on commit 8a3d0f4

Please sign in to comment.