]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
this changes makes api_field_* variables lazy
authorDylan Hardison <dylan@hardison.net>
Sun, 18 Dec 2022 21:22:52 +0000 (13:22 -0800)
committerDave Miller <github@justdave.net>
Sun, 18 Dec 2022 21:33:50 +0000 (16:33 -0500)
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.

Bugzilla/WebService/Bug.pm

index 7f90082133c6dbdc9d06dd2f30051ab2fef5f76d..1fb90f2cc571465af566e59b6d2d00622d2ddf24 100644 (file)
@@ -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};