最近のアップデートでカスタムフィールドのキャストに影響はありますか?

There have been multiple bug reports for the events plugin in the last two days that all seem to be type-casting issues with custom fields. The events plugin hasn’t been substantively updated in a few months.

The plugin.rb file casts event_start as an integer:

Topic.register_custom_field_type('event_start', :integer)

The error is being thrown here:

def has_event?
  self.custom_fields['event_start']&.nonzero?
end

The error itself follows this format:

NoMethodError (undefined method `nonzero?' for [1563127206, 1563127206]:Array)

As far as I understand register_custom_field_type it should ensure that the custom_field always returns as the defined type (perhaps I’m misunderstanding it).

Looking at the has_custom_fields.rb concern, there have been a few changes in the past week that could have affected this, in particular

@eviltrout Any thoughts on this?

In case this is indeed the bug causing problems, here I explain the consequences (it may make your site unusable):

This was a known bug with custom fields.

In very specific conditions, it would save the value multiple times, thus creating an Array, when you only want an Integer.

You can fix this by making sure there’s only 1 row per custom field in the database.

Yes, it’s cropped up a few times in the past. This latest rash seems to coincide with recent work on the has_custom_fields.rb concern, so there may be something to review there.

This doesn’t fix it, but helps to address it if it arises:

私の一般的なアドバイスとしては、プラグイン作者の方は、制約を適切に強制するために、マイグレーション時に常にインデックスを追加すべきです。実際、現在作成しているプラグインの多くは、絶対に必要な場合を除いてカスタムフィールドを使用せず、推論がはるかに容易なカスタムテーブルの使用を好んでいます。この具体的なケースでは、以下のインデックスが必要です。

create unique index idxStartEvent on topic_custom_fields(topic_id) where name = 'start_event'

コア側で他に何をする必要があるかはっきりしません。カスタムフィールドの刷新を検討したこともありますが、やや懸念があります。一つ検討しているのは、カスタムフィールドから配列サポートを廃止することです。長年にわたり膨大な数の問題を引き起こしているためです。

スレッドを掘り起こして申し訳ありませんが、その条件の一つは、既存の値を更新する際にシンボルインデックス(例:custom_fields[:hello])を使用した場合です。この場合、値が更新されるのではなく、別のフィールドが追加されてしまい、結果として配列が返されてしまいます。これが唯一の条件ではないかと考えられます。

これにより、発生する副作用は確実に修正されるはずです。