Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions packages/gapic-generator/gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import collections
import copy
import dataclasses
import functools
import json
import keyword
import re
Expand Down Expand Up @@ -1289,14 +1288,14 @@ def _to_regex(self, path_template: str) -> Pattern:
"""
return re.compile(f"^{self._convert_to_regex(path_template)}$")

# Use caching to avoid repeated computation
@functools.cache
def to_regex(self) -> Pattern:
@utils.cached_property
def _regex(self) -> Pattern:
return self._to_regex(self.path_template)

@property
# Use caching to avoid repeated computation
@functools.cache
def to_regex(self) -> Pattern:
return self._regex

@utils.cached_property
def key(self) -> Union[str, None]:
if self.path_template == "":
return self.field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

from gapic.schema import wrappers

import gc
import json
import uuid
import weakref
import proto
import pytest

Expand Down Expand Up @@ -146,6 +149,21 @@ def test_routing_parameter_key(field, path_template, expected):
assert param.key == expected


def test_routing_parameter_cache_does_not_retain_instance():
unique_id = f"id_{uuid.uuid4().hex}"
param = wrappers.RoutingParameter(
f"table_name_{unique_id}", f"{{{unique_id}=projects/*}}/instances/*/**"
)
Comment thread
parthea marked this conversation as resolved.
_ = param.to_regex()
_ = param.key
param_ref = weakref.ref(param)

del param
gc.collect()

assert param_ref() is None


def test_routing_parameter_multi_segment_raises():
param = wrappers.RoutingParameter(
"table_name", "{project_id=projects/*}/{instance_id=instances/*}/*/**"
Expand Down
Loading