Skip to content

Support date only type #89

Description

@tractorcow

Is your feature request related to a problem? Please describe.

When I am creating fields with type string, and format "date" I would like them to be typed as python datetime.date

"birth_date": {
	"type": "string",
	"format": "date"
},			

Describe the solution you'd like

Logic in type_converter when, with orjson enabled, types are generated as "datetime.date".

Describe alternatives you've considered

Potentially some other hooks or injectable logic inside type_converter that could allow a developer to inject or otherwise override type generation. This could be some kind of type registry.

Additional context
I am generating schema for date types that do not have time component, and would be incorrect to use datetime type.

Activity

  1. tractorcow commented on Mar 10, 2025

    @tractorcow
    Author

    Alternatively, if a type is generated as "type": "string", and some unknown "format", it should at least type as a string rather than erroring.

        raise TypeError(f"Unknown type: {schema.type}")
    TypeError: Unknown type: DataType.STRING
    make: *** [makefile:11: generate-sdk] Error 1
    
  2. dougborg commented on Oct 27, 2025

    @dougborg

    ✅ Implemented in PR #122

    Date-only type support has been added! String fields with format: "date" now generate datetime.date type.

    Usage

    OpenAPI Spec:

    {
      "birth_date": {
        "type": "string",
        "format": "date"
      }
    }

    Generated Code:

    from datetime import date
    
    class Person(BaseModel):
        birth_date: date

    Requirements

    • Requires use_orjson=True when generating code (same as other format conversions like UUID and datetime)
    • Falls back to str type when orjson is disabled

    Implementation

    The feature leverages the refactored _handle_format_conversions() function from #98, making it a clean 9-line addition in a single location.

    Testing

    • ✅ test_date_format_generates_date_type: Verifies proper date type generation
    • ✅ test_date_format_without_orjson: Verifies str fallback behavior
    • ✅ All 252 tests pass

    The feature will be available once PR #122 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions