Skip to content

Error with datetime object as endpoint return #1124

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

Open
ThomasMarques opened this issue Feb 18, 2025 · 1 comment · May be fixed by #1134
Open

Error with datetime object as endpoint return #1124

ThomasMarques opened this issue Feb 18, 2025 · 1 comment · May be fixed by #1134
Labels
bug Something isn't working

Comments

@ThomasMarques
Copy link

Bug Description

If a datetime.datetime (or datetime.date) is specified as the return type of an endpoint, there is an error during the generation of the OpenAPI documentation when the API starts up:

> run app.py
INFO:robyn.logger:SERVER IS RUNNING IN VERBOSE/DEBUG MODE. Set --log-level to WARN to run in production mode.
INFO:robyn.logger:Added route HttpMethod.POST /
Traceback (most recent call last):
  File ".../bug_date_robyn/app.py", line 18, in <module>
    app.start(host="0.0.0.0", port=8080)
  File ".../bug_date_robyn/.venv/lib/python3.12/site-packages/robyn/__init__.py", line 308, in start
    self._add_openapi_routes()
  File ".../bug_date_robyn/.venv/lib/python3.12/site-packages/robyn/__init__.py", line 267, in _add_openapi_routes
    self.router.prepare_routes_openapi(self.openapi, self.included_routers)
  File ".../bug_date_robyn/.venv/lib/python3.12/site-packages/robyn/router.py", line 255, in prepare_routes_openapi
    openapi.add_openapi_path_obj(lower_http_method(route.route_type), route.route, route.openapi_name, route.openapi_tags, route.function.handler)
  File ".../bug_date_robyn/.venv/lib/python3.12/site-packages/robyn/openapi.py", line 215, in add_openapi_path_obj
    modified_endpoint, path_obj = self.get_path_obj(
                                  ^^^^^^^^^^^^^^^^^^
  File ".../bug_date_robyn/.venv/lib/python3.12/site-packages/robyn/openapi.py", line 341, in get_path_obj
    response_schema = self.get_schema_object("response object", return_annotation)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../bug_date_robyn/.venv/lib/python3.12/site-packages/robyn/openapi.py", line 408, in get_schema_object
    properties["properties"][e] = self.get_schema_object(e, param_type.__annotations__[e])
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../bug_date_robyn/.venv/lib/python3.12/site-packages/robyn/openapi.py", line 407, in get_schema_object
    for e in param_type.__annotations__:
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'datetime.date' has no attribute '__annotations__'

Steps to Reproduce

Run Robyn with an endpoint that return a datetime:

  1. Create app.py:
from robyn import Robyn
from robyn.types import JSONResponse

import datetime

app = Robyn(__file__)


@app.post("/test_1")
def test_1() -> datetime.datetime:
    return datetime.datetime.now()


if __name__ == "__main__":
    app.start(host="0.0.0.0", port=8080)
  1. Run it: python app.py

It also poses a problem with datetime.date or with a JSONResponse object encapsulating the field:

@app.post("/test_2")
def test_2() -> datetime.date:
    return datetime.date.today()


class DateObject(JSONResponse):
    date: datetime.datetime


@app.post("/test_3")
def test_3() -> DateObject:
    return DateObject(date=datetime.datetime.now())

Your operating system

Linux

Your Python version (python --version)

3.12

Your Robyn version

latest

Additional Info

  • Robyn version: 0.65.0
  • Python version: 3.12.9
@ThomasMarques ThomasMarques added the bug Something isn't working label Feb 18, 2025
@Soroushsrd
Copy link

I would love to fix this one if no one's working on it

Soroushsrd added a commit to Soroushsrd/Robyn that referenced this issue Mar 5, 2025
- 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
@Soroushsrd Soroushsrd linked a pull request Mar 5, 2025 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants