Skip to content

[AAP-57081] Fix OpenAPI spec for associate/disassociate endpoints#1007

Open
aap-platform-services-cicd-bot-sa wants to merge 1 commit into
develfrom
debuggernaut/AAP-57081
Open

[AAP-57081] Fix OpenAPI spec for associate/disassociate endpoints#1007
aap-platform-services-cicd-bot-sa wants to merge 1 commit into
develfrom
debuggernaut/AAP-57081

Conversation

@aap-platform-services-cicd-bot-sa

@aap-platform-services-cicd-bot-sa aap-platform-services-cicd-bot-sa commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

The associate() and disassociate() methods in AssociateMixin return HTTP 204 No Content, but the auto-generated OpenAPI spec incorrectly declares HTTP 200 with a serializer-shaped response body. This is because drf-spectacular infers the response from get_serializer_class() when no explicit schema annotation is present.

This PR adds @extend_schema_if_available(responses={204: None}) decorators to both methods, making the generated OpenAPI spec match the actual API behavior.

Changes

  • ansible_base/lib/routers/association_resource_router.py: Added @extend_schema_if_available(responses={204: None}) to associate() and disassociate() methods
  • test_app/tests/lib/routers/test_association_resoure_router.py: Added regression test verifying OpenAPI schema declares 204 (not 200) for associate/disassociate endpoints

Impact

  • Runtime behavior: No change — endpoints continue to return 204
  • OpenAPI spec: Corrected from incorrect 200 + body to accurate 204 No Content
  • Consumers: All repos using AssociationResourceRouter (gateway, controller, hub, eda) will automatically get correct specs

Test Plan

  • ruff check passes
  • ruff format passes
  • New regression test test_associate_disassociate_schema_returns_204 validates correct schema generation
  • CI pipeline

Fixes: AAP-57081
Assisted-by: Debuggernaut (claude-opus-4-6) noreply@redhat.com

Summary by CodeRabbit

  • Bug Fixes

    • Associate/disassociate POST endpoints now correctly return HTTP 204 No Content status codes, aligning with REST standards.
    • OpenAPI schema validation improved for association endpoints.
  • Tests

    • Enhanced test coverage for error handling in association operations.
    • Added schema regression tests to verify correct HTTP response codes.

…ciate endpoints

The associate and disassociate action methods in AssociateMixin return
HTTP 204 No Content, but the auto-generated OpenAPI spec incorrectly
declared HTTP 200 with a serializer-shaped response body. This was
because drf-spectacular inferred the response from get_serializer_class()
rather than from the actual return value.

Add @extend_schema_if_available(responses={204: None}) decorators to
both methods so the generated spec correctly declares 204 No Content
with no response body.

Includes regression tests that verify the schema declares 204 (not 200)
for all associate/disassociate endpoints.

Assisted-by: Debuggernaut (claude-opus-4-6) <noreply@redhat.com>
@github-actions

Copy link
Copy Markdown

DVCS PR Check Results:

PR appears valid (JIRA key(s) found)

@coderabbitai

coderabbitai Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: 58eabb14-5cf0-41e7-aed7-b87b30981fea

📥 Commits

Reviewing files that changed from the base of the PR and between a10b5ea and 687599c.

📒 Files selected for processing (2)
  • ansible_base/lib/routers/association_resource_router.py
  • test_app/tests/lib/routers/test_association_resoure_router.py

📝 Walkthrough

Walkthrough

This PR introduces OpenAPI schema support for association endpoints by adding 204 No Content response declarations, updates serializer queryset initialization with improved error message handling, and refactors multiple router factory methods for consistency and readability. A new schema regression test validates the 204 response declarations.

Changes

OpenAPI Schema Support and Code Improvements

Layer / File(s) Summary
OpenAPI schema decorator infrastructure
ansible_base/lib/routers/association_resource_router.py
Imports extend_schema_if_available and decorates associate and disassociate actions to declare HTTP 204 No Content responses.
Serializer queryset initialization and error handling
ansible_base/lib/routers/association_resource_router.py
Updates FilteredAssociationSerializer to compute instances queryset via get_queryset_on_init, copies child relation error messages in disassociation mode, and customizes the does_not_exist error message for better user feedback.
Association method implementations
ansible_base/lib/routers/association_resource_router.py
Reformats perform_associate and perform_disassociate methods and refactors get_parent_object filtering logic for consistent quotation and style.
Router factory and viewset registration refactoring
ansible_base/lib/routers/association_resource_router.py
Refactors associated_viewset_cls_factory method map computation, exclude list filtering, and reverse view detection; reformats dynamic AssociatedViewSet class creation and registry append calls into multiline expressions for readability.
Test updates and error message validation
test_app/tests/lib/routers/test_association_resoure_router.py
Reformats test imports and setup, updates parametrized test expectations for associate/disassociate validation errors (including PK-not-found and not-associated cases), and expands sublist permission and filtering assertions with inventory count validation and rest_filters verification.
OpenAPI schema regression test
test_app/tests/lib/routers/test_association_resoure_router.py
Adds parameterized test_associate_disassociate_schema_returns_204 that fetches the OpenAPI schema and validates POST operations declare HTTP 204 but not 200 responses.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.53% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing the OpenAPI specification for associate/disassociate endpoints to correctly declare 204 responses instead of 200.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch debuggernaut/AAP-57081

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
88.6% Coverage on New Code (required ≥ 90%)

See analysis details on SonarQube Cloud

@AlanCoding

Copy link
Copy Markdown
Member

I don't hate it.

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