-
Notifications
You must be signed in to change notification settings - Fork 13
fix(ui): processing service details link and endpoint URL in form #1198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
037ef14
feat(ui): add project context to processing service details and optio…
mihow f399c4d
fix(ui): reformat with prettier 2.8.4 to match CI
mihow 8cbd437
fix: normalize empty endpoint_url to null across frontend and backend
mihow c81b429
fix: replace save() guard with data migration for empty endpoint_url
mihow 14e92c7
fix(ui): add null to EntityFieldValues customFields type
mihow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
ami/ml/migrations/0028_normalize_empty_endpoint_url_to_null.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from django.db import migrations | ||
|
|
||
|
|
||
| def normalize_empty_endpoint_url(apps, schema_editor): | ||
| ProcessingService = apps.get_model("ml", "ProcessingService") | ||
| ProcessingService.objects.filter(endpoint_url="").update(endpoint_url=None) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("ml", "0027_rename_last_checked_to_last_seen"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(normalize_empty_endpoint_url, migrations.RunPython.noop), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # React Form Values → DRF Serializer Behavior | ||
|
|
||
| How different form values travel from React Hook Form through the API to Django REST Framework serializers and into the database. | ||
|
|
||
| ## Value mapping for a CharField(null=True, blank=True) | ||
|
|
||
| | React form state | JSON sent | DRF `serializer.validated_data` | DB stores | | ||
| |---|---|---|---| | ||
| | field omitted / `undefined` | key absent | field uses its default (usually `""`) | `""` | | ||
| | `null` | `"field": null` | `None` | `NULL` | | ||
| | `""` (empty string) | `"field": ""` | `""` | `""` | | ||
| | `"http://..."` | `"field": "http://..."` | `"http://..."` | `"http://..."` | | ||
|
|
||
| ### Key observations | ||
|
|
||
| 1. **`undefined` and missing keys are equivalent** in JSON — `JSON.stringify({ a: undefined })` produces `{}`. DRF treats missing keys as "not provided" and uses the field's default or marks it as missing (if `required=True`). | ||
|
|
||
| 2. **Empty string `""` and `null` are different** — DRF distinguishes them. An empty string is a valid value for CharField, while `null` is only accepted when the field has `allow_null=True`. | ||
|
|
||
| 3. **React Hook Form returns `""` for cleared text inputs**, not `null` or `undefined`. If the intent is "no value", the form must explicitly normalize `""` → `null` before submission. | ||
|
|
||
| ## Convention in this project | ||
|
|
||
| For optional string fields where "no value" is a meaningful state (e.g., `endpoint_url` on ProcessingService), we use `NULL` in the database, not empty string: | ||
|
|
||
| - **Frontend**: Normalize empty strings to `null` in the `onSubmit` handler: `endpoint_url: values.endpoint_url || null` | ||
| - **Serializer**: Declare with `allow_null=True, allow_blank=False` to reject `""` at the API boundary | ||
| - **Model**: Keep `null=True, blank=True` (blank needed for Django admin), add a `save()` guard to normalize `""` → `None` | ||
| - **QuerySet filters**: Use `endpoint_url__isnull=True` instead of `Q(isnull=True) | Q(exact="")` | ||
|
|
||
| ### Example: ProcessingService.endpoint_url | ||
|
|
||
| ```python | ||
| # serializers.py — reject empty string, accept null | ||
| endpoint_url = serializers.CharField( | ||
| required=False, allow_null=True, allow_blank=False, max_length=1024 | ||
| ) | ||
|
|
||
| # models.py — safety net for admin/shell usage | ||
| def save(self, *args, **kwargs): | ||
| if self.endpoint_url == "": | ||
| self.endpoint_url = None | ||
| super().save(*args, **kwargs) | ||
| ``` | ||
|
|
||
| ```tsx | ||
| // form submit — normalize empty input to null | ||
| onSubmit={handleSubmit((values) => | ||
| onSubmit({ | ||
| name: values.name, | ||
| customFields: { | ||
| endpoint_url: values.endpoint_url || null, | ||
| }, | ||
| }) | ||
| )} | ||
| ``` | ||
|
|
||
| ## DRF serializer field flags reference | ||
|
|
||
| | Flag | Effect | | ||
| |---|---| | ||
| | `required=True` (default) | Field must be present in input | | ||
| | `required=False` | Field can be omitted; uses default | | ||
| | `allow_null=True` | Accepts JSON `null` → Python `None` | | ||
| | `allow_blank=True` | Accepts `""` for string fields | | ||
| | `allow_blank=False` (default) | Rejects `""` with validation error | | ||
|
|
||
| For `CharField` auto-generated from a model field: | ||
| - `null=True` on model → `allow_null=True` on serializer | ||
| - `blank=True` on model → `allow_blank=True`, `required=False` on serializer | ||
|
|
||
| Explicitly declaring the field on the serializer overrides these auto-generated defaults. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.