FIX: Ignore missing uploads in theme settings (#13486)

FIX: Ignore missing uploads in theme settings (#13486)

In some rare cases, this could prevent the site from bootstrapping, because theme settings are invoked early in the application.

diff --git a/app/models/theme.rb b/app/models/theme.rb
index fe71405..494e6fa 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -527,7 +527,9 @@ class Theme < ActiveRecord::Base
 
     theme_uploads = {}
     upload_fields.each do |field|
-      theme_uploads[field.name] = Discourse.store.cdn_url(field.upload.url)
+      if field.upload&.url
+        theme_uploads[field.name] = Discourse.store.cdn_url(field.upload.url)
+      end
     end
     hash['theme_uploads'] = theme_uploads if theme_uploads.present?
 
diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb
index 4f121fa..90550ff 100644
--- a/spec/models/theme_spec.rb
+++ b/spec/models/theme_spec.rb
@@ -504,6 +504,20 @@ HTML
     expect(json["theme_uploads"]["bob"]).to eq(upload.url)
   end
 
+  it 'does not break on missing uploads in settings' do
+    Theme.destroy_all
+
+    upload = UploadCreator.new(file_from_fixtures("logo.png"), "logo.png").create_for(-1)
+    theme.set_field(type: :theme_upload_var, target: :common, name: "bob", upload_id: upload.id)
+    theme.save!
+
+    Upload.find(upload.id).destroy
+    theme.clear_cached_settings!
+
+    json = JSON.parse(cached_settings(theme.id))
+    expect(json).to be_empty
+  end
+
   it 'uses CDN url for theme_uploads in settings' do
     set_cdn_url("http://cdn.localhost")
     Theme.destroy_all

GitHub sha: c8f4f7c235ca38b39d4b66bf9e1305410e36815e

This commit appears in #13486 which was approved by tgxworld. It was merged by pmusaraj.