From: Dylan Hardison Date: Sun, 18 Dec 2022 21:22:52 +0000 (-0800) Subject: this changes makes api_field_* variables lazy X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=17a80574d362743247c10b8c0b4c594513a6bd82;p=thirdparty%2Fbugzilla.git this changes makes api_field_* variables lazy api_field_types and api_field_names rely on values from the Bugzilla->fields definition. This in turn requires checking the schema of the bugs table at run time. That means trying to access these when a database is not configured, or before schema migrations have happened is an error. This changes those variables to hold functions (sub refs) that are comptued when called. The help performance not degrade, we make use of the Bugzilla::request_cache to only re-compute this once per request. --- diff --git a/Bugzilla/WebService/Bug.pm b/Bugzilla/WebService/Bug.pm index 7f9008213..1fb90f2cc 100644 --- a/Bugzilla/WebService/Bug.pm +++ b/Bugzilla/WebService/Bug.pm @@ -110,16 +110,23 @@ use constant ATTACHMENT_MAPPED_RETURNS => { mimetype => 'content_type', }; -our %api_field_types = ( - %{{map { $_ => 'double' } Bugzilla::Bug::NUMERIC_COLUMNS()}}, - %{{map { $_ => 'dateTime' } Bugzilla::Bug::DATE_COLUMNS()}}, -); +our $api_field_types = sub { + Bugzilla::request_cache->{api_field_types} ||= { + %{{map { $_ => 'double' } Bugzilla::Bug::NUMERIC_COLUMNS()}}, + %{{map { $_ => 'dateTime' } Bugzilla::Bug::DATE_COLUMNS()}}, + }; +}; -our %api_field_names = reverse %{Bugzilla::Bug::FIELD_MAP()}; +our $api_field_names = sub { + Bugzilla::request_cache->{api_field_types} ||= do { + my $tmp = {reverse %{Bugzilla::Bug::FIELD_MAP()}}; -# This doesn't normally belong in FIELD_MAP, but we do want to translate -# "bug_group" back into "groups". -$api_field_names{'bug_group'} = 'groups'; + # This doesn't normally belong in FIELD_MAP, but we do want to translate + # "bug_group" back into "groups". + $tmp->{'bug_group'} = 'groups'; + $tmp; + }; +}; ###################################################### # Add aliases here for old method name compatibility # @@ -841,9 +848,10 @@ sub update { } my %changes = %{$all_changes{$bug->id}}; + my $names = $api_field_names->(); foreach my $field (keys %changes) { my $change = $changes{$field}; - my $api_field = $api_field_names{$field} || $field; + my $api_field = $names->{$field} || $field; # We normalize undef to an empty string, so that the API # stays consistent for things like Deadline that can become @@ -1760,8 +1768,8 @@ sub _changeset_to_hash { foreach my $change (@{$changeset->{changes}}) { my $field_name = delete $change->{fieldname}; - my $api_field_type = $api_field_types{$field_name} || 'string'; - my $api_field_name = $api_field_names{$field_name} || $field_name; + my $api_field_type = $api_field_types->()->{$field_name} || 'string'; + my $api_field_name = $api_field_names->()->{$field_name} || $field_name; my $attach_id = delete $change->{attachid}; my $comment = delete $change->{comment};