Skip to content

refactor(api): migrate workspace account auxiliary responses to BaseModel#35220

Open
ai-hpc wants to merge 3 commits intolanggenius:mainfrom
ai-hpc:refactor/migrate-console-workspace-account-education-responses-28015
Open

refactor(api): migrate workspace account auxiliary responses to BaseModel#35220
ai-hpc wants to merge 3 commits intolanggenius:mainfrom
ai-hpc:refactor/migrate-console-workspace-account-education-responses-28015

Conversation

@ai-hpc
Copy link
Copy Markdown
Contributor

@ai-hpc ai-hpc commented Apr 14, 2026

Part of #28015

Summary

Refine the api/controllers/console/workspace/account.py BaseModel migration for workspace account auxiliary responses.

  • Keep auxiliary response schemas (AccountIntegrate*, Education*) on Pydantic ResponseModel.
  • Normalize timestamp fields from billing/integration responses with support for datetime | int | ISO string.
  • Adjust integrate serialization to validate full payload via AccountIntegrateListResponse.model_validate(...).
  • Keep strict education autocomplete response shape (data, curr_page, has_next required).
  • Use explicit DB session scope for get_account_by_email_with_case_fallback(...) in change-email flow.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint and make type-check (backend) and cd web && pnpm exec vp staged (frontend) to appease the lint gods
  • I ran make lint (backend)

@ai-hpc ai-hpc requested a review from QuantumGhost as a code owner April 14, 2026 21:02
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. refactor labels Apr 14, 2026
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Apr 14, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-14 21:17:32.597365981 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-14 21:17:23.934156242 +0000
@@ -16,6 +16,8 @@
   --> controllers/console/setup.py:65:2
 ERROR Object of class `MissingRouter` has no attribute `get` [missing-attribute]
   --> controllers/console/version.py:30:2
+ERROR Unexpected keyword argument `session` in function `services.account_service.AccountService.get_account_by_email_with_case_fallback` [unexpected-keyword]
+   --> controllers/console/workspace/account.py:597:94
 ERROR Object of class `object` has no attribute `value` [missing-attribute]
   --> controllers/service_api/app/conversation.py:87:24
 ERROR Class member `EasyUIBasedGenerateTaskPipeline._application_generate_entity` overrides parent class `BasedGenerateTaskPipeline` in an inconsistent manner [bad-override]

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refines the BaseModel migration work in the workspace account console controller by keeping auxiliary response payloads on ResponseModel, tightening education autocomplete response shape, and centralizing timestamp normalization for billing/integration responses.

Changes:

  • Added _to_timestamp normalization to support datetime | int | ISO string inputs for billing/integration timestamps.
  • Updated account integrate list serialization to validate the full payload via AccountIntegrateListResponse.model_validate(...).
  • Made education autocomplete response fields required (data, curr_page, has_next) and removed {} fallbacks for billing responses.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +178 to 179
def _serialize_account(account) -> dict:
return AccountResponse.model_validate(account, from_attributes=True).model_dump(mode="json")
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

_serialize_account now returns an unparameterized dict, which effectively becomes dict[Any, Any] and loses the guarantee that keys are strings (and makes type-checking less useful). Consider keeping the more precise return type (e.g., dict[str, Any]) to preserve the intent and avoid accidental non-string keys/values creeping in.

Copilot uses AI. Check for mistakes.
Comment on lines +182 to +189
def _to_timestamp(value: datetime | int | str | None) -> int | None:
if value is None:
return None
if isinstance(value, int):
return value
if isinstance(value, datetime):
return int(value.timestamp())
return value
return int(datetime.fromisoformat(value).astimezone(pytz.utc).timestamp())
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

_to_timestamp uses datetime.fromisoformat(value).astimezone(pytz.utc) for string inputs, which will raise for common ISO variants like a trailing Z (e.g., 2024-01-01T00:00:00Z) and for naive datetimes (no tzinfo). This can cause response validation to fail and return 500s for billing/integration responses. Consider normalizing Z to +00:00, handling naive values by assuming UTC (or explicitly rejecting them with a clear error), and wrapping parsing in a try/except to produce a controlled failure mode.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants