Skip to content

Error with datetime object as endpoint return #1124

@ThomasMarques

Description

@ThomasMarques

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions