Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix datetime object handling in OpenAPI schema generation #1134

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Soroushsrd
Copy link

Description

This PR fixes #1124

Summary

This PR fixes the OpenAPI schema generation for endpoints that return datetime objects. Previously, the application would fail to start with an AttributeError when attempting to generate OpenAPI documentation for endpoints returning datetime.datetime or datetime.date objects.

Changes made:

  • Added special handling for datetime.datetime and datetime.date types in schema generation
  • Added proper OpenAPI schema type ("string") and format ("date-time"/"date") for datetime objects
  • Added unit tests for datetime schema generation

PR Checklist

Please ensure that:

  • The PR contains a descriptive title
  • The PR contains a descriptive summary of the changes
  • You build and test your changes before submitting a PR.
  • You have added relevant documentation
  • You have added relevant tests. We prefer integration tests wherever possible

Pre-Commit Instructions:

- Add special handling for datetime.datetime and datetime.date types
- Add schema generation tests for datetime types
- Fix AttributeError when generating schema for datetime return types

Fixes sparckles#1124
Copy link

vercel bot commented Mar 5, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
robyn ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 5, 2025 4:54pm

if param_type.__module__ == "typing":
properties["anyOf"] = [{"type": self.get_openapi_type(param_type.__args__[0])}, {"type": "null"}]
from typing import Optional, Union
Copy link
Member

Choose a reason for hiding this comment

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

@Soroushsrd , thanks for the PR but we can move the imports to the top of the file, right?

@sansyrox
Copy link
Member

sansyrox commented Mar 6, 2025

Hey @Soroushsrd 👋

One more thing

Lint PR / lint (pull_request)Failing after 1m

Lint PR is failing as well

@Soroushsrd
Copy link
Author

Thank you for the comments. Will fix it ASAP

@tavallaie
Copy link
Contributor

Hi @Soroushsrd, some of the test for python 3.9 and 3.10 failing, can you fix them?

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.

Error with datetime object as endpoint return
3 participants