Conversation
Add support for including parent (required) programs when serializing Program objects. Introduces a programs SerializerMethodField and get_programs implementation that mirrors CourseSerializer behavior, uses BaseProgramSerializer for output, and filters parents by org_id/contract_id or excludes B2B-only programs. ProgramViewSet.get_serializer_context now sets include_programs and passes org_id/contract_id from query params to enable this behavior for single-item retrievals and readable_id lookups. Tests updated and a new test added to assert parent programs are returned (or null when not requested).
OpenAPI ChangesShow/hide ## Changes for v0.yaml:Unexpected changes? Ensure your branch is up-to-date with |
for more information, see https://pre-commit.ci
…ther-expose-its-parent-programs
Comment on lines
+172
to
+181
| programs_qs = programs_qs.filter( | ||
| program__contract_memberships__contract__organization__pk=self.context.get( | ||
| "org_id" | ||
| ) | ||
| ) | ||
| elif self.context.get("contract_id"): | ||
| programs_qs = programs_qs.filter( | ||
| program__contract_memberships__contract__pk=self.context.get( | ||
| "contract_id" | ||
| ) |
There was a problem hiding this comment.
Bug: The get_programs method can return duplicate parent programs because the underlying queryset is not deduplicated with .distinct() after joining on one-to-many relationships.
Severity: MEDIUM
Suggested Fix
Add a .distinct() call to the queryset before it is evaluated to ensure each parent program is included only once. For example, programs_qs.select_related("program").distinct().all() would prevent duplicate program objects from being added to the list.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: courses/serializers/v2/programs.py#L172-L181
Potential issue: The `get_programs` method in the `ProgramSerializer` can return
duplicate parent programs in the API response. This happens when filtering by `org_id`
or `contract_id`. The filtering involves joining across one-to-many relationships like
`program__contract_memberships`. If a program is part of multiple contracts within the
same organization, the underlying queryset will contain duplicate rows. The code then
constructs a list from this queryset without applying `.distinct()`, preserving the
duplicates in the final serialized output.
Did we get this right? 👍 / 👎 to inform future reviews.
Contributor
|
@cp-at-mit how do I make a program required for another program? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What are the relevant tickets?
https://github.com/mitodl/hq/issues/10432
Description (What does it do?)
Add support for including parent (required) programs when serializing Program objects. Introduces a programs SerializerMethodField and get_programs implementation that mirrors CourseSerializer behavior, uses BaseProgramSerializer for output, and filters parents by org_id/contract_id or excludes B2B-only programs. ProgramViewSet.get_serializer_context now sets include_programs and passes org_id/contract_id from query params to enable this behavior for single-item retrievals and readable_id lookups. Tests updated and a new test added to assert parent programs are returned (or null when not requested).
How can this be tested?
Create a program. Add that program as a requirement for another program. Call the http://mitxonline.odl.local:8013/api/v2/programs/2/ for the first program and verify that the second program is listed in the "programs" list.