Skip to content

Commit 19cc618

Browse files
francbartoliFrancesco Bartoli
andauthored
Fix to not mutating conformance class list (#2239)
* Fix to not mutating conformance class list * Fix flake8 * Move import and update copyright --------- Co-authored-by: Francesco Bartoli <francesco.bartoli@wfp.org>
1 parent 7e47b02 commit 19cc618

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

pygeoapi/api/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Ricardo Garcia Silva <ricardo.garcia.silva@geobeyond.it>
99
#
1010
# Copyright (c) 2026 Tom Kralidis
11-
# Copyright (c) 2025 Francesco Bartoli
11+
# Copyright (c) 2026 Francesco Bartoli
1212
# Copyright (c) 2022 John A Stevenson and Colin Blackburn
1313
# Copyright (c) 2023 Ricardo Garcia Silva
1414
#
@@ -886,7 +886,7 @@ def conformance(api: API, request: APIRequest) -> Tuple[dict, int, str]:
886886

887887
apis_dict = all_apis()
888888

889-
conformance_list = CONFORMANCE_CLASSES
889+
conformance_list = list(CONFORMANCE_CLASSES)
890890

891891
for key, value in api.config['resources'].items():
892892
if value['type'] == 'process':

tests/api/test_api.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
# John A Stevenson <jostev@bgs.ac.uk>
55
# Colin Blackburn <colb@bgs.ac.uk>
66
# Bernhard Mallinger <bernhard.mallinger@eox.at>
7+
# Francesco Bartoli <xbartolone@gmail.com>
78
#
89
# Copyright (c) 2024 Tom Kralidis
910
# Copyright (c) 2022 John A Stevenson and Colin Blackburn
11+
# Copyright (c) 2026 Francesco Bartoli
1012
#
1113
# Permission is hereby granted, free of charge, to any person
1214
# obtaining a copy of this software and associated documentation
@@ -39,10 +41,10 @@
3941
import pytest
4042

4143
from pygeoapi.api import (
42-
API, APIRequest, FORMAT_TYPES, F_HTML, F_JSON, F_JSONLD, F_GZIP,
43-
__version__, validate_bbox, validate_datetime, evaluate_limit,
44-
validate_subset, landing_page, openapi_, conformance, describe_collections,
45-
get_collection_schema,
44+
API, APIRequest, CONFORMANCE_CLASSES, FORMAT_TYPES, F_HTML, F_JSON,
45+
F_JSONLD, F_GZIP, __version__, validate_bbox, validate_datetime,
46+
evaluate_limit, validate_subset, landing_page, openapi_, conformance,
47+
describe_collections, get_collection_schema,
4648
)
4749
from pygeoapi.util import yaml_load, get_api_rules, get_base_url
4850

@@ -567,6 +569,37 @@ def test_conformance(config, api_):
567569
assert rsp_headers['Content-Language'] == 'en-US'
568570

569571

572+
def test_conformance_does_not_mutate_global_list(config, api_):
573+
"""Test conformance method does not mutate CONFORMANCE_CLASSES.
574+
575+
This test verifies that the global CONFORMANCE_CLASSES list is not
576+
mutated by calls to the conformance function. The base conformance
577+
classes should remain unchanged after multiple calls.
578+
"""
579+
580+
# Store the original length and content of the global list
581+
original_length = len(CONFORMANCE_CLASSES)
582+
original_classes = list(CONFORMANCE_CLASSES)
583+
584+
req = mock_api_request()
585+
586+
# Make multiple calls to conformance
587+
for _ in range(3):
588+
conformance(api_, req)
589+
590+
# The global list should NOT have been mutated
591+
assert len(CONFORMANCE_CLASSES) == original_length, (
592+
f'Global CONFORMANCE_CLASSES was mutated! '
593+
f'Original length: {original_length}, '
594+
f'Current length: {len(CONFORMANCE_CLASSES)}. '
595+
f'The conformance() function should create a copy of the list '
596+
f'before extending it.'
597+
)
598+
assert CONFORMANCE_CLASSES == original_classes, (
599+
'Global CONFORMANCE_CLASSES content was modified'
600+
)
601+
602+
570603
def test_describe_collections(config, api_):
571604
req = mock_api_request({"f": "html"})
572605
rsp_headers, code, response = describe_collections(api_, req)

0 commit comments

Comments
 (0)