Skip to content

IA-5415: Fix org unit types infinite recursion - #3342

Merged
Phil-V merged 4 commits into
developfrom
fix/IA-5415-org-unit-types-recursion
Sep 23, 2026
Merged

Phil-V merged 4 commits into
developfrom
fix/IA-5415-org-unit-types-recursion

Conversation

@Phil-V

@Phil-V Phil-V commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

What problem is this PR solving?

The OrgUnitType api could throw RecursionError when sub_unit_types or allow_creating_sub_unit_types references were forming a loop. This PR adds extra validation to prevent these cycles and makes the API more resilient against existing loops.

Related JIRA tickets

IA-5415

Changes

  • Add validation for allow_creating_sub_unit_types to prevent loops.
  • Fix some api responses that were likely intended to have depth of 1 but were recursively loading related OrgUnitTypes.
  • Make the api more resilient against loops if they somehow exist (admin, import scripts, etc.).

How to test

Field validation:

  • Go to the OrgUnitTypes page (Org units > Configuration > Organization unit type)
  • Edit some org unit types and set their "Sub org unit types to display/create" fields and try to create infinite loops (A -> B, B -> A for example).
  • The api should return a validation error.

Api resilience against loops:

  • Through the django admin, set up some OrgUnitTypes that reference each other in a loop through their sub_unit_types and allow_creating_sub_unit_types attributes.
  • Check the following endpoints for errors:
    GET /api/v2/orgunittypes/?fields=id,name,sub_unit_types
    GET /api/v2/orgunittypes/?fields=id,name,allow_creating_sub_unit_type
    GET /api/v2/orgunittypes/1/hierarchy/ (with the pk of a type involved in the loop)

Print screen / video

/

Notes

  • Opted not to add validation to the django admin. I was concerned this could create situations where it becomes impossible to fix or modify the OrgUnitTypes without shell/db access.
  • Nested DynamicFieldsModelSerializer still use the fields param from the request to override the default fields, creating the potential for recursion. Judging from how the serializers were defined (with a list of fields that don't include the subtypes), this was likely not to be the intended response. As a workaround,ignore_dynamic_fields was added to suppress that behavior.

Doc

/

@Phil-V
Phil-V marked this pull request as ready for review September 23, 2026 09:07

@bmonjoie bmonjoie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍
Great job

@Phil-V
Phil-V merged commit e01768a into develop Sep 23, 2026
16 checks passed
@Phil-V
Phil-V deleted the fix/IA-5415-org-unit-types-recursion branch September 23, 2026 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants