Fix empty egg config_files causing fatal 500 error (#2195)#2197
Fix empty egg config_files causing fatal 500 error (#2195)#2197lancepioch merged 2 commits intomainfrom
Conversation
When the Configuration Files field on an egg is cleared and saved, json_decode returns null which crashes the server creation/startup flow with a TypeError. Handle null/empty config_files gracefully at the service and transformer level, and add a default to the edit form to match the create form.
📝 WalkthroughWalkthroughDefaults and type guards for egg configuration fields were added across the admin UI, API transformer, and service layer so empty or null configuration fields are treated as an empty JSON object and validated before processing. Changes
Sequence Diagram(s)sequenceDiagram
participant AdminUI as Admin UI (EditEgg)
participant API as API Transformer
participant Service as EggConfigurationService
participant Processor as replacePlaceholders
AdminUI->>API: Submit egg data (config_files may be blank)
API->>API: Coerce null/empty -> '{}' and json_decode fields
API->>Service: Pass transformed payload (inherit_config_files decoded)
Service->>Service: json_decode with fallback '{}', validate is array/object
alt decoded is array/object
Service->>Processor: call replacePlaceholders(with decoded config)
Processor-->>Service: return processed configs
else not container
Service->>Service: use empty array for configs
end
Service-->>API: return processed configuration/result
API-->>AdminUI: respond with success
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
app/Transformers/Api/Application/EggTransformer.php (1)
54-58:⚠️ Potential issue | 🟠 MajorApply null-safety pattern consistently to all config JSON fields.
inherit_config_startup(line 56) andinherit_config_logs(line 58) are decoded without null guards, butinherit_config_files(line 39) uses the pattern?? '{}'to provide a safe default. Since all three fields are nullable, apply the same pattern to maintain consistency:'startup' => json_decode($model->inherit_config_startup ?? '{}', true), 'logs' => json_decode($model->inherit_config_logs ?? '{}', true),
🤖 Fix all issues with AI agents
In `@app/Filament/Admin/Resources/Eggs/Pages/EditEgg.php`:
- Around line 319-322: The Textarea field Textarea::make('config_files') in
EditEgg.php uses ->default('{}') which doesn't affect records on the EditRecord
page because existing state is hydrated from the DB; to ensure empty submissions
are saved as '{}' instead of null/empty, add a dehydration coercion on that
field such as ->dehydrateStateUsing(fn($state) => blank($state) ? '{}' : $state)
or mark it ->nullable() and implement a mutator to replace blank values with
'{}' before save; keep downstream guards in EggConfigurationService and
EggTransformer but ensure the field-level dehydrate/mutator fixes empty-save
behavior.
In `@app/Transformers/Api/Application/EggTransformer.php`:
- Line 39: The json_decode call for $files uses the null-coalescing operator
(??) which doesn't guard against empty strings and will still throw
JsonException for ''. Update the fallback so that inherit_config_files is
treated as falsy (empty string included) before decoding—i.e., use a
falsy-coalescing approach so json_decode receives '{}' when
$model->inherit_config_files is null or ''. Locate the json_decode(...) line
that references $model->inherit_config_files and replace the ?? fallback with a
falsy fallback (so decoding gets '{}' when the stored value is null/empty) while
keeping JSON_THROW_ON_ERROR.
- Use ?: instead of ?? in EggTransformer so empty strings also fall back to '{}'
- Apply same null-safety to config_startup and config_logs json_decode calls
- Replace ineffective default('{}') with dehydrateStateUsing on EditEgg form
When the Configuration Files field on an egg is cleared and saved, json_decode returns null which crashes the server creation/startup flow with a TypeError. Handle null/empty config_files gracefully at the service and transformer level, and add a default to the edit form to match the create form.
Fixes #2195