fix: prioritize filename checks over dir checks in file type detection #5149
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixed file type detection logic to check filenames before directory paths,
preventing formatter from corrupting JSON files in directories whose names
contain content type keywords.
Problem:
The find_type_by_path() function used directory substring matching before
checking specific filenames. When a directory path contained content type
names (e.g., "Lists", "Jobs"), ANY JSON file in that directory would be
misdetected as that content type, even if it had a specific purpose like
pack_metadata.json.
This caused the formatter to apply wrong transformations to misdetected files,
stripping required fields and corrupting file structure.
Example Impact:
Root Cause:
Line ordering in find_type_by_path():
LISTS_DIR in os.path.dirname(path)(directory substring)path.name == PACKS_PACK_META_FILE_NAME(filename)Solution:
Reordered checks to prioritize filename-based detection:
Testing:
Prevents formatter corruption for any file with specific naming in directories
containing content type keywords.
relates: https://jira-dc.paloaltonetworks.com/browse/CIAC-15453